npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cron-task

v1.0.5

Published

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.

Downloads

12

Readme

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 an instance of the scheduler
const scheduler = new CronTaskScheduler();

// Add tasks with categories
const task1 = scheduler.addTask({
  name: 'myTask',
  interval: '1 * * * *', // Run every 1 minute
  onTick: () => {
    console.log('Task executed every minute');
  },
  category: 'category1'
});

const task2 = scheduler.addTask({
  name: 'myTask2',
  interval: 15000, // Run every 15 seconds
  onTick: () => {
    console.log('Task executed every 15 seconds');
  },
  category: 'category1'
});

const task3 = scheduler.addTask({
  name: 'myTask3',
  interval: 10000, // Run every 10 seconds
  onTick: () => {
    console.log('Task executed every 10 seconds');
  },
  category: 'category2'
});


// Print tasks by ID
console.log("Task by ID:", scheduler.getTaskById(task1.id));

// Wait for 30 seconds to allow tasks to execute
setTimeout(() => {
  // Update task interval
  const updatedTask = scheduler.updateTaskById(task1.id, {
    name: 'myTask',
    interval: '*/2 * * * *', // Update to run every 2 minutes
    onTick: () => {
      console.log('Updated task executed every 2 minutes');
    },
    category: 'category1'
  });
  console.log('Updated task:', updatedTask);

  // Stop specific task by name
  scheduler.stopTaskByName('myTask2');
  console.log('Task myTask2 stopped');

  // Start a task by name
  scheduler.startTaskByName('myTask2');
  console.log('Task myTask2 started again');

  // Stop all tasks
  scheduler.stopAllTasks();
  console.log('All tasks stopped');

  // Print remaining tasks to verify they were stopped
  console.log('Tasks after stopping all:', scheduler.getTasks());

  // Start all tasks by category
  scheduler.startAllTasksByCategory('category1');
  console.log('Started all tasks in category1');

  // Stop all tasks by category
  scheduler.stopAllTasksByCategory('category1');
  console.log('Stopped all tasks in category1');

  // Remove tasks by category
  scheduler.removeTasksByCategory('category1');
  console.log('Removed all tasks in category1');

  // Get tasks by category
  const tasksByCategory = scheduler.getTasksByCategory('category2');
  console.log('Tasks in category2:', tasksByCategory);

  // Get tasks by name
  const tasksByName = scheduler.getTasksByName('myTask3');
  console.log('Tasks with name myTask3:', tasksByName);

}, 30000); // Wait for 30 seconds

// Wait for another 60 seconds before performing more actions
setTimeout(() => {
  // Add and start a new task
  const task4 = scheduler.addTask({
    name: 'myTask4',
    interval: 5000, // Run every 5 seconds
    onTick: () => {
      console.log('Task executed every 5 seconds');
    },
    category: 'category3'
  });
  console.log('Added new task:', task4);

  // Wait and then stop a specific task by name
  setTimeout(() => {
    scheduler.stopTaskByName('myTask4');
    console.log('Task myTask4 stopped');

    // Remove a task by ID
    scheduler.removeTaskById(task4.id);
    console.log(`Task ${task4.id} removed`);

    // Print remaining tasks
    console.log('Tasks after removal:', scheduler.getTasks());
    // Remove all tasks
    scheduler.removeAllTasks();
    console.log('All tasks removed');

    // Print remaining tasks to verify removal
    console.log('Tasks after removal of all:', scheduler.getTasks());
  }, 20000); // Wait for 20 seconds
}, 60000); // Wait for 60 seconds

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!