2020-11-05 16:07:28 -08:00
|
|
|
#!/usr/bin/env node
|
2017-09-19 08:19:01 -07:00
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2022-04-04 14:13:27 -07:00
|
|
|
if (process.argv[2] === '--check') {
|
|
|
|
|
console.log('OK');
|
|
|
|
|
process.exit(0);
|
|
|
|
|
}
|
2017-09-19 08:19:01 -07:00
|
|
|
|
2022-04-28 21:29:11 -07:00
|
|
|
const backuptask = require('../backuptask.js'),
|
2018-11-30 09:42:15 -08:00
|
|
|
database = require('../database.js'),
|
2019-07-26 17:11:33 -07:00
|
|
|
debug = require('debug')('box:backupupload'),
|
2022-04-28 21:29:11 -07:00
|
|
|
safe = require('safetydance'),
|
2020-11-05 14:30:45 -08:00
|
|
|
settings = require('../settings.js'),
|
|
|
|
|
v8 = require('v8');
|
2017-09-19 08:19:01 -07:00
|
|
|
|
|
|
|
|
// Main process starts here
|
2022-04-04 14:13:27 -07:00
|
|
|
const remotePath = process.argv[2];
|
2018-12-20 14:33:29 -08:00
|
|
|
const format = process.argv[3];
|
2019-01-18 14:50:04 -08:00
|
|
|
const dataLayoutString = process.argv[4];
|
2017-09-19 08:19:01 -07:00
|
|
|
|
2022-04-04 14:13:27 -07:00
|
|
|
debug(`Backing up ${dataLayoutString} to ${remotePath}`);
|
2017-09-19 08:19:01 -07:00
|
|
|
|
|
|
|
|
process.on('SIGTERM', function () {
|
|
|
|
|
process.exit(0);
|
|
|
|
|
});
|
|
|
|
|
|
2018-11-26 19:11:30 -08:00
|
|
|
// this can happen when the backup task is terminated (not box code)
|
|
|
|
|
process.on('disconnect', function () {
|
|
|
|
|
debug('parent process died');
|
|
|
|
|
process.exit(0);
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-03 11:43:12 -07:00
|
|
|
// send progress every n seconds
|
|
|
|
|
function throttledProgressCallback(msecs) {
|
|
|
|
|
let lastProgress = null;
|
|
|
|
|
|
|
|
|
|
return function (progress) {
|
|
|
|
|
let now = Date.now();
|
|
|
|
|
if (lastProgress && ((now - lastProgress) < msecs)) return;
|
|
|
|
|
process.send(progress);
|
|
|
|
|
lastProgress = now;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 14:30:45 -08:00
|
|
|
// https://github.com/josefzamrzla/gc-heap-stats#readme
|
|
|
|
|
// https://stackoverflow.com/questions/41541843/nodejs-v8-getheapstatistics-method
|
|
|
|
|
function dumpMemoryInfo() {
|
|
|
|
|
const mu = process.memoryUsage();
|
|
|
|
|
const hs = v8.getHeapStatistics();
|
|
|
|
|
|
2021-11-02 17:51:27 -07:00
|
|
|
function h(bytes) { // human readable
|
|
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(1024)),
|
|
|
|
|
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
|
|
|
|
2021-11-02 18:07:45 -07:00
|
|
|
return (bytes / Math.pow(1024, i)).toFixed(2) * 1 + '' + sizes[i];
|
2021-11-02 17:51:27 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-01 20:15:30 +02:00
|
|
|
debug(`process: rss=${h(mu.rss)} heapUsed=${h(mu.heapUsed)} heapTotal=${h(mu.heapTotal)} external=${h(mu.external)}`
|
|
|
|
|
+ ` v8 heap: used=${h(hs.used_heap_size)} total=${h(hs.total_heap_size)} max=${h(hs.heap_size_limit)}`);
|
2020-11-05 14:30:45 -08:00
|
|
|
}
|
|
|
|
|
|
2022-04-28 21:29:11 -07:00
|
|
|
(async function main() {
|
|
|
|
|
await database.initialize();
|
|
|
|
|
await settings.initCache();
|
2017-09-19 08:19:01 -07:00
|
|
|
|
2020-11-05 14:30:45 -08:00
|
|
|
dumpMemoryInfo();
|
2022-10-01 20:15:30 +02:00
|
|
|
const timerId = setInterval(dumpMemoryInfo, 180 * 1000);
|
2020-11-05 14:30:45 -08:00
|
|
|
|
2022-04-28 21:29:11 -07:00
|
|
|
const [uploadError] = await safe(backuptask.upload(remotePath, format, dataLayoutString, throttledProgressCallback(5000)));
|
|
|
|
|
debug('upload completed. error: ', uploadError);
|
2017-09-19 08:19:01 -07:00
|
|
|
|
2022-04-28 21:29:11 -07:00
|
|
|
process.send({ result: uploadError ? uploadError.message : '' });
|
|
|
|
|
clearInterval(timerId);
|
2017-09-19 08:19:01 -07:00
|
|
|
|
2022-04-28 21:29:11 -07:00
|
|
|
// 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);
|
|
|
|
|
})();
|