refs #5553 New parameter to skip lock
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Juan Ferrer 2023-04-18 21:27:09 +02:00
parent 5dd7c7e3bd
commit a85135b1c8
1 changed files with 25 additions and 18 deletions

View File

@ -24,6 +24,10 @@ module.exports = Self => {
arg: 'dryRun',
type: 'boolean',
description: 'Simulate actions'
}, {
arg: 'skipLock',
type: 'boolean',
description: 'Wether to skip exclusive lock'
}
],
returns: {
@ -36,7 +40,7 @@ module.exports = Self => {
}
});
Self.scrub = async function(collection, remove, limit, dryRun) {
Self.scrub = async function(collection, remove, limit, dryRun, skipLock) {
const $ = Self.app.models;
const env = process.env.NODE_ENV;
@ -52,14 +56,19 @@ module.exports = Self => {
const container = await $.ImageContainer.container(collection);
const rootPath = container.client.root;
const tx = await Self.beginTransaction({timeout: null});
const opts = {transaction: tx};
let tx;
let opts;
const lockName = 'salix.Image.scrub';
const [row] = await Self.rawSql(
`SELECT GET_LOCK(?, 10) hasLock`, [lockName], opts);
if (!row.hasLock)
throw new UserError('Cannot obtain exclusive lock');
if (!skipLock) {
tx = await Self.beginTransaction({timeout: null});
opts = {transaction: tx};
const [row] = await Self.rawSql(
`SELECT GET_LOCK(?, 10) hasLock`, [lockName], opts);
if (!row.hasLock)
throw new UserError('Cannot obtain exclusive lock');
}
try {
const now = Date.vnNew().toJSON();
@ -95,7 +104,7 @@ module.exports = Self => {
try {
if (!dryRun) await fs.unlink(srcFile);
} catch (err) {
console.error(err);
console.error(err.message);
}
}
@ -108,15 +117,13 @@ module.exports = Self => {
return cleanCount;
} finally {
try {
await Self.rawSql(`DO RELEASE_LOCK(?)`, [lockName], opts);
} catch (err) {
console.error(err);
}
try {
await tx.rollback();
} catch (err) {
console.error(err);
if (!skipLock) {
try {
await Self.rawSql(`DO RELEASE_LOCK(?)`, [lockName], opts);
await tx.rollback();
} catch (err) {
console.error(err.message);
}
}
}
};