Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding REDIS_HOST and POSTGRES_HOST option from env #371

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions result/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ io.on('connection', function (socket) {
});
});

const dbHost = process.env.POSTGRES_HOST || 'db';
var pool = new Pool({
connectionString: 'postgres://postgres:postgres@db/postgres'
connectionString: `postgres://postgres:postgres@${dbHost}/postgres`,
ssl: {
rejectUnauthorized: false
}
});

async.retry(
{times: 1000, interval: 1000},
function(callback) {
pool.connect(function(err, client, done) {
if (err) {
console.error("Waiting for db");
console.error(`Waiting for ${dbHost}`);
}
callback(err, client);
});
Expand All @@ -35,7 +39,7 @@ async.retry(
if (err) {
return console.error("Giving up");
}
console.log("Connected to db");
console.log(`Connected to ${dbHost}`);
getVotes(client);
}
);
Expand Down
3 changes: 2 additions & 1 deletion vote/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

def get_redis():
if not hasattr(g, 'redis'):
g.redis = Redis(host="redis", db=0, socket_timeout=5)
redis_host = os.environ.get('REDIS_HOST', 'redis')
g.redis = Redis(host=redis_host, db=0, socket_timeout=5)
return g.redis

@app.route("/", methods=['POST','GET'])
Expand Down
12 changes: 7 additions & 5 deletions worker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ public static int Main(string[] args)
{
try
{
var pgsql = OpenDbConnection("Server=db;Username=postgres;Password=postgres;");
var redisConn = OpenRedisConnection("redis");
var postgresHost = Environment.GetEnvironmentVariable("POSTGRES_HOST") ?? "db";
var pgsql = OpenDbConnection($"Server={postgresHost};Username=postgres;Password=postgres;SslMode=Prefer;Trust Server Certificate=true;");
var redisHost = Environment.GetEnvironmentVariable("REDIS_HOST") ?? "redis";
var redisConn = OpenRedisConnection(redisHost);
var redis = redisConn.GetDatabase();

// Keep alive is not implemented in Npgsql yet. This workaround was recommended:
Expand All @@ -34,7 +36,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 +48,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($"Server={postgresHost};Username=postgres;Password=postgres;SslMode=Prefer;Trust Server Certificate=true;");
}
else
{ // Normal +1 vote requested
Expand Down Expand Up @@ -151,4 +153,4 @@ private static void UpdateVote(NpgsqlConnection connection, string voterId, stri
}
}
}
}
}