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

@rakered/cron

v1.3.1

Published

Lightweight job scheduling for Node.js

Downloads

43

Readme

@rakered/cron

Lightweight job scheduling for Node.js, backed by a mongo database.

social image

Usage

This cron-runner schedules jobs, and runs the handlers when the time comes. All jobs are stored in a mongodb collection, to support persistence and load balancing. Spawn as many services as you like, and every job is guaranteed to be only run once. R

The database connection is managed with @rakered/mongo. The runner will connect to the MongoDB instance that's available under process.env.MONGO_URL, and will use the jobs collection to save and read the documents.

import cron from '@rakered/cron';

cron.schedule('every 5 minutes', 'purge', (job) => {
  console.log('house cleaning…');
});

Error handling

Rescheduling failed jobs, falls within the responsibility of your job handler.

import cron from '@rakered/cron';

cron.schedule('once', 'purge', (job) => {
  try {
    console.log('house cleaning…');
  } catch (error) {
    cron.reschedule(job);
  }
});

schedule(schedule, name, data | handler)

This method is used to schedule the jobs. It's possible to provide a handler directly, or later via the .handle method. Providing handlers directly, results in more compact code. Providing them separately, allows separating job scheduling from job handling. Depending on the work load, it might be nice to schedule jobs in your main app, while handling them in a microservice.

  • schedule String

    The schedule can be defined by either a cron expression, with or without second segment, or a text expression. Using the cron would look like 15 10 * * ? *, while the text looks like every 15 seconds.

  • name String

    Job names should be unique. This is used to ensure that every job is only run once, even when multiple runners are active.

  • data Record<string, unknown> | (job: Job) => Promise

    When providing a function, the function will act as handler. In all other cases, the data will be saved along with the job definition, and provided to the handler that's being defined by .handle.

import cron from '@rakered/cron';

cron.schedule('*/5 * * * *', 'clean-house', { houseKeeper: 'some person' });

cron.schedule('every 5 minutes', 'get-coffee', (job) => {
  console.log('house cleaning…');
});

scheduleMany(name, data[])

This method is used to schedule multiple jobs of the same type, to run as soon as possible. The job will be run once for each dataset. This method can be used to easily distribute load across various job runners or even servers.

  • name String

    The name of the job that should be run.

  • data Record<string, unknown>[]

    The datasets to schedule this job for. There will be created a single job, for each dataset.

import cron from '@rakered/cron';

cron.scheduleMany('send-daily-digest', [
  { email: '[email protected]' },
  { email: '[email protected]' },
]);

reschedule(job)

This method is used to reschedule failed jobs. Note that recurring jobs already run on a schedule, so reschedule those with caution. Jobs that are run once, can safely be rescheduled, but make sure that the error is something that can be overcome by time. Don't expect different outcomes from trying the same thing twice.

Rescheduled jobs, will run again 5 minutes after rescheduling.

  • job Job

    The job that's being passed to the job handler.

import cron from '@rakered/cron';

cron.schedule('*/5 * * * *', 'clean-house', { houseKeeper: 'some person' });

cron.schedule('once', 'get-coffee', (job) => {
  try {
    console.log('house cleaning…');
  } catch (e) {
    cron.reschedule(job, 5);
  }
});

handle(name, handler)

The handle method can be used to register handlers for specific job types. The name should match a name from jobs that are being scheduled.

  • name String

    The name of the job. Only jobs that are being scheduled with the same name will be received by this handler.

  • handler (job: Job): Promise

    The function that should be invoked when a job of this type is being triggered.

import cron from '@rakered/cron';

cron.handle('purge', (job) => {
  console.log('house cleaning…');
});