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

long-cron

v1.0.0

Published

A cron runner for long running processes that implements a simple strategy to prevent running the same job multiple times

Downloads

8

Readme

Why

The Cron module is amazing! No doubt!

But sometimes you want to use it and at the same time prevent running jobs until the last run has finished. This is a tricky thing for heavy computational jobs where the completion time cannot be precisely determined prior to running the job.

This module is a thin wrapper on top of Cron that manages a map of running processes on redis and only allows the cron function to run once the last job is done.

Additionally, it adds a feature called Wait Cycles that allows job keys to be expired after a job has taken longer that cron-time x 10 (or whatever value you set)

Finally I threw in the Human to Cron package because I prefer to work with human-readable crons.

To use

yarn add long-cron

Then...

    
    //Require
    const Cron = require('long-cron');
    
    //Initialize with options
    const options = {
        //How long to wait to clear keys for hung jobs
        //One cycle is equal to your cron duration/interval
        waitCycles: 10,

        // You can optionally pass your own redis (client) interval
        // This module uses ioredis by default
        // redis: YOUR REDIS INSTANCE,

        //You can set your time zone here. Refer to the cron module
        timeZone: 'America/Los_Angeles',
        
        //Optional prefix for your redis keys. The default is as shown below
        redisKeyPrefix: "long-cron:cache:"
    }
    
  
    const cron = new Cron(options);
    
    // now run your cron!
    // pass the duration in human (see human-to-cron module) or as a cron pattern

    cron.start('each 1 seconds', async function() {
    
        console.time("Long Cron");
        console.log('Running Cron Job');
    
        await new Promise((resolve, reject) => {
            setTimeout(resolve, 5000);
        });
    
        console.timeEnd("Long Cron");
    
    
    })

This will output the following...

    Running Cron Job
    Long Cron: 5.003s
    Running Cron Job
    Long Cron: 5.000s
    Running Cron Job
    Long Cron: 5.001s
    Running Cron Job
    Long Cron: 5.001s

As shown in the output, even though the cron job is set to run every one second we actually only run the function once the last job has returned (after 5 seconds).

If this job was to hung, the keys preventing it to run would automatically expire after 1x10 seconds.

It therefore means that when callig long running processes, set your cron interval and waitCycles to values that are reasonable based on the task at hand.

Enjoy!