0.1.7 • Published 5 years ago
moleculer-cronjob v0.1.7

The moleculer-cron is the cron based scheduler service for Moleculer
Install
$ npm install moleculer-cronjob --saveUsage
const { ServiceBroker } = require('moleculer')
const CronJob = require('../../index')
// Create broker
const broker = new ServiceBroker()
// Create my conjob service
broker.createService({
name: 'my.cronjob',
mixins: [CronJob],
settings: {
cronTime: '* * * * * *',
},
methods: {
onTick () {
this.logger.info(`Tick`)
}
}
})
// Start broker
broker.start().catch(error => console.log(error))The service uses settings as the cron.СronJob constructor parameters, except for onTick and onComplete, for which the onTick and onComplete methods are used respectively.
The object returned by the cron.СronJob constructor is stored in this.$cronjob property, accessible everywhere within the service's context.
Cronjob stops automatically when the service is stopped, but can also be stopped manually.
const { ServiceBroker } = require('moleculer')
const CronJob = require('../../index')
// Create broker
const broker = new ServiceBroker()
// Create my conjob service
broker.createService({
name: 'my.cronjob',
mixins: [CronJob],
settings: {
cronTime: '*/3 * * * * *',
runOnInit: true
},
metadata: {
ticksCount: 0
},
methods: {
onTick () {
this.logger.info(`Tick #${++this.metadata.ticksCount}`)
if (this.metadata.ticksCount === 5) {
this.$cronjob.stop()
}
},
onComplete () {
this.logger.info('Complete')
}
}
})
// Start broker
broker.start().catch(error => console.log(error))Test
$ npm testIn development with watching
$ npm run ciContribution
Please send pull requests improving the usage and fixing bugs, improving documentation and providing better examples, or providing some testing, because these things are important.
License
The project is available under the MIT license.
Contact
Copyright (c) 2019 Aleksandr Miroshnik
miroshnik@gmail.com