more async'ification
This commit is contained in:
+31
-34
@@ -925,43 +925,40 @@ function mailboxNameForLocation(location, manifest) {
|
||||
return 'noreply.app';
|
||||
}
|
||||
|
||||
function scheduleTask(appId, installationState, taskId, callback) {
|
||||
async function scheduleTask(appId, installationState, taskId, onFinished) {
|
||||
assert.strictEqual(typeof appId, 'string');
|
||||
assert.strictEqual(typeof installationState, 'string');
|
||||
assert.strictEqual(typeof taskId, 'string');
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
assert(typeof onFinished === 'undefined' || typeof onFinished === 'function');
|
||||
|
||||
const getBackupConfig = util.callbackify(settings.getBackupConfig);
|
||||
const backupConfig = await settings.getBackupConfig();
|
||||
|
||||
getBackupConfig(function (error, backupConfig) {
|
||||
if (error) return callback(error);
|
||||
let memoryLimit = 400;
|
||||
if (installationState === exports.ISTATE_PENDING_BACKUP || installationState === exports.ISTATE_PENDING_CLONE || installationState === exports.ISTATE_PENDING_RESTORE
|
||||
|| installationState === exports.ISTATE_PENDING_IMPORT || installationState === exports.ISTATE_PENDING_UPDATE) {
|
||||
memoryLimit = 'memoryLimit' in backupConfig ? Math.max(backupConfig.memoryLimit/1024/1024, 400) : 400;
|
||||
} else if (installationState === exports.ISTATE_PENDING_DATA_DIR_MIGRATION) {
|
||||
memoryLimit = 1024; // cp takes more memory than we think
|
||||
}
|
||||
|
||||
let memoryLimit = 400;
|
||||
if (installationState === exports.ISTATE_PENDING_BACKUP || installationState === exports.ISTATE_PENDING_CLONE || installationState === exports.ISTATE_PENDING_RESTORE
|
||||
|| installationState === exports.ISTATE_PENDING_IMPORT || installationState === exports.ISTATE_PENDING_UPDATE) {
|
||||
memoryLimit = 'memoryLimit' in backupConfig ? Math.max(backupConfig.memoryLimit/1024/1024, 400) : 400;
|
||||
} else if (installationState === exports.ISTATE_PENDING_DATA_DIR_MIGRATION) {
|
||||
memoryLimit = 1024; // cp takes more memory than we think
|
||||
const options = { timeout: 20 * 60 * 60 * 1000 /* 20 hours */, nice: 15, memoryLimit };
|
||||
|
||||
appTaskManager.scheduleTask(appId, taskId, options, async function (error) {
|
||||
debug(`scheduleTask: task ${taskId} of ${appId} completed`);
|
||||
if (error && (error.code === tasks.ECRASHED || error.code === tasks.ESTOPPED)) { // if task crashed, update the error
|
||||
debug(`Apptask crashed/stopped: ${error.message}`);
|
||||
let boxError = new BoxError(BoxError.TASK_ERROR, error.message);
|
||||
boxError.details.crashed = error.code === tasks.ECRASHED;
|
||||
boxError.details.stopped = error.code === tasks.ESTOPPED;
|
||||
// see also apptask makeTaskError
|
||||
boxError.details.taskId = taskId;
|
||||
boxError.details.installationState = installationState;
|
||||
await safe(update(appId, { installationState: exports.ISTATE_ERROR, error: boxError.toPlainObject(), taskId: null }));
|
||||
} else if (!(installationState === exports.ISTATE_PENDING_UNINSTALL && !error)) { // clear out taskId except for successful uninstall
|
||||
await safe(update(appId, { taskId: null }));
|
||||
}
|
||||
|
||||
const options = { timeout: 20 * 60 * 60 * 1000 /* 20 hours */, nice: 15, memoryLimit };
|
||||
|
||||
appTaskManager.scheduleTask(appId, taskId, options, async function (error) {
|
||||
debug(`scheduleTask: task ${taskId} of ${appId} completed`);
|
||||
if (error && (error.code === tasks.ECRASHED || error.code === tasks.ESTOPPED)) { // if task crashed, update the error
|
||||
debug(`Apptask crashed/stopped: ${error.message}`);
|
||||
let boxError = new BoxError(BoxError.TASK_ERROR, error.message);
|
||||
boxError.details.crashed = error.code === tasks.ECRASHED;
|
||||
boxError.details.stopped = error.code === tasks.ESTOPPED;
|
||||
// see also apptask makeTaskError
|
||||
boxError.details.taskId = taskId;
|
||||
boxError.details.installationState = installationState;
|
||||
await safe(update(appId, { installationState: exports.ISTATE_ERROR, error: boxError.toPlainObject(), taskId: null }));
|
||||
} else if (!(installationState === exports.ISTATE_PENDING_UNINSTALL && !error)) { // clear out taskId except for successful uninstall
|
||||
await safe(update(appId, { taskId: null }));
|
||||
}
|
||||
callback(error);
|
||||
});
|
||||
if (onFinished) onFinished(error);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -982,7 +979,7 @@ async function addTask(appId, installationState, task) {
|
||||
if (updateError && updateError.reason === BoxError.NOT_FOUND) throw new BoxError(BoxError.BAD_STATE, 'Another task is scheduled for this app'); // could be because app went away OR a taskId exists
|
||||
if (updateError) throw updateError;
|
||||
|
||||
if (scheduleNow) scheduleTask(appId, installationState, taskId, task.onFinished || NOOP_CALLBACK);
|
||||
if (scheduleNow) await safe(scheduleTask(appId, installationState, taskId, task.onFinished)); // ignore error
|
||||
|
||||
return taskId;
|
||||
}
|
||||
@@ -2230,13 +2227,13 @@ async function schedulePendingTasks() {
|
||||
|
||||
const result = await list();
|
||||
|
||||
result.forEach(function (app) {
|
||||
if (!app.taskId) return; // if not in any pending state, do nothing
|
||||
for (const app of result) {
|
||||
if (!app.taskId) continue; // if not in any pending state, do nothing
|
||||
|
||||
debug(`schedulePendingTasks: schedule task for ${app.fqdn} ${app.id}: state=${app.installationState},taskId=${app.taskId}`);
|
||||
|
||||
scheduleTask(app.id, app.installationState, app.taskId, NOOP_CALLBACK);
|
||||
});
|
||||
await safe(scheduleTask(app.id, app.installationState, app.taskId)); // ignore error
|
||||
}
|
||||
}
|
||||
|
||||
function downloadFile(app, filePath, callback) {
|
||||
|
||||
Reference in New Issue
Block a user