Skip to main content

Node.js Cron Job & Task Scheduler System

Build a reliable task scheduling system for Node.js with cron jobs, retries, concurrency control, and distributed locking.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt builds a reliable task scheduling system for a Node.js [framework] app, scheduling the specific [tasks] you list. It combines cron scheduling via [scheduler_lib], a durable task queue on [queue_backend], distributed locking to prevent duplicate execution across [instance_count] instances, retry-with-backoff plus a dead-letter queue, concurrency control capped at [max_concurrent] parallel tasks of the same type, monitoring, graceful shutdown, a manual trigger API, dependency chains, and failure alerts via [alert_channel].

The structure works because naive cron in a multi-instance deployment fires the same job on every node. By requiring distributed locking and a dead-letter queue up front — and tying them to [instance_count] — the output is forced to handle the realities of running scheduled work at scale rather than assuming a single process. The graceful-shutdown and dependency-chain pieces are what make it production-grade instead of a toy scheduler.

When to use it

  • When recurring jobs like report emails, cache warmups, or billing runs must fire reliably
  • When moving from one instance to [instance_count] and duplicate cron firing appears
  • When failed jobs need retries with backoff and a dead-letter queue for inspection
  • When some tasks must run strictly sequentially ([max_concurrent] of one)
  • When you need a manual trigger API to re-run a job on demand
  • When task failures must page someone through [alert_channel]
  • When you need an audit of execution history, success rates, and average durations for every scheduled job

Example output

You get a TypeScript implementation: the scheduler wired to [scheduler_lib], queue and worker setup on [queue_backend], the distributed lock mechanism that gates execution across [instance_count] instances, retry and dead-letter configuration, concurrency control capped at [max_concurrent] per task type, admin API endpoints for manual triggers and history, health-check integration, and typed task definitions for your [tasks]. Dependency chains are expressed so one task waits on another, and failure alerts route to [alert_channel]. It includes execution-history and success-rate monitoring rather than just a bare cron registration, so you can see at a glance which jobs are healthy and which are landing in the dead-letter queue.

Pro tips

  • Add distributed locking before you scale; once [instance_count] exceeds one, an unlocked cron runs on every node and double-charges or double-emails
  • Set [max_concurrent] to 1 for anything that mutates shared state, like invoice generation, to avoid overlapping runs
  • Make sure the dead-letter queue is actually inspected — a DLQ nobody reads just hides failures
  • Choose [scheduler_lib] with repeatable jobs (BullMQ) so schedules survive restarts instead of living only in memory
  • Test graceful shutdown explicitly; in-flight [tasks] that get killed mid-run on deploy are a common source of partial state
  • Wire [alert_channel] to fire on dead-letter arrivals, not just on first failure, so transient retries do not spam the channel

Frequently Asked Questions

How does this prevent the same cron job running on every instance?
It adds distributed locking tied to `[instance_count]`, so only one node acquires the lock and runs a given scheduled task. Without this, every instance fires the same cron, which can double-send emails or double-charge customers.
What happens to a task that keeps failing?
Failed tasks are retried with exponential backoff, and after exhausting attempts they land in a dead-letter queue for inspection. You should actually monitor that queue, since a dead-letter queue nobody reads simply hides recurring failures.
Can tasks depend on each other?
Yes. The prompt includes task dependency chains, so task B runs after task A completes. This is useful when, for example, a data cleanup must finish before a report email is generated from the cleaned data.
Does it handle deploys without killing running jobs?
It includes graceful shutdown that completes in-flight tasks before the process exits. Test this path explicitly, because tasks killed mid-run during a deploy are a frequent cause of partial or inconsistent state.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Node.js & TypeScript Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support