Skip to content

Commit

Permalink
Add logging to withFileLock (#273)
Browse files Browse the repository at this point in the history
1. On entrance
2a. On detecting a lock file
2b. On not detecting a lock file

Co-authored-by: Andrew Branch <[email protected]>
  • Loading branch information
sandersn and andrewbranch authored Jun 3, 2021
1 parent 3bc237f commit 1aa87f4
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/publisher/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ export default function main() {
type LockFileResult = { triggered: true } | { triggered: false; timestamp: string };

export async function withFileLock(lockFilePath: string, cb: () => Promise<void>): Promise<LockFileResult> {
console.log("Checking for lock file...");
if (await pathExists(lockFilePath)) {
const lastRunStartTimestamp = (await tryReadJson(lockFilePath, isLockfileFormat))?.timestamp || currentTimeStamp();
const elapsedSeconds = (Date.now() - Date.parse(lastRunStartTimestamp)) / 1000;
if (elapsedSeconds < getFunctionTimeoutSeconds()) {
console.log("Lock file exists; new run not triggered.");
return { triggered: false, timestamp: lastRunStartTimestamp };
}
}

console.log("Lock file does not exist; writing lock file and running.");
await writeFile(lockFilePath, JSON.stringify({ timestamp: currentTimeStamp() }));
cb().then(
() => remove(lockFilePath),
Expand Down

0 comments on commit 1aa87f4

Please sign in to comment.