Skip to content
Closed
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ To remove them, run:
kubectl delete -f k8s-specifications/
```

## Configuration

The services find each other by the host names `redis` and `db`, which is what Docker Compose, Swarm and the Kubernetes specifications provide. To run them somewhere else (for example each one on its own server), set these environment variables. All of them are optional and the defaults below match the Compose setup, so nothing changes if you do not set them.

| Service | Variable | Default |
|---------|----------|---------|
| vote | `REDIS_HOST` | `redis` |
| worker | `REDIS_HOST` | `redis` |
| worker, result | `POSTGRES_HOST` | `db` |
| worker, result | `POSTGRES_USER` | `postgres` |
| worker, result | `POSTGRES_PASSWORD` | `postgres` |
| worker, result | `POSTGRES_DB` | `postgres` |

## Architecture

![Architecture diagram](architecture.excalidraw.png)
Expand Down
5 changes: 4 additions & 1 deletion result/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ io.on('connection', function (socket) {
});

var pool = new Pool({
connectionString: 'postgres://postgres:postgres@db/postgres'
host: process.env.POSTGRES_HOST || 'db',
user: process.env.POSTGRES_USER || 'postgres',
password: process.env.POSTGRES_PASSWORD || 'postgres',
database: process.env.POSTGRES_DB || 'postgres'
});

async.retry(
Expand Down
5 changes: 3 additions & 2 deletions vote/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

option_a = os.getenv('OPTION_A', "Cats")
option_b = os.getenv('OPTION_B', "Dogs")
redis_host = os.getenv('REDIS_HOST', 'redis')
hostname = socket.gethostname()

app = Flask(__name__)
Expand All @@ -18,14 +19,14 @@

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

@app.route("/", methods=['POST','GET'])
def hello():
voter_id = request.cookies.get('voter_id')
if not voter_id:
voter_id = hex(random.getrandbits(64))[2:-1]
voter_id = hex(random.getrandbits(64))[2:]

vote = None

Expand Down
17 changes: 13 additions & 4 deletions worker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ public static int Main(string[] args)
{
try
{
var pgsql = OpenDbConnection("Server=db;Username=postgres;Password=postgres;");
var redisConn = OpenRedisConnection("redis");
var redisHost = Environment.GetEnvironmentVariable("REDIS_HOST") ?? "redis";
var connectionString = new NpgsqlConnectionStringBuilder
{
Host = Environment.GetEnvironmentVariable("POSTGRES_HOST") ?? "db",
Username = Environment.GetEnvironmentVariable("POSTGRES_USER") ?? "postgres",
Password = Environment.GetEnvironmentVariable("POSTGRES_PASSWORD") ?? "postgres",
Database = Environment.GetEnvironmentVariable("POSTGRES_DB") ?? "postgres"
}.ConnectionString;

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

// Keep alive is not implemented in Npgsql yet. This workaround was recommended:
Expand All @@ -34,7 +43,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 +55,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(connectionString);
}
else
{ // Normal +1 vote requested
Expand Down