Registry / database / node-mysql-cron

node-mysql-cron

JSON →
library1.0.1jsnpmunverified

A MySQL-based periodic cron job system supporting async worker functions with automatic error handling and JSON result serialization. Version 1.0.1 requires Node.js 12+ and a MySQL database. It provides conditional job execution via SQL WHERE clauses, parallel job execution limits, stalled job detection, and retry logic. Unlike traditional cron, it stores job definitions and history in a MySQL table, enabling distributed scheduling and persistence. Full TypeScript support and comprehensive type definitions are included. Release cadence is irregular.

npm install node-mysql-cron
INSTALL
IMPORT
SIG · NODE-MYSQL-CRON
N
node-mysql-cron
databasejavascriptv1.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Cron
import Cron from 'node-mysql-cron'
const Cron = require('node-mysql-cron'); // CommonJS require is also valid for this package but preferred to use default import
Default export. TypeScript type definitions are included.
Job
import type { Job } from 'node-mysql-cron'
const { Job } = require('node-mysql-cron'); // Job is a type, not a runtime export
Type-only import for TypeScript. Do not import as a value.
WorkerFunction
import type { WorkerFunction } from 'node-mysql-cron'
Type-only import for TypeScript. Defines signature for async worker function.

Sets up a MySQL connection pool, configures the cron system, registers an async worker, and starts processing jobs.

const mysql = require('mysql'); const Cron = require('node-mysql-cron'); const pool = mysql.createPool({ host: 'localhost', user: 'root', password: 'password', database: 'mydb', timezone: 'UTC', }); Cron.config({ pool, jobTable: 'nmc_job', pollInterval: 60000, parallelLimit: 2, errorLog: console.error, }); async function fetchDataWorker(job) { console.log(`Processing job: ${job.job_name}`); const response = await fetch('https://api.example.com/data'); const data = await response.json(); return { success: true, recordsProcessed: data.length, timestamp: new Date().toISOString(), }; } Cron.setWorker('fetch_data', fetchDataWorker); Cron.start();
Debug
Known issues
gotchaThe 'mysql' package must be a peer dependency and the pool must use 'mysql' (not 'mysql2') as the library expects the callback-based mysql API.
fix
Install mysql package: npm install mysql. Do not use mysql2 pool.
affects: >=1.0.0
gotchaWorker functions must be async and return a JSONValue (string, number, boolean, null, object, or array). Non-serializable values like undefined or Functions will cause runtime errors.
fix
Ensure workers are async and return a plain object or primitive.
affects: >=1.0.0
gotchaThe job table must be created with the exact schema as documented. Missing columns or wrong data types will cause SQL errors.
fix
Execute the CREATE TABLE statement from the documentation before starting Cron.
affects: >=1.0.0
gotchaCron.config() must be called before Cron.start() and must include a valid pool. Calling methods in the wrong order will throw errors.
fix
Always call Cron.config() first, then register workers with Cron.setWorker(), and finally Cron.start().
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cron.config is not a function
Importing incorrectly: using named import instead of default import.
fix
Use default import: import Cron from 'node-mysql-cron' (ESM) or const Cron = require('node-mysql-cron') (CJS).
Error: pool is required
Cron.config() was called without a pool property or pool is undefined.
fix
Ensure you pass an object with 'pool' key pointing to a mysql connection pool. Example: Cron.config({ pool: myPool }).
Cannot find module 'mysql'
The mysql package is not installed (peer dependency).
fix
Run: npm install mysql
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
mysqlrequiredPeer dependency for creating a connection pool passed to Cron.config
Agent activity
5 hits · last 30 days
node
4
Resources
node-mysql-cron — npm install node-mysql-cron · libregistry