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

node-cron-management

v0.1.5

Published

Improvement of node-cron package

Downloads

51

Readme

Node Cron Management

npm npm npms.io (final) npms.io (final) GitHub tag (latest by date)

This package is a package to manage cron-jobs in a node.js Typescript application. It is built using node-cron and reflect-metadata packages

Installation

npm i node-cron-management

Usage

The package works with decorators and hance the following lines should be in your tsconfig.json file

"experimentalDecorators": true,
"emitDecoratorMetadata": true,

Annotate your Job class with @cronGroup and annotate each handler with @cronJob

import { cronGroup, cronJob, CronManager } from "node-cron-management";

@cronGroup('jobs')
class JobService{
    constructor(private name:string){}

    @cronJob('*/1 * * * * *', 'print_name_func')
    printName(){
        console.log(this.name)
    }
}

@cronGroup('auth')
class AuthService{
    constructor(private users: any[]){}

    @cronJob('* * * * * *', 'my_urban_func', 'Asia/Makassar')
    unbanUser(){
        console.log(`There are ${this.users.length} users in the application`)
    }
}

const manager = new CronManager();

const jobService = new JobService('JobService');
const authJob = new AuthService([])

manager.register(JobService, jobService)
manager.register(AuthService,authJob)

manager.startAll() //starts all jobs
manager.stopAll() //stops all jobs

//starting and stopping for specific groups
manager.startGroup('jobs') //starts only jobs in the class with tag equal to jobs
manager.stopGroup('jobs') //stops only jobs in the class with grouptag equal to jobs

//starting and stopping for specific handlers or jobs
manager.startHandler('my_urban_func') //starts the single job with tag equal to my_urban_func
manager.stopHandler('my_urban_func') //stop the single job with tag equal to my_urban_func

manager.getGroups() // returns ['auth','jobs']
manager.getHandlers() //returns ['my_urban_func','print_name_func']

Methods

  • @cronGroup( groupTag? : string )
    params
    • groupTag: tag to uniquely identify a a set of jobs. The same groupTag can be used for multiple classes.
  • @cronJob( cronExpression: string, handlerTag?: string, timezone?: string)
    params
    • cronExpression: Expression describing the cron interval. The package uses node-cron in the background and hence the expression description is the same.
    • handlerTag: unique string to identify a single job. The job can be started and stopped using that tag.
    • timezone: yhe timezone that is used for job scheduling. See moment-timezone for valid values.
  • manager.register(Class:Function, instance: any)
    It registers an instance of a class to the cron job manager.
    params
    • Class: The class we're registering
    • instance: An instance of that class