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

easy-tasker

v0.1.2

Published

A flexible task scheduler for interval and time-aligned scheduling

Downloads

4

Readme

EasyTasker - Task Scheduler

easy-tasker is a flexible task scheduler for Node.js that supports both interval-based and cron-based scheduling. It allows you to dynamically update the arguments, intervals, and cron schedules during runtime, making it ideal for automating recurring tasks.

Features

  • Interval-based scheduling: Schedule tasks to run at a fixed interval with an optional initial delay.
  • Cron-based scheduling: Schedule tasks using cron-formatted expressions for time-aligned execution.
  • Dynamic updates: Change the task arguments, intervals, or cron schedules during runtime.
  • Optional logging: Enable or disable logging for debugging and development without cluttering production logs.ts.

Installation

Install the package using npm:

npm install easy-tasker

Usage

Interval-based Scheduling

const TaskScheduler = require('easy-tasker');

// Define the task function
function runTask(status, message) {
  console.log(`${status}: ${message}`);
}

// Create a scheduler
const scheduler = new TaskScheduler(runTask, { logging: true, taskId: 'task-001' });

// Schedule an interval-based task
scheduler.intervalSchedule(2, 5, 'Running', 'Initial status');

// Pause the schedule after 10 seconds
setTimeout(() => {
  scheduler.pauseSchedule();
}, 10000);

// Resume the schedule after 15 seconds
setTimeout(() => {
  scheduler.resumeSchedule();
}, 15000);

// Stop the task after 25 seconds
setTimeout(() => {
  scheduler.stopScheduling();
}, 25000);

Cron-based Scheduling

const TaskScheduler = require('easy-tasker');

// Define a task function
function runTask(status, message) {
  console.log(`${status}: ${message}`);
}

// Create a task scheduler with logging disabled
const scheduler = new TaskScheduler(runTask, { logging: false });

// Schedule the task to run every minute using a cron expression
scheduler.timeAlignedSchedule('*/1 * * * *', 'Running', 'Cron-based scheduling');

API

intervalSchedule(delay, interval, task)

  • delay: Time in milliseconds before the task first starts.
  • interval: Time in milliseconds for the task to run repeatedly.
  • task: The function to execute at each interval.

This method schedules a task to execute after an initial delay and repeatedly at the specified interval.

Example:

scheduler.intervalSchedule(2000, 10000, () => {
    console.log('Task runs every 10 seconds after a 2-second delay');
});

timeAlignedSchedule(cronString, task)

  • cronString: A cron expression that specifies when the task should run.
  • task: The function to execute at the specified time.

This method schedules a task to execute at a specific time using a cron expression.

Example:

scheduler.timeAlignedSchedule('0 * * * *', () => {
    console.log('Task runs every hour on the hour');
});

updateArgs(...newArgs)

  • newArgs: New arguments to be passed to the task function for future executions.

setNewInterval(interval)

  • interval: The new interval in milliseconds. This method updates the interval of a running interval-based task.

Example:

scheduler.setNewInterval(3000);

setNewSchedule(cronString)

  • newCronString: A new cron-formatted string to update the task schedule. This method updates the cron expression for a time-aligned task. Example:
scheduler.setNewSchedule('*/5 * * * *');

stopScheduling()

Stops any scheduled jobs, whether interval-based or cron-based. This method will clear the interval or cancel the cron job, effectively stopping the task from executing further.

pauseSchedule()

Pauses the scheduled task. The task will not execute until resumeSchedule() is called.

resumeSchedule()

Resumes a paused task, allowing it to continue executing according to its interval or cron schedule.

Options

The TaskScheduler constructor accepts an optional options object to configure the following:

  • taskId: A string identifier for the task, used for logging (optional).
  • logging: A boolean that enables or disables logging (default: false).

Project Structure

TaskScheduler/
├── .gitignore
├── LICENSE
├── README.md
├── package.json
├── src/
│   └── TaskScheduler.js
└── test/
    └── TaskScheduler.test.js
  • src/TaskScheduler.js: Contains the main logic for scheduling tasks.
  • test/TaskScheduler.test.js: Unit tests for the scheduler.
  • package.json: NPM configuration file.
  • .gitignore: Specifies files and directories to be excluded from version control.
  • README.md: This file, providing an overview of the project.

License

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

Contributions

Feel free to open issues or submit pull requests to improve the functionality or add new features. Contributions are welcome!

Tests

To run the test suite, use the following command:

npm test

Make sure you have mocha and chai installed as dev dependencies to ensure smooth test execution.

Notes

  • Ensure that your cron expressions are valid and conform to the typical 5-field format used by node-schedule.
  • Tasks can be dynamically updated using the provided methods without the need to restart your application.