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

moleculer-cron

v0.0.7

Published

Mixin for Node-Cron

Downloads

6,603

Readme

moleculer-cron NPM version

Cron mixin for Moleculer using cron.

Description

Easy to use cron with Moleculer!

Install

$ npm install moleculer-cron --save

Usage

Create cron service

Specify all of your cron tasks inside the settings.cronJobs array of the service.

const CronMixin = require("moleculer-cron");

broker.createService({
    name: "cron-job",

    mixins: [CronMixin],

    settings: {
        cronJobs: [
            {
                name: "jobHelloWorld",
                cronTime: '*/5 * * * * *', // Run every 5 seconds
                manualStart: true, // This job needs to be started manually
                onTick: async function() {
                    this.logger.info('JobHelloWorld ticked');
                    try {
                        const data = await this.broker.call("cron-job.say");
                        this.logger.info("Oh!", data);
                        
                        // Stop this job and start the other one
                        this.stopJob("jobHelloWorld");
                        this.startJob("jobToggle");
                        this.logger.info("Stopped JobHelloWorld and started JobToggle");
                    } catch (e) {
                        this.logger.info("error ", e);
                    }
                },
                onInitialize: function() {
                    this.logger.info("JobHelloWorld is init");
                    // This job is manual start, so it won't start automatically
                },
                onComplete: function() {
                    this.logger.info("JobHelloWorld is stopped");
                }
            },
            {
                name: "jobToggle",
                cronTime: '*/5 * * * * *', // Run every 5 seconds
                onTick: function() {
                    this.logger.info('JobToggle ticked');
                    
                    // Stop this job and start the other one
                    this.stopJob("jobToggle");
                    this.startJob("jobHelloWorld");
                    this.logger.info("Stopped JobToggle and started JobHelloWorld");
                },
                onInitialize: function() {
                    this.logger.info("JobToggle is init");
                    // This job will start automatically
                },
                onComplete: function() {
                    this.logger.info("JobToggle is stopped");
                }
            }
        ]
    },

    actions: {
        say: {
            handler() {
                return "HelloWorld!";
            }
        },
    }
});

Available Cron patterns:

  • Asterisk. E.g. *
  • Ranges. E.g. 1-3,5
  • Steps. E.g. */2

Read up on cron patterns here. Note that this library uses six fields, with 1 second as the finest granularity.

Cron Ranges

  • Seconds: 0-59
  • Minutes: 0-59
  • Hours: 0-23
  • Day of Month: 1-31
  • Months: 0-11 (Jan-Dec)
  • Day of Week: 0-6 (Sun-Sat)

API

Job Configuration

  • name - [REQUIRED] - Set a name for the job.
  • cronTime - [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS Date object.
  • manualStart - [OPTIONAL] - Specifies whether to start the job just before exiting the constructor. Default is false.
  • timeZone - [OPTIONAL] - Specify the timezone for the execution. Check all timezones available at Moment Timezone Website.
  • onInitialize - [OPTIONAL] - Executed before the cron job is created.
  • onStart - [OPTIONAL] - When the cron is starting.
  • onStop - [OPTIONAL] - When the cron is stopping.
  • onComplete - [OPTIONAL] - A function that will fire when the job is stopped.
  • onTick - [REQUIRED] - The function to fire at the specified time.

Mixin Methods

  • startJob(jobName) - Starts the specified job.
  • stopJob(jobName) - Stops the specified job.
  • getJob(jobName) - Returns the job object for the specified job name.

Job Object Methods

  • startJob() - Starts the job.
  • stopJob() - Stops the job.
  • lastDate() - Returns the last execution date of the job.
  • running() - Returns whether the job is currently running.
  • setTime(time) - Changes the time for the job. time can be a cron string or a Date object.
  • nextDates(count) - Returns an array of the next count dates that the job will run.
  • addCallback(callback) - Adds an additional callback function to be executed when the job ticks.

Utility Methods

  • getCronTime(time) - Returns a CronTime instance for the given time.

Notes

For any issues or feature requests, please create an issue on the GitHub repository. Make sure to search existing issues before creating a new one.