Skip to content

Node-cron Simple to Complex Setup with PM2

Cách tạo scheduled jobs với node-cron, phân biệt Express vs Worker processes, và quản lý với PM2 ecosystem

📋 Table of Contents

🎯 Giới Thiệu

Cron là gì?

Cron = Công cụ schedule tasks (jobs) chạy tự động theo lịch trên server.
Use cases:
Gửi email theo lịch
Backup database định kỳ
Cleanup old data
Generate reports
Sync data từ API
Send notifications

node-cron là gì?

node-cron = JavaScript library để tạo cron jobs trong Node.js
So với Linux crontab:
Feature
Linux crontab
node-cron
Syntax
* * * * *
* * * * * (giống)
Language
Shell/Bash
JavaScript/Node.js
Database access
Khó
Dễ (Sequelize, etc)
Error handling
Khó
Dễ (try-catch)
Logging
File system
Winston, console
Deployment
System level
App level
There are no rows in this table

🧠 Khái Niệm Cơ Bản

1. Cron Syntax

Ví dụ:

2. Process vs Thread

Process = 1 chương trình đang chạy độc lập
Đặc điểm:
Mỗi process có memory riêng
Chạy độc lập, không ảnh hưởng nhau
Crash 1 process ≠ crash tất cả
PID riêng (Process ID)

🔰 PHASE 1: Simple Cron (Console Log)

Mục tiêu: Hiểu cơ bản về cron

Step 1: Install

Step 2: Tạo Simple Worker

File: workers/simpleWorker.js

Step 3: Test

Output:
✅ Success! Bạn đã tạo cron job đầu tiên!
Press Ctrl+C để stop.

Step 4: Multiple Schedules

📝 PHASE 2: Database Integration

Mục tiêu: Ghi logs vào MySQL database

Step 1: Setup Database (ví dụ Sequelize)

Step 2: Tạo DB Worker

Step 3: Test DB Worker

Terminal 1: Run worker
Output:
Terminal 2: Check database
Result:
✅ Success! Worker đang ghi vào database!

🚀 PHASE 3: Production Worker

Mục tiêu: Tạo real-world email worker với error handling, retry logic
Architecture:

Step 1: Database Schema

Table: job_queue

Step 2: Email Worker

File: workers/emailWorker.js

Express vs Worker Processes

Khái Niệm Cốt Lõi

Express AppWorker2 Node.js processes RIÊNG BIỆT:

Comparison Table

Aspect
Express App
Worker Process
File
app.js / server.js
workers/emailWorker.js
Purpose
Handle HTTP requests
Run background jobs
Port
Listen on port (3000)
No port needed
Triggered by
User requests
Cron schedule
Process
1 separate process
1 separate process
Memory
Own memory space
Own memory space
Crash impact
Only Express crashes
Only Worker crashes
Communication
Via MySQL/Redis
Via MySQL/Redis
Scaling
Multiple instances
Usually 1 instance
There are no rows in this table
Đặc điểm:
✅ Chạy liên tục background
✅ Có cron job (node-cron)
✅ Gửi email
✅ Process scheduled tasks
❌ KHÔNG handle HTTP request
❌ KHÔNG có port

Communication Flow

Key Points:
Express và Worker KHÔNG nói chuyện trực tiếp
Giao tiếp qua MySQL database
Express write vào database
Worker read từ database
Decoupled architecture = Reliable & Scalable

PM2 Ecosystem Management

PM2 là gì?

PM2 = Process Manager for Node.js
Chức năng:
Start/stop/restart processes
Monitor processes (CPU, memory)
Auto-restart on crash
Auto-start on server boot
Log management
Cluster mode
Zero-downtime reload

Installation

Basic Commands

Ecosystem Config File

File: ecosystem.config.js (project root)

Usage with Ecosystem

Auto-Start on Server Boot

Test:

Log Management

Setup log rotation:
Result:
Logs > 10MB → auto-rotate
Keep last 7 files
Compress old logs
Check every 30 seconds

🐛 Troubleshooting

Issue 1: Cron Not Running

Symptoms:
Worker starts but no logs
Jobs not processed
Debug:
Solution:
Verify cron syntax
Check server time: date
Ensure worker is running: pm2 status

Issue 2: Jobs Stuck in Processing

Symptoms:
Jobs have status = 'processing'
Never complete
Debug:
Solution:

Issue 3: Memory Leak

Symptoms:
Worker memory increases over time
PM2 restarts worker frequently
Debug:
Solution:

Issue 4: Multiple Workers Running

Symptoms:
Jobs processed twice
Duplicate emails sent
Debug:
Solution:

Issue 5: PM2 Not Auto-Starting on Boot

Symptoms:
After server reboot, workers not running
Debug:
Solution:

🎓 Summary

Key Takeaways:

node-cron = Schedule jobs in Node.js
Cron syntax: * * * * * = Every minute
Separate processes: Express ≠ Worker
Communication: Via database (MySQL)
PM2: Manage multiple processes
ecosystem.config.js: Define all processes
Graceful shutdown: Handle SIGTERM/SIGINT
Error handling: Try-catch, retry logic
Monitoring: PM2 logs, metrics, alerts
Production: Auto-start, auto-restart, log rotation

Learning Path:

Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.