Package Exports
- pnscheduler
- pnscheduler/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pnscheduler) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
PNScheduler
Persistent NodeJS Scheduler - A simple job scheduler for Node.js applications, with Postgres-based persistence.
Installation
$ npm install pnschedulerFeatures
- Schedule jobs at a specific time
- Jobs persist between restarts
- Configurable grace periods
Set Up
First of all, set the PGCONNECTIONSTRING environment variable for your Postgres database:
#.env
PGCONNECTIONSTRING=<connection_string>Secondly, run the migration script to create the database tables:
$ node --env-file=.env node_modules/pnscheduler/dist/db/migrationScript.jsQuick Start
Just define a job, schedule it, and start the scheduler.
import scheduler from 'pnscheduler';
// Define a simple job
scheduler.defineJob("greeting", () => {
console.log("Hello, world!");
});
// Schedule it for tomorrow
await scheduler.scheduleJob("greeting", new Date(Date.now() + 86400000));
// Start the scheduler
scheduler.start();Usage Examples
Defining Jobs
// Job without parameters
scheduler.defineJob("simple-job", () => {
console.log("Hello without params!");
});
// Job with parameters
scheduler.defineJob("parameterized-job",
({ something }: { something: string }) => {
console.log(something);
}
);Scheduling Jobs
// Basic scheduling
await scheduler.scheduleJob("simple-job", new Date(2084, 0, 1));
// With parameters
await scheduler.scheduleJob("parameterized-job", new Date(2084, 0, 1), {
something: "Hello, world!"
});
// With grace period (5 minutes)
await scheduler.scheduleJob("simple-job", new Date(2084, 0, 1), null, 300);Job Management
// Unscheduling a job
const job = await scheduler.scheduleJob("simple-job", new Date(2084, 0, 1));
await scheduler.unscheduleJob(job);
// Manual job execution
await scheduler.executeJob(job);
// Finding scheduled jobs
const scheduledJobs = await scheduler.findScheduledJobs();
console.log(scheduledJobs);
// Finding due jobs
const dueJobs = await scheduler.findDueJobs();
console.log(dueJobs);Scheduler Control
// Adjust check frequency (defaults to 1 second)
scheduler.start(30); // check every 30 seconds
// Stop the scheduler
scheduler.stop();API Reference
Scheduler Methods
defineJob(name: string, handler: Function): voidscheduleJob(name: string, date: Date, params?: any, gracePeriod?: number): Promise<Job>unscheduleJob(job: Job): Promise<void>executeJob(job: Job): Promise<void>findScheduledJobs(): Promise<Job[]>start(interval: number): voidstop(): void
Scheduler Properties
jobRegistry- As an alternative todefineJob, you can also define jobs by assigning directly to this property,
e.g.scheduler.jobRegistry = {"job-name", () => {}}.verbose- Set this to true to enable verbose logging,
e.g.scheduler.verbose = true;
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License