2.0.3 • Published 1 month ago

@prairielearn/named-locks v2.0.3

Weekly downloads
-
License
-
Repository
github
Last release
1 month ago

@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');
  },
);
2.0.3

1 month ago

2.0.2

1 month ago

2.0.1

2 months ago

2.0.0

2 months ago

1.5.11

3 months ago

1.5.10

4 months ago

1.5.9

5 months ago

1.5.5

7 months ago

1.5.4

8 months ago

1.5.3

8 months ago

1.5.2

9 months ago

1.5.1

9 months ago

1.5.0

10 months ago

1.4.0

10 months ago

1.5.8

6 months ago

1.5.7

6 months ago

1.5.6

7 months ago

1.3.3

11 months ago

1.3.2

12 months ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.0

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago