Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions db/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ async function getCachePath(pgClient, idBranch) {
throw new Error('idBranch non valide');
}

async function lockSharedBranch(pgClient, idBranch) {
debug(` ~~lockSharedBranch (idBranch: ${idBranch})`);
await pgClient.query(
'SELECT pg_advisory_xact_lock_shared($1)',
[idBranch],
);
debug(` ~~lockSharedBranch (idBranch: ${idBranch}) done`);
}

async function lockBranch(pgClient, idBranch) {
debug(` ~~lockBranch (idBranch: ${idBranch})`);
await pgClient.query(
'SELECT pg_advisory_xact_lock($1)',
[idBranch],
);
debug(` ~~lockBranch (idBranch: ${idBranch}) done`);
}

async function getBranches(pgClient, idCache) {
debug(` ~~getBranches (idCache: ${idCache})`);
let results;
Expand Down Expand Up @@ -546,6 +564,8 @@ module.exports = {
deleteCache,
insertListOpi,
getCachePath,
lockSharedBranch,
lockBranch,
getBranches,
// getIdCacheFromPath,
insertBranch,
Expand Down
38 changes: 38 additions & 0 deletions middlewares/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,48 @@ async function getCachePath(req, _res, next) {
next();
}

async function lockShared(req, _res, next) {
debug('>>GET lockShared');
if (req.error) {
next();
return;
}
const params = matchedData(req);
const { idBranch } = params;
try {
await db.lockSharedBranch(req.client, idBranch);
} catch (error) {
debug(error);
req.error = error;
}
debug(' next>>');
next();
}

async function lock(req, _res, next) {
debug('>>GET lock');
if (req.error) {
next();
return;
}
const params = matchedData(req);
const { idBranch } = params;
try {
await db.lockBranch(req.client, idBranch);
} catch (error) {
debug(error);
req.error = error;
}
debug(' next>>');
next();
}

module.exports = {
getBranches,
postBranch,
deleteBranch,
rebase,
getCachePath,
lockShared,
lock,
};
Loading