@prairielearn/named-locks v2.0.3
@prairielearn/named-locks
Uses Postgres row-level locks to grant exclusive access to resources.
Usage
You must first call init
with Postgres connection details and an idle error handler:
import { init } from '@prairielearn/named-locks';
await init(
{
user: 'postgres',
// ... other Postgres config
},
(err) => {
throw err;
},
);
You can then obtain a lock and do work while it is held. The lock will be automatically released once the provided function either resolves or rejects.
import { doWithLock } from '@prairielearn/named-locks';
await doWithLock('name', {}, async () => {
console.log('Doing some work');
});
Optionally, you may configure the lock to automatically "renew" itself by periodically making no-op queries in the background. This is useful for operations that may take longer than the configured Postgres idle session timeout.
import { doWithLock } from '@prairielearn/named-locks';
await doWithLock(
'name',
{
autoRenew: true,
},
async () => {
console.log('Doing some work');
},
);
1 month ago
1 month ago
2 months ago
2 months ago
3 months ago
4 months ago
5 months ago
7 months ago
8 months ago
8 months ago
9 months ago
9 months ago
10 months ago
10 months ago
6 months ago
6 months ago
7 months ago
11 months ago
12 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago