sftp: only rebuild when app task queue is empty

when multiple apptasks are scheduled, we end up with a sequence like this:
    - task1 finishes
    - task2 (uninstall) removes  appdata directory
    - sftp rebuild (from task1 finish)
    - task2 fails because sftp rebuild created empty appdata directory

a fix is to delay the sftp rebuild until all tasks are done. of course,
the same race is still there, if a user initiated another task immediately
but this seems unlikely. if that happens often, we can further add a sftpRebuildInProgress
flag inside apptaskmanager.
This commit is contained in:
Girish Ramakrishnan
2021-03-16 18:29:01 -07:00
parent 098da7426c
commit 4cba5ca405
3 changed files with 9 additions and 19 deletions

View File

@@ -21,24 +21,11 @@ var apps = require('./apps.js'),
volumes = require('./volumes.js'),
_ = require('underscore');
var gRebuildInProgress = false;
function rebuild(serviceConfig, options, callback) {
assert.strictEqual(typeof serviceConfig, 'object');
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof callback, 'function');
if (gRebuildInProgress) {
debug('waiting for other rebuild to finish');
return setTimeout(function () { rebuild(serviceConfig, options, callback); }, 5000);
}
gRebuildInProgress = true;
function done(error) {
gRebuildInProgress = false;
callback(error);
}
debug('rebuilding container');
const force = !!options.force;
@@ -48,7 +35,7 @@ function rebuild(serviceConfig, options, callback) {
const cloudronToken = hat(8 * 128);
apps.getAll(function (error, result) {
if (error) return done(error);
if (error) return callback(error);
let dataDirs = [];
result.forEach(function (app) {
@@ -89,7 +76,7 @@ function rebuild(serviceConfig, options, callback) {
if (!force && _.isEqual(currentDataDirs, dataDirs)) {
debug('Skipping rebuild, no changes');
return done();
return callback();
}
}
}
@@ -119,7 +106,7 @@ function rebuild(serviceConfig, options, callback) {
shell.exec.bind(null, 'stopSftp', 'docker stop sftp || true'),
shell.exec.bind(null, 'removeSftp', 'docker rm -f sftp || true'),
shell.exec.bind(null, 'startSftp', cmd)
], done);
], callback);
});
});
});