{"id":13651502,"url":"https://github.com/davidroman0O/moleculer-cron","last_synced_at":"2025-04-22T22:31:24.371Z","repository":{"id":53099658,"uuid":"121531026","full_name":"davidroman0O/moleculer-cron","owner":"davidroman0O","description":"Moleculer Addons - Cron tasks","archived":false,"fork":false,"pushed_at":"2024-07-19T17:02:04.000Z","size":653,"stargazers_count":41,"open_issues_count":4,"forks_count":25,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-05T14:31:56.319Z","etag":null,"topics":["cron","moleculer"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/davidroman0O.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-14T16:08:16.000Z","updated_at":"2025-01-07T23:57:28.000Z","dependencies_parsed_at":"2024-03-03T00:31:15.334Z","dependency_job_id":"4db25bb0-96ff-4fd8-980f-83b3db272470","html_url":"https://github.com/davidroman0O/moleculer-cron","commit_stats":{"total_commits":40,"total_committers":7,"mean_commits":5.714285714285714,"dds":0.7,"last_synced_commit":"52e56ea89f37a006ff16d9f26cc6e0722501771f"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidroman0O%2Fmoleculer-cron","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidroman0O%2Fmoleculer-cron/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidroman0O%2Fmoleculer-cron/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidroman0O%2Fmoleculer-cron/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davidroman0O","download_url":"https://codeload.github.com/davidroman0O/moleculer-cron/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248108028,"owners_count":21049057,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cron","moleculer"],"created_at":"2024-08-02T02:00:50.079Z","updated_at":"2025-04-22T22:31:24.074Z","avatar_url":"https://github.com/davidroman0O.png","language":"JavaScript","funding_links":[],"categories":["Mixins"],"sub_categories":["Tasks, Queues and Jobs"],"readme":"# moleculer-cron [![NPM version](https://img.shields.io/npm/v/moleculer-cron.svg)](https://www.npmjs.com/package/moleculer-cron)\n\nCron mixin for Moleculer using [cron](https://www.npmjs.com/package/cron).\n\n## Description\n\nEasy to use cron with Moleculer!\n\n## Install\n\n```bash\n$ npm install moleculer-cron --save\n```\n\n## Usage\n\n### Create cron service\n\nSpecify all of your cron tasks inside the `settings.cronJobs` array of the service.\n\n```js\nconst CronMixin = require(\"moleculer-cron\");\n\nbroker.createService({\n    name: \"cron-job\",\n\n    mixins: [CronMixin],\n\n    settings: {\n        cronJobs: [\n            {\n                name: \"jobHelloWorld\",\n                cronTime: '*/5 * * * * *', // Run every 5 seconds\n                manualStart: true, // This job needs to be started manually\n                onTick: async function() {\n                    this.logger.info('JobHelloWorld ticked');\n                    try {\n                        const data = await this.broker.call(\"cron-job.say\");\n                        this.logger.info(\"Oh!\", data);\n                        \n                        // Stop this job and start the other one\n                        this.stopJob(\"jobHelloWorld\");\n                        this.startJob(\"jobToggle\");\n                        this.logger.info(\"Stopped JobHelloWorld and started JobToggle\");\n                    } catch (e) {\n                        this.logger.info(\"error \", e);\n                    }\n                },\n                onInitialize: function() {\n                    this.logger.info(\"JobHelloWorld is init\");\n                    // This job is manual start, so it won't start automatically\n                },\n                onComplete: function() {\n                    this.logger.info(\"JobHelloWorld is stopped\");\n                }\n            },\n            {\n                name: \"jobToggle\",\n                cronTime: '*/5 * * * * *', // Run every 5 seconds\n                onTick: function() {\n                    this.logger.info('JobToggle ticked');\n                    \n                    // Stop this job and start the other one\n                    this.stopJob(\"jobToggle\");\n                    this.startJob(\"jobHelloWorld\");\n                    this.logger.info(\"Stopped JobToggle and started JobHelloWorld\");\n                },\n                onInitialize: function() {\n                    this.logger.info(\"JobToggle is init\");\n                    // This job will start automatically\n                },\n                onComplete: function() {\n                    this.logger.info(\"JobToggle is stopped\");\n                }\n            }\n        ]\n    },\n\n    actions: {\n        say: {\n            handler() {\n                return \"HelloWorld!\";\n            }\n        },\n    }\n});\n```\n\n## Available Cron patterns:\n\n- Asterisk. E.g. *\n- Ranges. E.g. 1-3,5\n- Steps. E.g. */2\n\n[Read up on cron patterns here](http://crontab.org). Note that this library uses six fields, with 1 second as the finest granularity.\n\n## Cron Ranges\n\n- Seconds: 0-59\n- Minutes: 0-59\n- Hours: 0-23\n- Day of Month: 1-31\n- Months: 0-11 (Jan-Dec)\n- Day of Week: 0-6 (Sun-Sat)\n\n## API\n\n### Job Configuration\n\n- `name` - [REQUIRED] - Set a name for the job.\n- `cronTime` - [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS [Date](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date) object.\n- `manualStart` - [OPTIONAL] - Specifies whether to start the job just before exiting the constructor. Default is false.\n- `timeZone` - [OPTIONAL] - Specify the timezone for the execution. Check all timezones available at [Moment Timezone Website](http://momentjs.com/timezone/).\n- `onInitialize` - [OPTIONAL] - Executed before the cron job is created.\n- `onStart` - [OPTIONAL] - When the cron is starting.\n- `onStop` - [OPTIONAL] - When the cron is stopping.\n- `onComplete` - [OPTIONAL] - A function that will fire when the job is stopped.\n- `onTick` - [REQUIRED] - The function to fire at the specified time.\n\n### Mixin Methods\n\n- `startJob(jobName)` - Starts the specified job.\n- `stopJob(jobName)` - Stops the specified job.\n- `getJob(jobName)` - Returns the job object for the specified job name.\n\n### Job Object Methods\n\n- `startJob()` - Starts the job.\n- `stopJob()` - Stops the job.\n- `lastDate()` - Returns the last execution date of the job.\n- `running()` - Returns whether the job is currently running.\n- `setTime(time)` - Changes the time for the job. `time` can be a cron string or a Date object.\n- `nextDates(count)` - Returns an array of the next `count` dates that the job will run.\n- `addCallback(callback)` - Adds an additional callback function to be executed when the job ticks.\n\n### Utility Methods\n\n- `getCronTime(time)` - Returns a CronTime instance for the given time.\n\n## Notes\n\nFor any issues or feature requests, please create an issue on the GitHub repository. Make sure to search existing issues before creating a new one.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidroman0O%2Fmoleculer-cron","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidroman0O%2Fmoleculer-cron","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidroman0O%2Fmoleculer-cron/lists"}