services: simplify startup logic

This commit is contained in:
Girish Ramakrishnan
2021-09-26 22:48:14 -07:00
parent 0415262305
commit 7ea9252059
3 changed files with 40 additions and 49 deletions
+27 -35
View File
@@ -500,11 +500,9 @@ async function rebuildService(id, auditSource) {
// this attempts to recreate the service docker container if they don't exist but platform infra version is unchanged
// passing an infra version of 'none' will not attempt to purge existing data
const serviceConfig = await getServiceConfig(id);
switch (id) {
case 'turn':
await startTurn({ version: 'none' }, serviceConfig);
await startTurn({ version: 'none' });
break;
case 'mongodb':
await startMongodb({ version: 'none' });
@@ -516,10 +514,10 @@ async function rebuildService(id, auditSource) {
await startMysql({ version: 'none' });
break;
case 'sftp':
await sftp.rebuild(serviceConfig, { /* options */ });
await sftp.start({ version: 'none' });
break;
case 'graphite':
await startGraphite({ version: 'none' }, serviceConfig);
await startGraphite({ version: 'none' });
break;
default:
// nothing to rebuild for now.
@@ -781,51 +779,45 @@ async function applyServiceConfig(id, serviceConfig) {
async function startServices(existingInfra) {
assert.strictEqual(typeof existingInfra, 'object');
const servicesConfig = await settings.getServicesConfig();
let startFuncs = [ ];
const startFuncs = [];
// always start addons on any infra change, regardless of minor or major update
if (existingInfra.version !== infra.version) {
debug(`startServices: ${existingInfra.version} -> ${infra.version}. starting all services`);
startFuncs.push(
mail.startMail, // start this first to reduce email downtime
startTurn.bind(null, existingInfra, servicesConfig['turn'] || {}),
startMysql.bind(null, existingInfra),
startPostgresql.bind(null, existingInfra),
startMongodb.bind(null, existingInfra),
startRedis.bind(null, existingInfra),
startGraphite.bind(null, existingInfra, servicesConfig['graphite'] || {}),
sftp.start.bind(null, existingInfra, servicesConfig['sftp'] || {}),
startTurn,
startMysql,
startPostgresql,
startMongodb,
startRedis,
startGraphite,
sftp.start,
);
} else {
assert.strictEqual(typeof existingInfra.images, 'object');
if (infra.images.mail.tag !== existingInfra.images.mail.tag) startFuncs.push(mail.startMail); // start this first to reduce email downtime
if (infra.images.turn.tag !== existingInfra.images.turn.tag) startFuncs.push(startTurn.bind(null, existingInfra, servicesConfig['turn'] || {}));
if (infra.images.mysql.tag !== existingInfra.images.mysql.tag) startFuncs.push(startMysql.bind(null, existingInfra));
if (infra.images.postgresql.tag !== existingInfra.images.postgresql.tag) startFuncs.push(startPostgresql.bind(null, existingInfra));
if (infra.images.mongodb.tag !== existingInfra.images.mongodb.tag) startFuncs.push(startMongodb.bind(null, existingInfra));
if (infra.images.redis.tag !== existingInfra.images.redis.tag) startFuncs.push(startRedis.bind(null, existingInfra));
if (infra.images.graphite.tag !== existingInfra.images.graphite.tag) startFuncs.push(startGraphite.bind(null, existingInfra, servicesConfig['graphite'] || {}));
if (infra.images.sftp.tag !== existingInfra.images.sftp.tag) startFuncs.push(sftp.start.bind(null, existingInfra, servicesConfig['sftp'] || {}));
if (infra.images.turn.tag !== existingInfra.images.turn.tag) startFuncs.push(startTurn);
if (infra.images.mysql.tag !== existingInfra.images.mysql.tag) startFuncs.push(startMysql);
if (infra.images.postgresql.tag !== existingInfra.images.postgresql.tag) startFuncs.push(startPostgresql);
if (infra.images.mongodb.tag !== existingInfra.images.mongodb.tag) startFuncs.push(startMongodb);
if (infra.images.redis.tag !== existingInfra.images.redis.tag) startFuncs.push(startRedis);
if (infra.images.graphite.tag !== existingInfra.images.graphite.tag) startFuncs.push(startGraphite);
if (infra.images.sftp.tag !== existingInfra.images.sftp.tag) startFuncs.push(sftp.start);
debug('startServices: existing infra. incremental service create %j', startFuncs.map(function (f) { return f.name; }));
}
for (const func of startFuncs) {
await func();
await func(existingInfra);
}
// we always start db containers with unlimited memory. we then scale them down per configuration
let updateFuncs = [
applyServiceConfig.bind(null, 'mysql', servicesConfig['mysql'] || {}),
applyServiceConfig.bind(null, 'postgresql', servicesConfig['postgresql'] || {}),
applyServiceConfig.bind(null, 'mongodb', servicesConfig['mongodb'] || {}),
];
for (const updateFunc of updateFuncs) {
safe(updateFunc()); // no waiting. and it's ok if applying service configs fails
const servicesConfig = await settings.getServicesConfig();
for (const id of [ 'mysql', 'postgresql', 'mongodb' ]) {
const serviceConfig = servicesConfig[id] || {};
safe(applyServiceConfig(id, serviceConfig), { debug }); // no waiting. and it's ok if applying service configs fails
}
}
@@ -914,10 +906,10 @@ async function setupTurn(app, options) {
await addonConfigs.set(app.id, 'turn', env);
}
async function startTurn(existingInfra, serviceConfig) {
async function startTurn(existingInfra) {
assert.strictEqual(typeof existingInfra, 'object');
assert.strictEqual(typeof serviceConfig, 'object');
const serviceConfig = await getServiceConfig('turn');
const tag = infra.images.turn.tag;
const memoryLimit = serviceConfig.memoryLimit || SERVICES['turn'].defaultMemoryLimit;
const memory = system.getMemoryAllocation(memoryLimit);
@@ -1631,10 +1623,10 @@ async function restoreMongoDb(app, options) {
});
}
async function startGraphite(existingInfra, serviceConfig) {
async function startGraphite(existingInfra) {
assert.strictEqual(typeof existingInfra, 'object');
assert.strictEqual(typeof serviceConfig, 'object');
const serviceConfig = await getServiceConfig('graphite');
const tag = infra.images.graphite.tag;
const memoryLimit = serviceConfig.memoryLimit || 256 * 1024 * 1024;
const memory = system.getMemoryAllocation(memoryLimit);