Giter VIP home page Giter VIP logo

moleculer-bullmq's Introduction

moleculer-bullmq

Coverage Status Codacy Badge Maintainability David Downloads

How to create job

You just need to add the BullMqMixin and add a queue attribute to you action.
This action will be call with the params & meta of the scheduler.
The return of the action will be the job result.
The mixin will add the BullMq job into locals

module.exports = {
  name: 'jobs',
  mixins: [BullMqMixin],
  settings: {
    bullmq: {
      worker: { concurrency: 50 }
    }
  },
  actions: {
    resize: {
      queue: true,
      params: { width: 'number', height: 'number' },
      async handler(ctx) {
        const { width, height } = ctx.params
        const { user } = ctx.meta
        ctx.locals.job.updateProgress(100)
        return { user, size: width * height, job: ctx.locals.job.id }
      }
    }
  }
}

By default it use the redis cacher, but you can specify a custom client :

module.exports = {
  name: 'jobs',
  mixins: [BullMqMixin],
  settings: {
    bullmq: {
      client: 'redis://:[email protected]:6380/4'
    }
  }
}
The `client` option goes to the `IORedis` constructor.

How to queue job

You can use the queue method, with five parameters : Current context, Queue name, Action name, Parameters, Job options

module.exports = {
  name: 'my.service',
  mixins: [BullMqMixin],
  actions: {
    'resize.async': {
      async handler(ctx) {
        ctx.meta.user = 'Bob de glace'
        const job = await this.queue(ctx, 'jobs', 'resize', { width: 42, height: 42 }, { priority: 10 })
      }
    }
  }
}

If your in the same service as your scheduling action, you can omit the queue name with the localQueue method

module.exports = {
  name: 'my.service',
  mixins: [BullMqMixin],
  actions: {
    resize: {
      queue: true,
      params: { width: 'number', height: 'number' },
      async handler(ctx) {
        const { width, height } = ctx.params
        const { user } = ctx.meta
        ctx.locals.job.updateProgress(100)
        return { user, size: width * height, job: ctx.locals.job.id }
      }
    },
    'resize.async': {
      async handler(ctx) {
        ctx.meta.user = 'Bob de glace'
        const job = await this.localQueue(ctx, 'resize', { width: 42, height: 42 }, { priority: 10 })
      }
    }
  }
}

moleculer-bullmq's People

Contributors

hugome avatar kesslerdev avatar yanndebelgique avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.