MCPcopy
hub / github.com/node-cron/node-cron / EnvVarRunCoordinator

Class EnvVarRunCoordinator

src/coordinator/env-var-run-coordinator.ts:13–35  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11 * runs. For HA, per-fire coordination, provide a Redis-backed RunCoordinator.
12 */
13export class EnvVarRunCoordinator implements RunCoordinator {
14 constructor(private readonly envName: string = 'NODE_CRON_RUN') {
15 // Validate eagerly so the failure happens at schedule time (startup),
16 // not at the first fire.
17 this.read();
18 }
19
20 shouldRun(): boolean {
21 return this.read();
22 }
23
24 private read(): boolean {
25 const value = process.env[this.envName];
26 if (value !== 'true' && value !== 'false') {
27 throw new Error(
28 `node-cron: a \`distributed\` task needs ${this.envName} set to 'true' or 'false'. ` +
29 `Set it to 'true' on exactly one instance and 'false' on the others, ` +
30 `or provide a coordinator via cron.setRunCoordinator(...).`
31 );
32 }
33 return value === 'true';
34 }
35}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected