Browse Source

Fix race condition in worker

Aanand Prasad 9 years ago
parent
commit
c57e9c315a
1 changed files with 9 additions and 1 deletions
  1. 9 1
      worker/src/Worker/Program.cs

+ 9 - 1
worker/src/Worker/Program.cs

@@ -40,17 +40,25 @@ namespace Worker
 
         private static NpgsqlConnection OpenDbConnection(string connectionString)
         {
-            var connection = new NpgsqlConnection(connectionString);
+            NpgsqlConnection connection;
+
             while (true)
             {
                 try
                 {
+                    connection = new NpgsqlConnection(connectionString);
                     connection.Open();
                     break;
                 }
+                catch (SocketException)
+                {
+                    Console.Error.WriteLine("Failed to connect to db - retrying");
+                    Thread.Sleep(1000);
+                }
                 catch (DbException)
                 {
                     Console.Error.WriteLine("Failed to connect to db - retrying");
+                    Thread.Sleep(1000);
                 }
             }