Skip to content

Commit e548968

Browse files
author
Nate McMaster
committed
Recreate worker as a .NET Core app
1 parent 6562cee commit e548968

File tree

6 files changed

+149
-200
lines changed

6 files changed

+149
-200
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
*.pyc
2+
project.lock.json
3+
bin/
4+
obj/

worker/Dockerfile

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

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
3+
WORKDIR /app
84

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

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

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"]
11+
CMD dotnet Worker.dll

worker/pom.xml

Lines changed: 0 additions & 84 deletions
This file was deleted.

worker/src/Worker/Program.cs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using System;
2+
using System.Data.Common;
3+
using System.Linq;
4+
using System.Net;
5+
using System.Net.Sockets;
6+
using System.Threading;
7+
using Newtonsoft.Json;
8+
using Npgsql;
9+
using StackExchange.Redis;
10+
11+
namespace Worker
12+
{
13+
public class Program
14+
{
15+
public static int Main(string[] args)
16+
{
17+
try
18+
{
19+
var pgsql = OpenDbConnection("Server=db;Username=postgres;");
20+
var redis = OpenRedisConnection("redis").GetDatabase();
21+
22+
var definition = new { vote = "", voter_id = "" };
23+
while (true)
24+
{
25+
string json = redis.ListLeftPopAsync("votes").Result;
26+
if (json != null)
27+
{
28+
var vote = JsonConvert.DeserializeAnonymousType(json, definition);
29+
Console.WriteLine($"Processing vote for '{vote.vote}' by '{vote.voter_id}'");
30+
UpdateVote(pgsql, vote.voter_id, vote.vote);
31+
}
32+
}
33+
}
34+
catch (Exception ex)
35+
{
36+
Console.Error.WriteLine(ex.ToString());
37+
return 1;
38+
}
39+
}
40+
41+
private static NpgsqlConnection OpenDbConnection(string connectionString)
42+
{
43+
var connection = new NpgsqlConnection(connectionString);
44+
while (true)
45+
{
46+
try
47+
{
48+
connection.Open();
49+
break;
50+
}
51+
catch (DbException)
52+
{
53+
Console.Error.WriteLine("Failed to connect to db - retrying");
54+
}
55+
}
56+
57+
var command = connection.CreateCommand();
58+
command.CommandText = @"CREATE TABLE IF NOT EXISTS votes (
59+
id VARCHAR(255) NOT NULL UNIQUE,
60+
vote VARCHAR(255) NOT NULL
61+
)";
62+
command.ExecuteNonQuery();
63+
64+
return connection;
65+
}
66+
67+
private static ConnectionMultiplexer OpenRedisConnection(string hostname)
68+
{
69+
// Use IP address to workaround hhttps://github.com/StackExchange/StackExchange.Redis/issues/410
70+
var ipAddress = GetIp(hostname);
71+
Console.WriteLine($"Found redis at {ipAddress}");
72+
73+
while (true)
74+
{
75+
try
76+
{
77+
return ConnectionMultiplexer.Connect(ipAddress);
78+
}
79+
catch (RedisConnectionException)
80+
{
81+
Console.Error.WriteLine("Failed to connect to redis - retrying");
82+
Thread.Sleep(1000);
83+
}
84+
}
85+
}
86+
87+
private static string GetIp(string hostname)
88+
=> Dns.GetHostEntryAsync(hostname)
89+
.Result
90+
.AddressList
91+
.First(a => a.AddressFamily == AddressFamily.InterNetwork)
92+
.ToString();
93+
94+
private static void UpdateVote(NpgsqlConnection connection, string voterId, string vote)
95+
{
96+
var command = connection.CreateCommand();
97+
try
98+
{
99+
command.CommandText = "INSERT INTO votes (id, vote) VALUES (@id, @vote)";
100+
command.Parameters.AddWithValue("@id", voterId);
101+
command.Parameters.AddWithValue("@vote", vote);
102+
command.ExecuteNonQuery();
103+
}
104+
catch (DbException)
105+
{
106+
command.CommandText = "UPDATE votes SET vote = @vote WHERE id = @id";
107+
command.ExecuteNonQuery();
108+
}
109+
finally
110+
{
111+
command.Dispose();
112+
}
113+
}
114+
}
115+
}

worker/src/Worker/project.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "Worker",
3+
"buildOptions": {
4+
"emitEntryPoint": true,
5+
"warningsAsErrors": true
6+
},
7+
"dependencies": {
8+
"StackExchange.Redis": "1.1.604-alpha",
9+
"Npgsql": "3.1.3",
10+
"Newtonsoft.Json": "9.0.1-beta1",
11+
"Microsoft.NETCore.App": {
12+
"type": "platform",
13+
"version": "1.0.0-rc2-3002702"
14+
}
15+
},
16+
"frameworks": {
17+
"netcoreapp1.0": { }
18+
},
19+
"runtimeOptions": {
20+
"configProperties": {
21+
"System.GC.Server": true
22+
}
23+
}
24+
}

worker/src/main/java/worker/Worker.java

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)