New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

cron-task

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cron-task

A simple cron task library for node.js with some useful features. Like assigning a name to task and getting the task by name. or category to task and getting the task by category.

latest
Source
npmnpm
Version
1.0.6
Version published
Maintainers
1
Created
Source

cron-task

cron-task is a module for scheduling tasks at specific time intervals using cron expressions or milliseconds. You can easily add, remove, start, and stop tasks.

Installation

To install cron-task, run the following command:

npm install cron-task

Usage

const { CronTaskScheduler } = require('cron-task');

// Create scheduler instance
const scheduler = new CronTaskScheduler();

// Add a simple "Hello World" task that runs every 3 seconds
scheduler.addTask({
  name: 'helloWorldTask',
  interval: 3000, // 3 seconds in milliseconds
  onTick: () => {
    console.log('Hello World!', new Date().toLocaleTimeString());
  }
});

console.log('Scheduler started. Press Ctrl+C to stop.');

API

Task class

  • task: Object describing the task with the following properties:
    • id: unique uuid for task (this not be send on creation)
    • name: Unique name for the task.
    • category (optional): Task category.
    • interval: Interval in milliseconds or cron expression.
    • onTick: Function executed at each interval.

Methods

  • addTask(task: Omit<Task, 'id'>): Task
  • getTasks(): Task[]
  • getTaskById(id:string): void
  • getTasksByName(name: string): Task[]
  • getTasksByCategory(category: string): Task[]
  • removeTaskById(id:string): void
  • removeTaskByName(name: string): void
  • removeTasksByCategory(category: string): void
  • removeAllTasks(): void
  • startTaskById(id: string): void
  • startTaskByName(name: string): void
  • stopTaskByName(name: string): void
  • stopAllTasksByCategory(category: string): void
  • updateTaskById(id:string): void

Cron Expression

* * * * * 
- - - - -
| | | | |
| | | | +---- Day of the week (0 - 7) (Sunday=0 or 7)
| | | +------ Month (1 - 12)
| | +-------- Day of the month (1 - 31)
| +---------- Hour (0 - 23)
+------------ Minute (0 - 59)

Contribution

If you encounter any issues or have suggestions, feel free to open an issue or submit a pull request.

Thanks for contributing!

Keywords

cron

FAQs

Package last updated on 26 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts