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

ha-job-scheduler

v0.5.0

Published

Highly available cron job scheduler using Redis

Downloads

256

Readme

HA Job Scheduler

Highly available cron job scheduling using node-schedule and Redis.

Designed to be used in an environment with redundant schedulers. Only one scheduler will ever successfully run the cron job.

Previously missed invocations can be run by passing a non-zero value for persistScheduledMs to scheduleRecurring. This will persist invocations to Redis which can be also useful for debugging.

Works well with nats-jobs

scheduleRecurring

scheduleRecurring(
  id: string,
  rule: Rule,
  runFn: RunFn,
  options?: RecurringOptions
) => GracefulShutdown

Schedule a recurring job. runFn will be called for every invocation of the rule.

Set persistScheduledMs to a value greater than the frequency of the cron rule to guarantee that the last missed job will be run. This is useful for infrequent jobs that cannot be missed. For example, if you have a job that runs at 6am daily, you might want to set persistScheduledMs to ms('25h') so that a missed run will be attempted up to one hour past the scheduled invocation.

Guarantees at most one delivery.

import { jobScheduler } from 'ha-job-scheduler'
import ms from 'ms'

const scheduler = jobScheduler()
const runFn = (date: Date) => {
  console.log(date)
}
const { stop } = scheduler.scheduleRecurring(
  'everyMinute',
  '* * * * *',
  runFn,
  { persistScheduledMs: ms('1h') }
)
// Gracefully handle signals
const shutDown = async () => {
  await stop()
  process.exit(0)
}
process.on('SIGTERM', shutDown)
process.on('SIGINT', shutDown)

scheduleDelayed

Schedule data to be delivered at a later date. Duplicate payloads will be ignored. scheduleFor accepts a number of milliseconds in the future or a date. Use in conjunction with runDelayed.

Returns a boolean indicating if the item was successfully scheduled.

scheduleDelayed(
  id: string,
  data: Uint8Array,
  scheduleFor: number | Date
) => Promise<boolean>
// Schedule for the future
for (let i = 1; i <= 3; i++) {
  await scheduler.scheduleDelayed(
    'orders',
    `delayed data ${i}`,
    ms(`${i * 10}s`)
  )
}

runDelayed

Check for delayed items according to the recurrence rule. Default interval is every minute. Calls runFn for the batch of items where the delayed timestamp is <= now. The default number of items to retrieve at one time is 100.

The id parameter should match the id passed to scheduleDelayed.

Guarantees at least one delivery.

runDelayed(
  id: string,
  runFn: DelayedFn,
  options?: DelayedOptions
) => GracefulShutdown
// Do something with scheduled jobs
scheduler.runDelayed('orders', async (values) => {
  console.log('Running delayed for', values)
})