Files
cloudron-box/src/scripts/backupupload.js
2020-11-05 16:08:57 -08:00

80 lines
2.3 KiB
JavaScript
Executable File

#!/usr/bin/env node
'use strict';
if (process.argv[2] === '--check') return console.log('OK');
var assert = require('assert'),
async = require('async'),
backups = require('../backups.js'),
database = require('../database.js'),
debug = require('debug')('box:backupupload'),
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 backupId = process.argv[2];
const format = process.argv[3];
const dataLayoutString = process.argv[4];
debug(`Backing up ${dataLayoutString} to ${backupId}`);
process.on('SIGTERM', function () {
process.exit(0);
});
// this can happen when the backup task is terminated (not box code)
process.on('disconnect', function () {
debug('parent process died');
process.exit(0);
});
// 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;
};
}
// 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();
debug(`process: rss: ${mu.rss} heapTotal: ${mu.heapTotal} heapUsed: ${mu.heapUsed} external: ${mu.external}`);
debug(`v8 heap : used ${hs.used_heap_size} total: ${hs.total_heap_size} max: ${hs.heap_size_limit}`);
}
initialize(function (error) {
if (error) throw error;
dumpMemoryInfo();
const timerId = setInterval(dumpMemoryInfo, 30000);
backups.upload(backupId, format, dataLayoutString, throttledProgressCallback(5000), function resultHandler(error) {
debug('upload completed. error: ', error);
process.send({ result: error ? error.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);
});
});