backuptask: make upload/download async

This commit is contained in:
Girish Ramakrishnan
2022-04-28 21:29:11 -07:00
parent 7123ec433c
commit 8ef730ad9c
3 changed files with 37 additions and 60 deletions

View File

@@ -7,23 +7,13 @@ if (process.argv[2] === '--check') {
process.exit(0);
}
const assert = require('assert'),
async = require('async'),
backuptask = require('../backuptask.js'),
const backuptask = require('../backuptask.js'),
database = require('../database.js'),
debug = require('debug')('box:backupupload'),
safe = require('safetydance'),
settings = require('../settings.js'),
v8 = require('v8');
function initialize(callback) {
assert.strictEqual(typeof callback, 'function');
async.series([
database.initialize,
settings.initCache
], callback);
}
// Main process starts here
const remotePath = process.argv[2];
const format = process.argv[3];
@@ -70,20 +60,20 @@ function dumpMemoryInfo() {
debug(`v8 heap: used ${h(hs.used_heap_size)} total: ${h(hs.total_heap_size)} max: ${h(hs.heap_size_limit)}`);
}
initialize(function (error) {
if (error) throw error;
(async function main() {
await database.initialize();
await settings.initCache();
dumpMemoryInfo();
const timerId = setInterval(dumpMemoryInfo, 30000);
backuptask.upload(remotePath, format, dataLayoutString, throttledProgressCallback(5000), function resultHandler(error) {
debug('upload completed. error: ', error);
const [uploadError] = await safe(backuptask.upload(remotePath, format, dataLayoutString, throttledProgressCallback(5000)));
debug('upload completed. error: ', uploadError);
process.send({ result: error ? error.message : '' });
clearInterval(timerId);
process.send({ result: uploadError ? uploadError.message : '' });
clearInterval(timerId);
// https://nodejs.org/api/process.html are exit codes used by node. apps.js uses the value below
// to check apptask crashes
process.exit(error ? 50 : 0);
});
});
// https://nodejs.org/api/process.html are exit codes used by node. apps.js uses the value below
// to check apptask crashes
process.exit(uploadError ? 50 : 0);
})();