Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update worker to use environment variables
  • Loading branch information
mbruntink committed Aug 16, 2020
1 parent 245c8f3 commit 143275a
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions worker/src/Worker/Program.cs
Expand Up @@ -16,8 +16,16 @@ public static int Main(string[] args)
{
try
{
var pgsql = OpenDbConnection("Server=db;Username=postgres;Password=postgres;");
var redisConn = OpenRedisConnection("redis");
var sdEndpoint = Environment.GetEnvironmentVariable("COPILOT_SERVICE_DISCOVERY_ENDPOINT");

var pgUser = Environment.GetEnvironmentVariable("POSTGRES_USER");
var pgPassword = Environment.GetEnvironmentVariable("POSTGRES_PASSWORD");

var pgConn = "Server=db." + sdEndpoint + ";Username=" + pgUser + ";Password=" + pgPassword + ";";
var redisHost = "redis." + sdEndpoint;

var pgsql = OpenDbConnection(pgConn);
var redisConn = OpenRedisConnection(redisHost);
var redis = redisConn.GetDatabase();

// Keep alive is not implemented in Npgsql yet. This workaround was recommended:
Expand All @@ -34,7 +42,7 @@ public static int Main(string[] args)
// Reconnect redis if down
if (redisConn == null || !redisConn.IsConnected) {
Console.WriteLine("Reconnecting Redis");
redisConn = OpenRedisConnection("redis");
redisConn = OpenRedisConnection(redisHost);
redis = redisConn.GetDatabase();
}
string json = redis.ListLeftPopAsync("votes").Result;
Expand All @@ -46,7 +54,7 @@ public static int Main(string[] args)
if (!pgsql.State.Equals(System.Data.ConnectionState.Open))
{
Console.WriteLine("Reconnecting DB");
pgsql = OpenDbConnection("Server=db;Username=postgres;Password=postgres;");
pgsql = OpenDbConnection(pgConn);
}
else
{ // Normal +1 vote requested
Expand Down

0 comments on commit 143275a

Please sign in to comment.