@tslock/elasticsearch
v2.0.1
Published
TSLock provider for Elasticsearch
Readme
@tslock/elasticsearch
TSLock provider backed by Elasticsearch.
A TSLock provider that implements LockProvider directly using a Painless script + upsert with refresh for immediate visibility. This is a faithful port of ShedLock's ElasticsearchLockProvider.
Installation
pnpm add @tslock/core @tslock/elasticsearch @elastic/elasticsearchUsage
import { createLockConfig, DefaultLockingTaskExecutor } from '@tslock/core';
import { ElasticsearchLockProvider } from '@tslock/elasticsearch';
import { Client } from '@elastic/elasticsearch';
const client = new Client({ node: 'http://localhost:9200' });
const provider = new ElasticsearchLockProvider(client);
const executor = new DefaultLockingTaskExecutor(provider);
await executor.executeWithLock(
() => myScheduledTask(),
createLockConfig({ name: 'my-task', lockAtMostFor: '5m', lockAtLeastFor: '1m' }),
);Configuration
new ElasticsearchLockProvider(client, options?) accepts:
| Option | Default | Description |
|---|---|---|
| index | 'shedlock' | The Elasticsearch index used for lock documents. |
| fieldNames | see below | Override any of name, lockUntil, lockedAt, lockedBy. |
Default field names: name, lockUntil, lockedAt, lockedBy (via FieldNames.DEFAULT).
Lock-name safety: Lock names must be non-empty, contain no control characters, and be at most 1024 UTF-8 bytes because the name is used as the lock document identifier.
Requirements
- Node.js >= 22
- Peer:
@elastic/elasticsearch
License
Apache 2.0 — see LICENSE for details.
