BullMQ.LockManager (BullMQ v1.3.2)
View SourceManages lock renewal for BullMQ workers.
Instead of creating a timer for each active job, LockManager uses a single timer that periodically checks all tracked jobs and extends locks for those that are about to expire. This is much more efficient when processing many concurrent jobs.
The lock manager:
- Runs a single timer every
lock_renew_time / 2milliseconds - Tracks all active jobs with their tokens and timestamps
- Extends locks in batch for all jobs whose locks are expiring
- Emits events when lock renewal fails
Summary
Functions
Returns a specification to start this module under a supervisor.
Gets the number of jobs currently being tracked.
Gets a list of all tracked job IDs.
Checks if a specific job is being tracked.
Starts the lock manager.
Stops the lock manager.
Starts tracking a job for lock renewal.
Stops tracking a job (call when job completes or fails).
Types
@type job_info() :: %{token: String.t(), ts: non_neg_integer()}
@type state() :: %{ connection: atom(), keys: map(), lock_duration: non_neg_integer(), lock_renew_time: non_neg_integer(), tracked_jobs: %{required(String.t()) => job_info()}, timer_ref: reference() | nil, closed: boolean(), on_lock_renewal_failed: function() | nil, on_locks_renewed: function() | nil }
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec get_active_job_count(pid()) :: non_neg_integer()
Gets the number of jobs currently being tracked.
Gets a list of all tracked job IDs.
Checks if a specific job is being tracked.
@spec start_link(keyword()) :: GenServer.on_start()
Starts the lock manager.
Options
:connection- The Redis connection name (required):keys- Queue keys context (required):lock_duration- Lock duration in milliseconds (default: 30000):lock_renew_time- Time between lock renewal checks (default: lock_duration / 2):on_lock_renewal_failed- Callback when lock renewal fails for jobs:on_locks_renewed- Callback when locks are successfully renewed
@spec stop(pid()) :: :ok
Stops the lock manager.
Starts tracking a job for lock renewal.
Stops tracking a job (call when job completes or fails).