davidroman0O / moleculer-cron

Moleculer Addons - Cron tasks
MIT License
38 stars 25 forks source link
cron moleculer

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:

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

Cron Ranges

API

Job Configuration

Mixin Methods

Job Object Methods

Utility Methods

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.