diff --git a/CHANGES b/CHANGES index ee9f495ec..504750c1d 100644 --- a/CHANGES +++ b/CHANGES @@ -2130,3 +2130,5 @@ * filemanager: Add new file creation action and collapse new and upload actions * rsync: add warning to remove lifecycle rules * Add volume management +* backups: adjust node's heap size based on memory limit + diff --git a/src/backups.js b/src/backups.js index df894b919..9369ff7d1 100644 --- a/src/backups.js +++ b/src/backups.js @@ -851,15 +851,23 @@ function runBackupUpload(uploadConfig, progressCallback, callback) { assert.strictEqual(typeof progressCallback, 'function'); assert.strictEqual(typeof callback, 'function'); - const { backupId, format, dataLayout, progressTag } = uploadConfig; + const { backupId, backupConfig, dataLayout, progressTag } = uploadConfig; assert.strictEqual(typeof backupId, 'string'); - assert.strictEqual(typeof format, 'string'); + assert.strictEqual(typeof backupConfig, 'object'); assert.strictEqual(typeof progressTag, 'string'); assert(dataLayout instanceof DataLayout, 'dataLayout must be a DataLayout'); let result = ''; // the script communicates error result as a string - shell.sudo(`backup-${backupId}`, [ BACKUP_UPLOAD_CMD, backupId, format, dataLayout.toString() ], { preserveEnv: true, ipc: true }, function (error) { + // https://stackoverflow.com/questions/48387040/node-js-recommended-max-old-space-size + const envCopy = Object.assign({}, process.env); + if (backupConfig.memoryLimit && backupConfig.memoryLimit >= 2*1024*1024*1024) { + const heapSize = Math.min((backupConfig.memoryLimit/1024/1024) - 256, 8192); + debug(`runBackupUpload: adjusting heap size to ${heapSize}M`); + envCopy.NODE_OPTIONS = `--max-old-space-size=${heapSize}`; + } + + shell.sudo(`backup-${backupId}`, [ BACKUP_UPLOAD_CMD, backupId, backupConfig.format, dataLayout.toString() ], { env: envCopy, ipc: true }, function (error) { if (error && (error.code === null /* signal */ || (error.code !== 0 && error.code !== 50))) { // backuptask crashed return callback(new BoxError(BoxError.INTERNAL_ERROR, 'Backuptask crashed')); } else if (error && error.code === 50) { // exited with error @@ -928,7 +936,7 @@ function uploadBoxSnapshot(backupConfig, progressCallback, callback) { const uploadConfig = { backupId: 'snapshot/box', - format: backupConfig.format, + backupConfig, dataLayout: new DataLayout(boxDataDir, []), progressTag: 'box' }; @@ -1115,7 +1123,7 @@ function uploadAppSnapshot(backupConfig, app, progressCallback, callback) { const uploadConfig = { backupId, - format: backupConfig.format, + backupConfig, dataLayout, progressTag: app.fqdn };