Skip to content

Commit 022cb87

Browse files
committed
Restore Java worker
1 parent b3a3d6e commit 022cb87

File tree

3 files changed

+200
-7
lines changed

3 files changed

+200
-7
lines changed

worker/Dockerfile

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
FROM microsoft/dotnet:1.0.0-preview1
1+
FROM java:openjdk-8-jdk-alpine
22

3-
WORKDIR /app
3+
RUN MAVEN_VERSION=3.3.3 \
4+
&& cd /usr/share \
5+
&& wget http://archive.apache.org/dist/maven/maven-3/$MAVEN_VERSION/binaries/apache-maven-$MAVEN_VERSION-bin.tar.gz -O - | tar xzf - \
6+
&& mv /usr/share/apache-maven-$MAVEN_VERSION /usr/share/maven \
7+
&& ln -s /usr/share/maven/bin/mvn /usr/bin/mvn
48

5-
ADD src/ /app/src/
9+
WORKDIR /code
610

7-
RUN dotnet restore -v minimal src/ \
8-
&& dotnet publish -c Release -o ./ src/Worker/ \
9-
&& rm -rf src/ $HOME/.nuget/
11+
ADD pom.xml /code/pom.xml
12+
RUN ["mvn", "dependency:resolve"]
13+
RUN ["mvn", "verify"]
1014

11-
CMD dotnet Worker.dll
15+
# Adding source, compile and package into a fat jar
16+
ADD src /code/src
17+
RUN ["mvn", "package"]
18+
19+
CMD ["java", "-jar", "target/worker-jar-with-dependencies.jar"]

worker/pom.xml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>worker</groupId>
7+
<artifactId>worker</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.json</groupId>
14+
<artifactId>json</artifactId>
15+
<version>20140107</version>
16+
</dependency>
17+
18+
<dependency>
19+
<groupId>redis.clients</groupId>
20+
<artifactId>jedis</artifactId>
21+
<version>2.7.2</version>
22+
<type>jar</type>
23+
<scope>compile</scope>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.postgresql</groupId>
28+
<artifactId>postgresql</artifactId>
29+
<version>9.4-1200-jdbc41</version>
30+
</dependency>
31+
</dependencies>
32+
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-jar-plugin</artifactId>
38+
<version>2.4</version>
39+
<configuration>
40+
<finalName>worker</finalName>
41+
<archive>
42+
<manifest>
43+
<addClasspath>true</addClasspath>
44+
<mainClass>worker.Worker</mainClass>
45+
<classpathPrefix>dependency-jars/</classpathPrefix>
46+
</manifest>
47+
</archive>
48+
</configuration>
49+
</plugin>
50+
<plugin>
51+
<groupId>org.apache.maven.plugins</groupId>
52+
<artifactId>maven-compiler-plugin</artifactId>
53+
<version>3.1</version>
54+
<configuration>
55+
<source>1.7</source>
56+
<target>1.7</target>
57+
</configuration>
58+
</plugin>
59+
<plugin>
60+
<groupId>org.apache.maven.plugins</groupId>
61+
<artifactId>maven-assembly-plugin</artifactId>
62+
<executions>
63+
<execution>
64+
<goals>
65+
<goal>attached</goal>
66+
</goals>
67+
<phase>package</phase>
68+
<configuration>
69+
<finalName>worker</finalName>
70+
<descriptorRefs>
71+
<descriptorRef>jar-with-dependencies</descriptorRef>
72+
</descriptorRefs>
73+
<archive>
74+
<manifest>
75+
<mainClass>worker.Worker</mainClass>
76+
</manifest>
77+
</archive>
78+
</configuration>
79+
</execution>
80+
</executions>
81+
</plugin>
82+
</plugins>
83+
</build>
84+
</project>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package worker;
2+
3+
import redis.clients.jedis.Jedis;
4+
import redis.clients.jedis.exceptions.JedisConnectionException;
5+
import java.sql.*;
6+
import org.json.JSONObject;
7+
8+
class Worker {
9+
public static void main(String[] args) {
10+
try {
11+
Jedis redis = connectToRedis("redis");
12+
Connection dbConn = connectToDB("db");
13+
14+
System.err.println("Watching vote queue");
15+
16+
while (true) {
17+
String voteJSON = redis.blpop(0, "votes").get(1);
18+
JSONObject voteData = new JSONObject(voteJSON);
19+
String voterID = voteData.getString("voter_id");
20+
String vote = voteData.getString("vote");
21+
22+
System.err.printf("Processing vote for '%s' by '%s'\n", vote, voterID);
23+
updateVote(dbConn, voterID, vote);
24+
}
25+
} catch (SQLException e) {
26+
e.printStackTrace();
27+
System.exit(1);
28+
}
29+
}
30+
31+
static void updateVote(Connection dbConn, String voterID, String vote) throws SQLException {
32+
PreparedStatement insert = dbConn.prepareStatement(
33+
"INSERT INTO votes (id, vote) VALUES (?, ?)");
34+
insert.setString(1, voterID);
35+
insert.setString(2, vote);
36+
37+
try {
38+
insert.executeUpdate();
39+
} catch (SQLException e) {
40+
PreparedStatement update = dbConn.prepareStatement(
41+
"UPDATE votes SET vote = ? WHERE id = ?");
42+
update.setString(1, vote);
43+
update.setString(2, voterID);
44+
update.executeUpdate();
45+
}
46+
}
47+
48+
static Jedis connectToRedis(String host) {
49+
Jedis conn = new Jedis(host);
50+
51+
while (true) {
52+
try {
53+
conn.keys("*");
54+
break;
55+
} catch (JedisConnectionException e) {
56+
System.err.println("Failed to connect to redis - retrying");
57+
sleep(1000);
58+
}
59+
}
60+
61+
System.err.println("Connected to redis");
62+
return conn;
63+
}
64+
65+
static Connection connectToDB(String host) throws SQLException {
66+
Connection conn = null;
67+
68+
try {
69+
70+
Class.forName("org.postgresql.Driver");
71+
String url = "jdbc:postgresql://" + host + "/postgres";
72+
73+
while (conn == null) {
74+
try {
75+
conn = DriverManager.getConnection(url, "postgres", "");
76+
} catch (SQLException e) {
77+
System.err.println("Failed to connect to db - retrying");
78+
sleep(1000);
79+
}
80+
}
81+
82+
PreparedStatement st = conn.prepareStatement(
83+
"CREATE TABLE IF NOT EXISTS votes (id VARCHAR(255) NOT NULL UNIQUE, vote VARCHAR(255) NOT NULL)");
84+
st.executeUpdate();
85+
86+
} catch (ClassNotFoundException e) {
87+
e.printStackTrace();
88+
System.exit(1);
89+
}
90+
91+
return conn;
92+
}
93+
94+
static void sleep(long duration) {
95+
try {
96+
Thread.sleep(duration);
97+
} catch (InterruptedException e) {
98+
System.exit(1);
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)