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

@siddiqus/crony

v1.0.0

Published

A NodeJS Cron Client

Downloads

1

Readme

Crony - Scheduled jobs

Features:

  • Fault tolerant - exceptions handled
  • Easily initialize cron jobs in a declarative format
  • Redis based redlock for ensuring one-time execution per cron across distributed nodes
  • Easily produce a scheduled one-time job on the fly
  • Built-in retry mechanism on failure

Installation

# using yarn:
yarn add @siddiqus/crony

# using npm:
npm install --save @siddiqus/crony

Scheduled Jobs

  1. Declare a jobs file for cron jobs
// e.g. jobs/index.ts
import { initialize, CronJobConfig } from '@siddiqus/crony'
import { someService } from './services/some-service'

export const jobs: CronJobConfig[] = [
  {
    disabled: true,
    id: 'basic-job-id',
    jobRunner: async () => {
      console.log(`this won't run because it's disabled`)
    },
    cronTime: '*/2 * * * * *', // Once every 2 seconds
    redlockTtl: '1s',
  },
  {
    id: 'some-other-job-id',
    jobRunner: someService.someJob,
    cronTime: '00 00 06 * * *', // Everyday at 06:00
    redlockTtl: '2m',
  },
]
  1. Initialize the job with a redis client and the jobs array
// e.g. server.ts
import { Crony } from '@siddiqus/crony'
import { jobs } from './jobs'

async function run() {
  await Crony.initialize({
    jobs,
    redisOptions: {
      host: '127.0.0.1',
      port: 6379,
      auth_pass: '',
      db: 1,
    },
    logger: console,
  })
}

Cron Job Configuration

  • Each job in the job configs list has a 'disabled' boolean that can be set by the user
// example disabling a single job with an expression
export const jobs: CronJobConfig[] = [
  {
    disabled: canBeSome === expression, // this one
    id: 'basic-job-id',
    jobRunner: async () => {
      console.log("this won't run because it's disabled")
    },
    cronTime: '*/2 * * * * *', // Once every 2 seconds
    redlockTtl: '1s',
  },
]
  • There is a isEnabledAsyncCb callback parameter for the initialize function. This can be a generic implementation to check some db or data store for cron enabled or not. The following example shows checking if cron is enabled from a database model. (if cron config is not in db, then by default it is enabled)
import { Crony } from '@siddiqus/crony'

Crony.initialize(opts: {
  jobs, // some jobs
  redisOptions, // redis credentials
  logger: console,
  isEnabledAsyncCb: async (jobId: string) => {
    const cronConfig = await CronConfigModel.findById(jobId);
    return cronConfig ? cronConfig.isEnabled : true
  }
})
  • The cronTime is optional, so if it is not declared, the cron job will not run.

Scheduled one-time job

You can also schedule a single job to run at a specific time. Here's an example

Per job config

import { Crony } from '@siddiqus/crony'

Crony.scheduleJob({
  jobName: 'hello-world',
  scheduleAt: new Date('2022-03-01 10:45:00'),
  callback: () => {
    // some stuff
    console.log('running this job at 2022-03-01 10:45:00')
  },
  maxAttempts: 3, // will attempt 3 times total if failed
})

Notes

  • Redis is optional, but should be required if app is deployed using multiple instances

License

Licensed under the UNLICENSED License. Check the LICENSE file for details.