From c57e9c315a76144552cba13ca200b89026b39141 Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Sat, 18 Jun 2016 14:16:32 -0700 Subject: [PATCH] Fix race condition in worker --- worker/src/Worker/Program.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/worker/src/Worker/Program.cs b/worker/src/Worker/Program.cs index fb6d922..83ea264 100644 --- a/worker/src/Worker/Program.cs +++ b/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); } }