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

@kreisler/js-cron

v2.0.0

Published

Wrapper for node-cron

Downloads

4

Readme

JsCron - Node.js Cron Task Manager ⏰🚀

A powerful and flexible class for managing scheduled tasks using node-cron in Node.js with TypeScript support.

Features

  • Create, destroy, pause, and resume cron jobs easily.
  • Manage tasks using the underlying node-cron API.
  • Supports setting default configurations like timezone and running tasks on initialization.

Installation

Install the package @kreisler/js-cron:

npm install @kreisler/js-cron

Import the class

Import JsCron with ES Module syntax:

import { JsCron } from '@kreisler/js-cron';

or using CommonJS:

const { JsCron } = require('@kreisler/js-cron');

Usage

Create a new instance of JsCron:

const JsCron = new JsCron({ timezone: 'America/Bogota' });

Creating a Task

To create a new scheduled task, use the createTask method with a name, cron expression, and a callback function:

const result = JsCron.createTask('morningTask', '0 0 8 * * *', () => {
  console.log('Buenos días! ☀️');
});
console.log(result); // { success: true, message: 'Tarea "morningTask" creada con éxito.' }

Destroying a Task

Use destroyTask to stop and remove a task by its name:

const result = jsCron.destroyTask('morningTask');
console.log(result); // { success: true, message: 'Tarea "morningTask" destruida con éxito.' }

Pausing a Task

Pause an existing task without removing it:

const result = jsCron.pauseTask('morningTask');
console.log(result); // { success: true, message: 'Tarea "morningTask" pausada.' }

Resuming a Paused Task

Resume a paused task:

const result = jsCron.resumeTask('morningTask');
console.log(result); // { success: true, message: 'Tarea "morningTask" reanudada.' }

Getting All Tasks

Retrieve all managed tasks:

const tasks = jsCron.getTasks();
console.log(tasks); // Outputs a Map containing all the ScheduledTask instances

Getting a Specific Task

Get a specific task by its name:

const task = jsCron.getTask('morningTask');
console.log(task); // Outputs the ScheduledTask instance or undefined if not found

Examples

Example #1: Basic Usage

Create a task that runs every 10 seconds:

const jsCron = new JsCron();
jsCron.createTask('taskEvery10Seconds', '*/10 * * * * *', () => {
  console.log('Task running every 10 seconds!');
});

Configuration Options

You can pass configuration options to the constructor, such as timezone and runOnInit:

const jsCron = new JsCron({
  timezone: 'America/Bogota',
  runOnInit: true // Optional, will run tasks immediately upon creation
});