Skip to content

📆 The cron-based scheduler service for Moleculer framework

License

Notifications You must be signed in to change notification settings

miroshnik/moleculer-cronjob

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

📆 The cron-based scheduler service for Moleculer framework

Resources

License

Stars

Watchers

Forks

Packages