moleculer-cronjob

0.1.7 • Public • Published

Moleculer logo

NPM version Build Status Coverage Status Codacy Badge Code Climate David Known Vulnerabilities

The moleculer-cron is the cron based scheduler service for Moleculer

Install

$ npm install moleculer-cronjob --save

Usage

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 test

In development with watching

$ npm run ci

Contribution

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

Package Sidebar

Install

npm i moleculer-cronjob

Weekly Downloads

185

Version

0.1.7

License

MIT

Unpacked Size

20 kB

Total Files

14

Last publish

Collaborators

  • miroshnik