@hexok/ioredis-lifecycle
Connection lifecycle for hexok Redis adapters built on ioredis. Redis adapters import it. App code does not: each adapter re-exports LifecycleError, so an app imports it from the adapter it uses. It holds no queue, broadcast, or schedule logic.
ioredis 6 is a peer dependency, because the lifecycle works on the adapter’s own clients. Install with npm i @hexok/ioredis-lifecycle ioredis. Connections negotiate RESP3 and keep v5 reply shapes.
Guarantees
Section titled “Guarantees”- Every client has an
errorlistener from the moment it opens. Errors go to theLogSinkwith{ adapter, connection }. ioredis does not print “Unhandled error event”. - Outside
stop()cleanup, a guarded command runs only while the state isready. Otherwise it rejects withNotStarted,Starting, orStopped, and it does not run. During cleanup,context.runworks while the state isstopping. - A client that ends after
start()is logged aterror. stop()resolves withinstopTimeoutMswhile Redis refuses connections, stops answering, or the client is reconnecting.- A guarded command still pending when
stop()stops waiting rejects withStoppedBeforeReply. It does not hang. stop()clears every timer it made. It removes everyreadyandabortlistener it added. Each client keeps itserrorlistener, which still logs, and itsendlistener, which does nothing once the lifecycle stops.- A settled guarded command leaves nothing behind. Its listeners belong to that call and are removed when it settles.
start()works again afterstop()or after a failed start. It opens new clients.
Lifecycle
Section titled “Lifecycle”The states are idle, starting, ready, stopping, and stopped. A failed start returns to idle. Read lifecycle.state to add it to logs.
state is ready from start() until stop(), even while Redis is down. It says whether the adapter accepts calls, not whether Redis answers. Use it to gate traffic during startup and shutdown, not as a Redis health check.
start(open, onReady?) calls open to make the clients, then waits until each one is ready. It connects a client made with lazyConnect. A start() during a pending start joins it, and its open is not called. A start() while ready resolves at once. onReady runs once, when the state becomes ready and before start() resolves. Set adapter timers there, so two start() calls cannot set them twice. If onReady throws, the start fails.
start() rejects with:
EndedBeforeReadywhen a client ends, such as whenretryStrategygives up.StartTimeoutafterstartTimeoutMs.Stoppedwhenstop()is called first, or whenstop()has not finished.
After a failed start, every client is disconnected and a later start() begins clean.
run(operation, command, options?) runs command with the clients and returns its result. operation is the adapter method name, such as publish. It appears in error messages. The command’s own error passes through unchanged. Pass { signal } to end a command early with signal.reason, such as a blocking read on a client you are about to detach(). assertReady(operation) throws the same state errors synchronously, for a synchronous method.
attach({ operation, connection, open, fields? }) opens one more client while ready, such as a blocking connection per subscription. Its errors are logged with { adapter, connection, ...fields }, and stop() closes it. detach(client) disconnects it now without QUIT.
stop(cleanup?) does these steps in order, within one budget of stopTimeoutMs:
- The state becomes
stopping. Newrun()calls reject withStopped. A pendingstart()rejects withStopped. - When the lifecycle was ready,
cleanupruns while in-flight commands finish. Cleanup runs commands withcontext.run, which works during stop.context.signalaborts when stop stops waiting. - Guarded commands still pending reject with
StoppedBeforeReply. - Each ready client gets
QUITwithin the time left. A client that is not ready, or whoseQUITdoes not settle, is disconnected. - The state becomes
stopped.
Cleanup runs before pending commands are rejected, because cleanup usually needs Redis, to release a lock or remove a consumer. In-flight commands get the same time to finish, so a healthy Redis answers them and their outcome is known. Cleanup does not run when stop() cancels a pending start. A stop() during a pending stop joins it, and its cleanup is not called. An error from cleanup is logged, and stop goes on.
Errors
Section titled “Errors”Every error is a LifecycleError, a hexok Errors catalog. Match on code. An error’s name is ioredis-lifecycle.<Code>, such as ioredis-lifecycle.StoppedBeforeReply.
import { LifecycleError } from '@hexok/ioredis-lifecycle';
if (LifecycleError.is(error) && error.code === 'StoppedBeforeReply') { // Redis may or may not have run the command.}| Code | When |
|---|---|
NotStarted |
A command before start(), or after a failed start. |
Starting |
A command while start() is pending. |
Stopped |
A command during or after stop(). A start() that stop() cancelled. |
StoppedBeforeReply |
stop() stopped waiting before Redis replied. Redis may or may not have run the command. |
StartTimeout |
A client was not ready within startTimeoutMs. |
EndedBeforeReady |
A client ended before it was ready. |
Each message starts with the adapter name and says the fix, such as queue: start() has not finished. Await start() before publish().
NotStarted, Starting, and Stopped mean the call came at the wrong time. Fix the call order. Do not retry them. StoppedBeforeReply means Redis may or may not have run the command. The adapter is stopping, so do not retry on it. StartTimeout and EndedBeforeReady reject start() and close every connection. Call start() again, or exit and let the orchestrator restart the process.
LifecycleError.is uses instanceof, so it matches only errors from the same copy of this package. Code that may see errors from several adapter versions should match on error.code.
The lifecycle logs these through the LogSink. fields are the ones passed to attach.
| Event | Level | Fields |
|---|---|---|
A client emits error, such as a failed connection attempt |
error |
adapter, connection, fields |
A client ends after start() |
error |
adapter, connection, fields |
QUIT fails during stop() |
error |
adapter, connection, fields |
cleanup throws during stop() |
error |
adapter |
Adapters follow four rules in their own logs:
- An adapter logs a
StoppedBeforeReplyfrom its own background commands atwarn. It does not logNotStarted,Starting, orStoppedfrom background work. Errors returned to the caller are not logged by the adapter. - A log of a
LifecycleErrorcarries itscodeas a field, such as{ adapter, connection, code: 'StoppedBeforeReply' }, so log queries filter on it without parsing messages. - The
connectionfield is present only on a connection error or a failed Redis command. A handler, job, or decode failure is user-code or data failure and carries noconnection. - Every adapter log carries
adapter. Thenconnection, by the rule above, and the adapter’s subject field:topicandgroupfor the queue,channelfor broadcast, andjobfor schedule.
When Redis is down
Section titled “When Redis is down”- During
start(), ioredis retries with itsretryStrategy, and each failed attempt is logged. WithoutstartTimeoutMs,start()waits until Redis returns. An app that boots before Redis then waits for it. - If
retryStrategygives up afterstart(), the connection ends and is logged aterror.statestaysreadyand commands fail untilstop()andstart(). The defaultretryStrategynever gives up. - During an outage, ioredis holds commands in its offline queue.
openRedissetsmaxRetriesPerRequest: null, so they wait for Redis instead of failing after 20 attempts. disconnect()on a reconnecting client does not end it and does not flush its offline queue. ioredis would never settle those commands. The guard rejects them withStoppedBeforeReplywhenstop()stops waiting.- A Redis that accepts connections and does not answer never replies to
QUIT. TheQUITgets only the time left, and then the client is disconnected. - After
stop()resolves, ioredis can hold a half-closed socket to an unanswering Redis fordisconnectTimeout. That is 2 seconds by default. SetdisconnectTimeoutin the redis options to let the process exit sooner.
Configuration
Section titled “Configuration”new RedisLifecycle(options) takes:
| Option | Default | Why |
|---|---|---|
adapter |
required | Prefixes every error message and is the adapter log field. |
stopTimeoutMs |
required | Each adapter knows how long its cleanup needs, such as waiting for handlers. The adapter sets and documents its default. |
startTimeoutMs |
unset | See below. |
logger |
no-op | A structural LogSink. @hexok/logger and console fit it. |
Unset, start() waits for Redis, which suits compose-style boot. Set it under an orchestrator such as Kubernetes, so the process exits and is restarted.
An invalid stopTimeoutMs or startTimeoutMs throws a RangeError that starts with the adapter name.
openRedis(redis) takes a URL or ioredis options and sets maxRetriesPerRequest: null. Options you pass win.
sleep(ms, signal?) resolves after ms or as soon as signal aborts. Use it for backoff. It never rejects and leaves no timer or listener.
Writing an adapter
Section titled “Writing an adapter”The adapter owns one RedisLifecycle. start, every command, and stop go through it.
import { LifecycleError, type LogSink, openRedis, RedisLifecycle,} from '@hexok/ioredis-lifecycle';import type { Redis, RedisOptions } from 'ioredis';
export interface RedisLockConfig { readonly redis: string | RedisOptions; /** Default 5000. */ readonly stopTimeoutMs?: number; readonly logger?: LogSink;}
const silent: LogSink = { debug() {}, info() {}, warn() {}, error() {} };
export class RedisLock { readonly #redis: string | RedisOptions; readonly #logger: LogSink; readonly #lifecycle: RedisLifecycle<{ commands: Redis }>; readonly #held = new Set<string>(); #renew: ReturnType<typeof setInterval> | undefined;
constructor(config: RedisLockConfig) { this.#redis = config.redis; this.#logger = config.logger ?? silent; this.#lifecycle = new RedisLifecycle({ adapter: 'lock', stopTimeoutMs: config.stopTimeoutMs ?? 5000, logger: config.logger, }); }
start(): Promise<void> { return this.#lifecycle.start( () => ({ commands: openRedis(this.#redis) }), () => { this.#renew = setInterval(() => void this.#renewAll(), 10_000); }, ); }
async acquire(key: string): Promise<boolean> { const set = await this.#lifecycle.run('acquire', ({ commands }) => commands.set(key, 'me', 'PX', 30_000, 'NX'), ); if (set === 'OK') this.#held.add(key); return set === 'OK'; }
stop(): Promise<void> { return this.#lifecycle.stop(async ({ run }) => { clearInterval(this.#renew); this.#renew = undefined; // `run` from the context works during stop. `this.#lifecycle.run` rejects with `Stopped`. await Promise.all( [...this.#held].map((key) => run('release', ({ commands }) => commands.del(key)), ), ); this.#held.clear(); }); }
async #renewAll(): Promise<void> { for (const key of this.#held) { try { await this.#lifecycle.run('renew', ({ commands }) => commands.pexpire(key, 30_000), ); } catch (error) { this.#logRenewFailure(error, key); } } }
/** Background work: a stop in progress is a `warn`, a call at the wrong time is not logged. */ #logRenewFailure(error: unknown, key: string): void { if (LifecycleError.is(error)) { if (error.code === 'StoppedBeforeReply') { this.#logger.warn(error.message, { adapter: 'lock', connection: 'commands', code: error.code, key, }); } return; } this.#logger.error( error instanceof Error ? error : new Error('lock: renew failed'), { adapter: 'lock', connection: 'commands', key }, ); }}A client per subscription goes through attach, and its reads take a signal so detach can end them:
const abort = new AbortController();const blocking = this.#lifecycle.attach({ operation: 'subscribe', connection: 'blocking', open: () => openRedis(this.#redis), fields: { topic },});const rows = await this.#lifecycle.run( 'subscribe', () => blocking.xreadgroup('GROUP', group, consumer, 'BLOCK', 1000, 'STREAMS', key, '>'), { signal: abort.signal },);
// On unsubscribe:abort.abort(new Error('unsubscribed'));this.#lifecycle.detach(blocking);