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