1.0.6 • Published 5 months ago

jsm-scheduler v1.0.6

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

jsm-scheduler

npm version License: MIT Downloads Bundle Size Issues Pull Requests Build Status

A lightweight and intuitive job scheduler library for Node.js. Leverage decorators to define and manage cron jobs in a clean and scalable way.

Features

  • Schedule jobs using simple decorators like @Cron, @Delay, @MaxFreezeTime, etc.
  • Supports production-only jobs with @ProductionOnly.
  • Modular and clean architecture.
  • Built on top of the powerful node-schedule library.

Installation

Install the package using npm:

npm install jsm-scheduler

Or with yarn:

yarn add jsm-scheduler

Usage

Basic Example

Here is how you can use jsm-scheduler to define and manage scheduled tasks:

1. Define Your Scheduler Class

import { Scheduler, Cron, Delay, MaxFreezeTime, TaskName, ProductionOnly } from 'jsm-scheduler';

@Scheduler
class TaskScheduler {
  
  @Cron('0 0 0 * * *')
  @Delay(1000) // Adds a 1-second delay before execution
  @MaxFreezeTime(15 * 60 * 1000) // Ensures tasks are frozen for a maximum of 15 minutes
  @TaskName('midnightTask') // Custom task name
  @ProductionOnly() // Executes only in production environments
  async midnightTask() {
    console.log('Executing midnight task logic...');
    // Add your custom task logic here
  }
}

2. Initialize the Scheduler

Simply import the file containing your scheduler class to initialize it:

// index.ts
import './task-scheduler'; // Import your scheduler class

When you run your application, the scheduler will automatically register and execute the defined jobs.

Advanced Example

You can define multiple jobs with different configurations:

import { Scheduler, Cron, Delay, TaskName } from 'jsm-scheduler';

@Scheduler
class AdvancedScheduler {
  @Cron('*/10 * * * * *') // Runs every 10 seconds
  @TaskName('heartbeat')
  async heartbeatTask() {
    console.log('Heartbeat task running...');
  }

  @Cron('0 0 12 * * *') // Runs every day at noon
  async dailyReportTask() {
    console.log('Generating daily report...');
    // Your report generation logic here
  }
}

Decorators

@Cron(cron: string)

Defines the cron expression for the task.

@Delay(delay: number)

Adds a delay (in milliseconds) before the task execution.

@MaxFreezeTime(maxFreezeTime: number)

Ensures a task cannot be frozen for more than the specified time (in milliseconds).

@TaskName(name: string)

Sets a custom name for the task.

@ProductionOnly()

Restricts the execution of the task to production environments (NODE_ENV=production).

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! If you'd like to report issues or submit pull requests, please visit the GitHub repository.

Links

Author

Developed by Reevo Solutions.

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago