2015-10-18 08:40:24 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2020-11-25 22:16:20 -08:00
|
|
|
sync,
|
|
|
|
|
suspendJobs,
|
|
|
|
|
resumeJobs
|
2015-10-18 08:40:24 -07:00
|
|
|
};
|
|
|
|
|
|
2021-06-03 12:20:44 -07:00
|
|
|
const apps = require('./apps.js'),
|
2015-10-18 08:40:24 -07:00
|
|
|
assert = require('assert'),
|
2020-08-18 21:15:54 -07:00
|
|
|
BoxError = require('./boxerror.js'),
|
2019-07-26 10:10:14 -07:00
|
|
|
constants = require('./constants.js'),
|
2015-10-18 08:40:24 -07:00
|
|
|
CronJob = require('cron').CronJob,
|
2017-04-23 21:53:59 -07:00
|
|
|
debug = require('debug')('box:scheduler'),
|
2015-10-19 22:42:13 -07:00
|
|
|
docker = require('./docker.js'),
|
2021-08-25 19:41:46 -07:00
|
|
|
safe = require('safetydance'),
|
2015-10-18 08:40:24 -07:00
|
|
|
_ = require('underscore');
|
|
|
|
|
|
2020-09-01 12:44:48 -07:00
|
|
|
// appId -> { containerId, schedulerConfig (manifest), cronjobs }
|
2020-11-25 22:16:20 -08:00
|
|
|
let gState = { };
|
|
|
|
|
let gSuspendedAppIds = new Set(); // suspended because some apptask is running
|
2015-10-18 08:40:24 -07:00
|
|
|
|
2020-11-25 22:25:36 -08:00
|
|
|
// TODO: this should probably also stop existing jobs to completely prevent race but the code is not re-entrant
|
2020-11-25 22:16:20 -08:00
|
|
|
function suspendJobs(appId) {
|
|
|
|
|
debug(`suspendJobs: ${appId}`);
|
|
|
|
|
gSuspendedAppIds.add(appId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function resumeJobs(appId) {
|
|
|
|
|
debug(`resumeJobs: ${appId}`);
|
|
|
|
|
gSuspendedAppIds.delete(appId);
|
|
|
|
|
}
|
2015-10-18 08:40:24 -07:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
async function runTask(appId, taskName) {
|
2020-08-18 19:08:37 -07:00
|
|
|
assert.strictEqual(typeof appId, 'string');
|
|
|
|
|
assert.strictEqual(typeof taskName, 'string');
|
2015-10-20 01:00:31 -07:00
|
|
|
|
2020-08-18 19:08:37 -07:00
|
|
|
const JOB_MAX_TIME = 30 * 60 * 1000; // 30 minutes
|
|
|
|
|
const containerName = `${appId}-${taskName}`;
|
2015-10-20 10:16:59 -07:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
if (gSuspendedAppIds.has(appId)) return;
|
2020-11-25 22:16:20 -08:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
const app = await apps.get(appId);
|
|
|
|
|
if (!app) throw new BoxError(BoxError.NOT_FOUND, 'App not found');
|
2015-10-20 01:00:31 -07:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
if (app.installationState !== apps.ISTATE_INSTALLED || app.runState !== apps.RSTATE_RUNNING || app.health !== apps.HEALTH_HEALTHY) return;
|
2015-10-20 01:00:31 -07:00
|
|
|
|
2021-08-31 07:59:07 -07:00
|
|
|
const [error, data] = await safe(docker.inspect(containerName));
|
2021-08-25 19:41:46 -07:00
|
|
|
if (!error && data && data.State.Running === true) {
|
|
|
|
|
const jobStartTime = new Date(data.State.StartedAt); // iso 8601
|
|
|
|
|
if (new Date() - jobStartTime < JOB_MAX_TIME) return;
|
|
|
|
|
}
|
2015-10-20 01:00:31 -07:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
await docker.restartContainer(containerName);
|
2015-10-18 08:40:24 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
async function createJobs(app, schedulerConfig) {
|
2020-08-18 19:08:37 -07:00
|
|
|
assert.strictEqual(typeof app, 'object');
|
|
|
|
|
assert(schedulerConfig && typeof schedulerConfig === 'object');
|
2018-02-27 13:50:29 -08:00
|
|
|
|
2020-08-18 19:08:37 -07:00
|
|
|
const appId = app.id;
|
2021-08-25 19:41:46 -07:00
|
|
|
const jobs = { };
|
2020-08-18 19:08:37 -07:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
for (const taskName of Object.keys(schedulerConfig)) {
|
2020-08-18 19:08:37 -07:00
|
|
|
const task = schedulerConfig[taskName];
|
|
|
|
|
const randomSecond = Math.floor(60*Math.random()); // don't start all crons to decrease memory pressure
|
|
|
|
|
const cronTime = (constants.TEST ? '*/5 ' : `${randomSecond} `) + task.schedule; // time ticks faster in tests
|
|
|
|
|
|
|
|
|
|
const containerName = `${app.id}-${taskName}`;
|
|
|
|
|
const cmd = schedulerConfig[taskName].command;
|
|
|
|
|
|
2020-08-18 23:53:14 -07:00
|
|
|
// stopJobs only deletes jobs since previous run. This means that when box code restarts, none of the containers
|
|
|
|
|
// are removed. The deleteContainer here ensures we re-create the cron containers with the latest config
|
2021-08-25 19:41:46 -07:00
|
|
|
await safe(docker.deleteContainer(containerName)); // ignore error
|
|
|
|
|
const [error] = await safe(docker.createSubcontainer(app, containerName, [ '/bin/sh', '-c', cmd ], { } /* options */));
|
|
|
|
|
if (error && error.reason !== BoxError.ALREADY_EXISTS) continue;
|
|
|
|
|
|
|
|
|
|
debug(`createJobs: ${taskName} (${app.fqdn}) will run in container ${containerName}`);
|
|
|
|
|
|
|
|
|
|
const cronJob = new CronJob({
|
|
|
|
|
cronTime: cronTime, // at this point, the pattern has been validated
|
|
|
|
|
onTick: async () => {
|
|
|
|
|
const [error] = await safe(runTask(appId, taskName)); // put the app id in closure, so we don't use the outdated app object by mistake
|
|
|
|
|
if (error) debug(`could not run task ${taskName} : ${error.message}`);
|
|
|
|
|
},
|
|
|
|
|
start: true
|
|
|
|
|
});
|
2020-08-18 19:08:37 -07:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
jobs[taskName] = cronJob;
|
|
|
|
|
}
|
2015-10-20 01:00:31 -07:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
return jobs;
|
2015-10-20 01:00:31 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
async function stopJobs(appId, appState) {
|
2015-10-18 08:40:24 -07:00
|
|
|
assert.strictEqual(typeof appId, 'string');
|
2015-10-20 01:00:31 -07:00
|
|
|
assert.strictEqual(typeof appState, 'object');
|
2015-10-18 08:40:24 -07:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
if (!appState) return;
|
2015-10-20 01:00:31 -07:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
for (const taskName of Object.keys(appState.schedulerConfig)) {
|
2020-08-18 22:35:08 -07:00
|
|
|
if (appState.cronJobs && appState.cronJobs[taskName]) appState.cronJobs[taskName].stop();
|
2015-10-20 10:16:59 -07:00
|
|
|
|
2020-08-18 19:08:37 -07:00
|
|
|
const containerName = `${appId}-${taskName}`;
|
2021-08-25 19:41:46 -07:00
|
|
|
const [error] = await safe(docker.deleteContainer(containerName));
|
|
|
|
|
if (error) debug(`stopJobs: failed to delete task container with name ${containerName} : ${error.message}`);
|
|
|
|
|
}
|
2020-08-18 19:08:37 -07:00
|
|
|
}
|
2015-10-18 08:40:24 -07:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
async function sync() {
|
2021-06-03 12:20:44 -07:00
|
|
|
if (constants.TEST) return;
|
|
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
const allApps = await apps.list();
|
2015-10-18 08:40:24 -07:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
const allAppIds = allApps.map(function (app) { return app.id; });
|
|
|
|
|
const removedAppIds = _.difference(Object.keys(gState), allAppIds);
|
|
|
|
|
if (removedAppIds.length !== 0) debug(`sync: stopping jobs of removed apps ${JSON.stringify(removedAppIds)}`);
|
2015-10-18 08:40:24 -07:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
for (const appId of removedAppIds) {
|
|
|
|
|
debug(`sync: removing jobs of ${appId}`);
|
|
|
|
|
const [error] = await safe(stopJobs(appId, gState[appId]));
|
|
|
|
|
if (error) debug(`sync: error stopping jobs of removed app ${appId}: ${error.message}`);
|
|
|
|
|
}
|
2018-02-27 13:21:38 -08:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
gState = _.omit(gState, removedAppIds);
|
2015-10-18 08:40:24 -07:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
for (const app of allApps) {
|
|
|
|
|
const appState = gState[app.id] || null;
|
|
|
|
|
const schedulerConfig = app.manifest.addons ? app.manifest.addons.scheduler : null;
|
2015-10-18 08:40:24 -07:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
if (!appState && !schedulerConfig) continue; // nothing to do
|
|
|
|
|
if (appState && appState.cronJobs) { // we had created jobs for this app previously
|
|
|
|
|
if (_.isEqual(appState.schedulerConfig, schedulerConfig) && appState.containerId === app.containerId) continue; // nothing changed
|
|
|
|
|
}
|
2020-08-18 21:15:54 -07:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
debug(`sync: adding jobs of ${app.id} (${app.fqdn})`);
|
2015-12-23 13:23:47 -08:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
const [error] = await safe(stopJobs(app.id, appState));
|
|
|
|
|
if (error) debug(`sync: error stopping jobs of ${app.id} : ${error.message}`);
|
2015-10-20 00:02:25 -07:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
if (!schedulerConfig) { // updated app version removed scheduler addon
|
|
|
|
|
delete gState[app.id];
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2015-11-22 21:17:17 -08:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
const cronJobs = await createJobs(app, schedulerConfig); // if docker is down, the next sync() will recreate everything for this app
|
|
|
|
|
gState[app.id] = { containerId: app.containerId, schedulerConfig, cronJobs };
|
|
|
|
|
}
|
2015-10-18 08:40:24 -07:00
|
|
|
}
|