|
| 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 redisConn = OpenRedisConnection("redis"); |
| 21 | + var redis = redisConn.GetDatabase(); |
| 22 | + |
| 23 | + // Keep alive is not implemented in Npgsql yet. This workaround was recommended: |
| 24 | + // https://github.com/npgsql/npgsql/issues/1214#issuecomment-235828359 |
| 25 | + var keepAliveCommand = pgsql.CreateCommand(); |
| 26 | + keepAliveCommand.CommandText = "SELECT 1"; |
| 27 | + |
| 28 | + var definition = new { vote = "", voter_id = "" }; |
| 29 | + while (true) |
| 30 | + { |
| 31 | + // Slow down to prevent CPU spike, only query each 100ms |
| 32 | + Thread.Sleep(100); |
| 33 | + |
| 34 | + // Reconnect redis if down |
| 35 | + if (redisConn == null || !redisConn.IsConnected) { |
| 36 | + Console.WriteLine("Reconnecting Redis"); |
| 37 | + redisConn = OpenRedisConnection("redis"); |
| 38 | + redis = redisConn.GetDatabase(); |
| 39 | + } |
| 40 | + string json = redis.ListLeftPopAsync("votes").Result; |
| 41 | + if (json != null) |
| 42 | + { |
| 43 | + var vote = JsonConvert.DeserializeAnonymousType(json, definition); |
| 44 | + Console.WriteLine($"Processing vote for '{vote.vote}' by '{vote.voter_id}'"); |
| 45 | + // Reconnect DB if down |
| 46 | + if (!pgsql.State.Equals(System.Data.ConnectionState.Open)) |
| 47 | + { |
| 48 | + Console.WriteLine("Reconnecting DB"); |
| 49 | + pgsql = OpenDbConnection("Server=db;Username=postgres;"); |
| 50 | + } |
| 51 | + else |
| 52 | + { // Normal +1 vote requested |
| 53 | + UpdateVote(pgsql, vote.voter_id, vote.vote); |
| 54 | + } |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + keepAliveCommand.ExecuteNonQuery(); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + catch (Exception ex) |
| 63 | + { |
| 64 | + Console.Error.WriteLine(ex.ToString()); |
| 65 | + return 1; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static NpgsqlConnection OpenDbConnection(string connectionString) |
| 70 | + { |
| 71 | + NpgsqlConnection connection; |
| 72 | + |
| 73 | + while (true) |
| 74 | + { |
| 75 | + try |
| 76 | + { |
| 77 | + connection = new NpgsqlConnection(connectionString); |
| 78 | + connection.Open(); |
| 79 | + break; |
| 80 | + } |
| 81 | + catch (SocketException) |
| 82 | + { |
| 83 | + Console.Error.WriteLine("Waiting for db"); |
| 84 | + Thread.Sleep(1000); |
| 85 | + } |
| 86 | + catch (DbException) |
| 87 | + { |
| 88 | + Console.Error.WriteLine("Waiting for db"); |
| 89 | + Thread.Sleep(1000); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + Console.Error.WriteLine("Connected to db"); |
| 94 | + |
| 95 | + var command = connection.CreateCommand(); |
| 96 | + command.CommandText = @"CREATE TABLE IF NOT EXISTS votes ( |
| 97 | + id VARCHAR(255) NOT NULL UNIQUE, |
| 98 | + vote VARCHAR(255) NOT NULL |
| 99 | + )"; |
| 100 | + command.ExecuteNonQuery(); |
| 101 | + |
| 102 | + return connection; |
| 103 | + } |
| 104 | + |
| 105 | + private static ConnectionMultiplexer OpenRedisConnection(string hostname) |
| 106 | + { |
| 107 | + // Use IP address to workaround https://github.com/StackExchange/StackExchange.Redis/issues/410 |
| 108 | + var ipAddress = GetIp(hostname); |
| 109 | + Console.WriteLine($"Found redis at {ipAddress}"); |
| 110 | + |
| 111 | + while (true) |
| 112 | + { |
| 113 | + try |
| 114 | + { |
| 115 | + Console.Error.WriteLine("Connecting to redis"); |
| 116 | + return ConnectionMultiplexer.Connect(ipAddress); |
| 117 | + } |
| 118 | + catch (RedisConnectionException) |
| 119 | + { |
| 120 | + Console.Error.WriteLine("Waiting for redis"); |
| 121 | + Thread.Sleep(1000); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private static string GetIp(string hostname) |
| 127 | + => Dns.GetHostEntryAsync(hostname) |
| 128 | + .Result |
| 129 | + .AddressList |
| 130 | + .First(a => a.AddressFamily == AddressFamily.InterNetwork) |
| 131 | + .ToString(); |
| 132 | + |
| 133 | + private static void UpdateVote(NpgsqlConnection connection, string voterId, string vote) |
| 134 | + { |
| 135 | + var command = connection.CreateCommand(); |
| 136 | + try |
| 137 | + { |
| 138 | + command.CommandText = "INSERT INTO votes (id, vote) VALUES (@id, @vote)"; |
| 139 | + command.Parameters.AddWithValue("@id", voterId); |
| 140 | + command.Parameters.AddWithValue("@vote", vote); |
| 141 | + command.ExecuteNonQuery(); |
| 142 | + } |
| 143 | + catch (DbException) |
| 144 | + { |
| 145 | + command.CommandText = "UPDATE votes SET vote = @vote WHERE id = @id"; |
| 146 | + command.ExecuteNonQuery(); |
| 147 | + } |
| 148 | + finally |
| 149 | + { |
| 150 | + command.Dispose(); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments