1
0
mirror of https://github.com/0rangebananaspy/authelia.git synced 2024-09-14 22:47:21 +07:00
authelia/test/environment.ts
Clément Michaud c503765dd6
Implement retry mechanism for broken connections to mongo ()
Before this patch, when Authelia started, if Mongo was not
up and running, Authelia failed to connect and never retried.
Now, everytime Authelia faces a broken connection, it tries
to reconnect during the next operation.
2018-08-19 16:51:36 +02:00

59 lines
1.9 KiB
TypeScript

const { exec } = require('child_process');
import Bluebird = require("bluebird");
function docker_compose(includes: string[]) {
const compose_args = includes.map((dc: string) => `-f ${dc}`).join(' ');
return `docker-compose ${compose_args}`;
}
export class Environment {
private includes: string[];
constructor(includes: string[]) {
this.includes = includes;
}
private runCommand(command: string, timeout?: number): Bluebird<void> {
return new Bluebird<void>(function(resolve, reject) {
console.log('[ENVIRONMENT] Running: %s', command);
exec(command, function(err, stdout, stderr) {
if(err) {
reject(err);
return;
}
if(!timeout) resolve();
else setTimeout(resolve, timeout);
});
});
}
setup(timeout?: number): Bluebird<void> {
const command = docker_compose(this.includes) + ' up -d'
console.log('[ENVIRONMENT] Starting up...');
return this.runCommand(command, timeout);
}
cleanup(): Bluebird<void> {
const command = docker_compose(this.includes) + ' down'
console.log('[ENVIRONMENT] Cleaning up...');
return this.runCommand(command);
}
stop_service(serviceName: string): Bluebird<void> {
const command = docker_compose(this.includes) + ' stop ' + serviceName;
console.log('[ENVIRONMENT] Stopping service %s...', serviceName);
return this.runCommand(command);
}
start_service(serviceName: string): Bluebird<void> {
const command = docker_compose(this.includes) + ' start ' + serviceName;
console.log('[ENVIRONMENT] Starting service %s...', serviceName);
return this.runCommand(command);
}
restart_service(serviceName: string, timeout?: number): Bluebird<void> {
const command = docker_compose(this.includes) + ' restart ' + serviceName;
console.log('[ENVIRONMENT] Restarting service %s...', serviceName);
return this.runCommand(command, timeout);
}
}