Files
cloudron-box/src/apptaskmanager.js

88 lines
2.8 KiB
JavaScript
Raw Normal View History

2019-08-28 15:00:55 -07:00
'use strict';
exports = module.exports = {
scheduleTask: scheduleTask
};
2019-09-24 19:50:24 -07:00
let assert = require('assert'),
BoxError = require('./boxerror.js'),
2019-09-19 16:40:31 -07:00
debug = require('debug')('box:apptaskmanager'),
2019-08-28 15:00:55 -07:00
fs = require('fs'),
locker = require('./locker.js'),
safe = require('safetydance'),
path = require('path'),
paths = require('./paths.js'),
tasks = require('./tasks.js');
let gActiveTasks = { }; // indexed by app id
let gPendingTasks = [ ];
2019-09-24 20:29:01 -07:00
let gInitialized = false;
2019-08-28 15:00:55 -07:00
const TASK_CONCURRENCY = 3;
const NOOP_CALLBACK = function (error) { if (error) debug(error); };
function waitText(lockOperation) {
if (lockOperation === locker.OP_BOX_UPDATE) return 'Waiting for Cloudron to finish updating. See the Settings view';
if (lockOperation === locker.OP_PLATFORM_START) return 'Waiting for Cloudron to initialize';
if (lockOperation === locker.OP_FULL_BACKUP) return 'Wait for Cloudron to finish backup. See the Backups view';
return ''; // cannot happen
}
2019-09-24 20:29:01 -07:00
function initializeSync() {
gInitialized = true;
locker.on('unlocked', startNextTask);
}
2019-08-28 15:00:55 -07:00
// callback is called when task is finished
function scheduleTask(appId, taskId, callback) {
assert.strictEqual(typeof appId, 'string');
assert.strictEqual(typeof taskId, 'string');
assert.strictEqual(typeof callback, 'function');
2019-09-24 20:29:01 -07:00
if (!gInitialized) initializeSync();
2019-08-28 15:00:55 -07:00
if (appId in gActiveTasks) {
return callback(new BoxError(BoxError.CONFLICT, `Task for %s is already active: ${appId}`));
2019-08-28 15:00:55 -07:00
}
if (Object.keys(gActiveTasks).length >= TASK_CONCURRENCY) {
debug(`Reached concurrency limit, queueing task id ${taskId}`);
2019-12-18 09:33:44 -08:00
tasks.update(taskId, { percent: 1, message: 'Waiting for other app tasks to complete' }, NOOP_CALLBACK);
2019-08-28 15:00:55 -07:00
gPendingTasks.push({ appId, taskId, callback });
return;
}
var lockError = locker.recursiveLock(locker.OP_APPTASK);
if (lockError) {
debug(`Could not get lock. ${lockError.message}, queueing task id ${taskId}`);
2019-12-18 09:33:44 -08:00
tasks.update(taskId, { percent: 1, message: waitText(lockError.operation) }, NOOP_CALLBACK);
2019-08-28 15:00:55 -07:00
gPendingTasks.push({ appId, taskId, callback });
return;
}
gActiveTasks[appId] = {};
const logFile = path.join(paths.LOG_DIR, appId, 'apptask.log');
if (!fs.existsSync(path.dirname(logFile))) safe.fs.mkdirSync(path.dirname(logFile)); // ensure directory
2019-10-14 14:16:15 -07:00
tasks.startTask(taskId, { logFile, timeout: 20 * 60 * 60 * 1000 /* 20 hours */ }, function (error, result) {
2019-08-28 15:00:55 -07:00
callback(error, result);
delete gActiveTasks[appId];
locker.unlock(locker.OP_APPTASK); // unlock event will trigger next task
});
}
function startNextTask() {
if (gPendingTasks.length === 0) return;
assert(Object.keys(gActiveTasks).length < TASK_CONCURRENCY);
const t = gPendingTasks.shift();
scheduleTask(t.appId, t.taskId, t.callback);
}