| 11 | * runs. For HA, per-fire coordination, provide a Redis-backed RunCoordinator. |
| 12 | */ |
| 13 | export 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 | } |
nothing calls this directly
no outgoing calls
no test coverage detected