make startBackupTask async

This commit is contained in:
Girish Ramakrishnan
2021-09-10 12:10:10 -07:00
parent 242fad137c
commit ae13fe60a7
4 changed files with 53 additions and 69 deletions

View File

@@ -15,7 +15,6 @@ const backups = require('../backups.js'),
fs = require('fs'),
os = require('os'),
path = require('path'),
safe = require('safetydance'),
settings = require('../settings.js'),
tasks = require('../tasks.js');
@@ -86,66 +85,55 @@ describe('backuptask', function () {
fs.rmSync(backupConfig.backupFolder, { recursive: true, force: true });
});
function createBackup(callback) {
backups.startBackupTask({ username: 'test' }, async function (error, taskId) { // this call does not wait for the backup!
if (error) return callback(error);
async function createBackup() {
const taskId = await backups.startBackupTask({ username: 'test' });
// eslint-disable-next-line no-constant-condition
while (true) {
await delay(1000);
// eslint-disable-next-line no-constant-condition
while (true) {
await delay(1000);
const p = await tasks.get(taskId);
const p = await tasks.get(taskId);
if (p.percent !== 100) continue;
if (p.error) return callback(new Error(`backup failed: ${p.error.message}`));
if (!p.result) return callback(new Error('backup has no result:' + p));
if (p.percent !== 100) continue;
if (p.error) throw new Error(`backup failed: ${p.error.message}`);
if (!p.result) throw new Error('backup has no result:' + p);
const [error, result] = await safe(backups.getByIdentifierAndStatePaged(backups.BACKUP_IDENTIFIER_BOX, backups.BACKUP_STATE_NORMAL, 1, 1));
const result = await backups.getByIdentifierAndStatePaged(backups.BACKUP_IDENTIFIER_BOX, backups.BACKUP_STATE_NORMAL, 1, 1);
if (error) return callback(error);
if (result.length !== 1) return callback(new Error('result is not of length 1'));
if (result.length !== 1) throw new Error('result is not of length 1');
// the task progress and the db entry is set in the worker. wait for 2 seconds for backup lock to get released in parent process
await delay(2000);
// the task progress and the db entry is set in the worker. wait for 2 seconds for backup lock to get released in parent process
await delay(2000);
callback(null, result[0]);
}
});
return result[0];
}
}
it('can backup', function (done) {
it('can backup', async function () {
// arch only has maria db which lacks some mysqldump options we need, this is only here to allow running the tests :-/
if (require('child_process').execSync('/usr/bin/mysqldump --version').toString().indexOf('MariaDB') !== -1) {
console.log('test skipped because of MariaDB');
return done();
return;
}
createBackup(function (error, result) {
expect(error).to.be(null);
expect(fs.statSync(path.join(backupConfig.backupFolder, 'snapshot/box.tar.gz')).nlink).to.be(2); // hard linked to a rotated backup
expect(fs.statSync(path.join(backupConfig.backupFolder, `${result.id}.tar.gz`)).nlink).to.be(2);
const result = await createBackup();
expect(fs.statSync(path.join(backupConfig.backupFolder, 'snapshot/box.tar.gz')).nlink).to.be(2); // hard linked to a rotated backup
expect(fs.statSync(path.join(backupConfig.backupFolder, `${result.id}.tar.gz`)).nlink).to.be(2);
backupInfo1 = result;
done();
});
backupInfo1 = result;
});
it('can take another backup', function (done) {
it('can take another backup', async function () {
// arch only has maria db which lacks some mysqldump options we need, this is only here to allow running the tests :-/
if (require('child_process').execSync('/usr/bin/mysqldump --version').toString().indexOf('MariaDB') !== -1) {
console.log('test skipped because of MariaDB');
return done();
return;
}
createBackup(function (error, result) {
expect(error).to.be(null);
expect(fs.statSync(path.join(backupConfig.backupFolder, 'snapshot/box.tar.gz')).nlink).to.be(2); // hard linked to a rotated backup
expect(fs.statSync(path.join(backupConfig.backupFolder, `${result.id}.tar.gz`)).nlink).to.be(2); // hard linked to new backup
expect(fs.statSync(path.join(backupConfig.backupFolder, `${backupInfo1.id}.tar.gz`)).nlink).to.be(1); // not hard linked anymore
done();
});
const result = await createBackup();
expect(fs.statSync(path.join(backupConfig.backupFolder, 'snapshot/box.tar.gz')).nlink).to.be(2); // hard linked to a rotated backup
expect(fs.statSync(path.join(backupConfig.backupFolder, `${result.id}.tar.gz`)).nlink).to.be(2); // hard linked to new backup
expect(fs.statSync(path.join(backupConfig.backupFolder, `${backupInfo1.id}.tar.gz`)).nlink).to.be(1); // not hard linked anymore
});
});
});