All Projects → Rocketseat → Adonis Bull

Rocketseat / Adonis Bull

Licence: mit
The easiest way to start using an asynchronous job queue with AdonisJS. Ready for Adonis v5 ⚡️

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Adonis Bull

Spring Boot
spring-boot 项目实践总结
Stars: ✭ 989 (+458.76%)
Mutual labels:  schedule, redis
Databay
Databay is a Python interface for scheduled data transfer. It facilitates transfer of (any) data from A to B, on a scheduled interval.
Stars: ✭ 175 (-1.13%)
Mutual labels:  schedule
Ketchup
ketchup (番茄酱) 是一个基于dotnet core的微服务框架。
Stars: ✭ 170 (-3.95%)
Mutual labels:  redis
Symfony Ddd Wishlist
Wishlist, a sample application on Symfony 3 and Vue.js built with DDD in mind
Stars: ✭ 172 (-2.82%)
Mutual labels:  redis
Nginx Helper
Nginx Helper for WordPress caching, permalinks & efficient file handling in multisite
Stars: ✭ 170 (-3.95%)
Mutual labels:  redis
Spoon
🥄 A package for building specific Proxy Pool for different Sites.
Stars: ✭ 173 (-2.26%)
Mutual labels:  redis
1backend
Run your web apps easily with a complete platform that you can install on any server. Build composable microservices and lambdas.
Stars: ✭ 2,024 (+1043.5%)
Mutual labels:  redis
Redis Cli
A pure go implementation of redis-cli.
Stars: ✭ 175 (-1.13%)
Mutual labels:  redis
Owllook
owllook-小说搜索引擎
Stars: ✭ 2,163 (+1122.03%)
Mutual labels:  schedule
Leaguestats
📈 League of Legends Stats Web App
Stars: ✭ 172 (-2.82%)
Mutual labels:  redis
Seckill
基于Spring Boot的高性能秒杀系统
Stars: ✭ 171 (-3.39%)
Mutual labels:  redis
Lad
👦 Lad is the best Node.js framework. Made by a former Express TC and Koa team member.
Stars: ✭ 2,112 (+1093.22%)
Mutual labels:  redis
Spring Examples
Spring Examples
Stars: ✭ 172 (-2.82%)
Mutual labels:  redis
Xpcms
基于node的cms系统, 后端采用node+koa+redis,并通过本人封装的redis库实现数据操作,前端采用vue+ts+vuex开发,后台管理系统采用react全家桶开发
Stars: ✭ 170 (-3.95%)
Mutual labels:  redis
Operators
Collection of Kubernetes Operators built with KUDO.
Stars: ✭ 175 (-1.13%)
Mutual labels:  redis
Source Code Hunter
😱 从源码层面,剖析挖掘互联网行业主流技术的底层实现原理,为广大开发者 “提升技术深度” 提供便利。目前开放 Spring 全家桶,Mybatis、Netty、Dubbo 框架,及 Redis、Tomcat 中间件等
Stars: ✭ 7,392 (+4076.27%)
Mutual labels:  redis
Proxy pool
Python爬虫代理IP池(proxy pool)
Stars: ✭ 13,964 (+7789.27%)
Mutual labels:  redis
Aioredlock
🔒 The asyncio implemetation of Redis distributed locks
Stars: ✭ 171 (-3.39%)
Mutual labels:  redis
Ansible Role Redis
Ansible Role - Redis
Stars: ✭ 176 (-0.56%)
Mutual labels:  redis
Spring Boot Plus
🔥 Spring-Boot-Plus is a easy-to-use, high-speed, high-efficient,feature-rich, open source spring boot scaffolding. 🚀
Stars: ✭ 2,198 (+1141.81%)
Mutual labels:  redis

Adonis Bull

A Bull provider for AdonisJS Adonis Bull provides an easy way to start using Bull.

build-image coveralls-image license-image npm-image


This documentation refers to the stable version of Adonis Bull, for Adonis v4.x
> If you are using Adonis v5, click here.

Why

Using Bull with Adonis shouldn't be hard. It shouldn't require dozens of steps to configure it. That's why adonis-bull exists. It provides an easy way to use queues when developing applications with AdonisJS.

Install

adonis install @rocketseat/adonis-bull

Usage

Register the Bull commands at start/app.js

const aceProviders = ['@rocketseat/adonis-bull/providers/Command']

Register the Bull provider at start/app.js

const providers = [
  //...
  '@rocketseat/adonis-bull/providers/Bull',
]

Create a file with the jobs that will be processed at start/jobs.js:

module.exports = ['App/Jobs/UserRegisterEmail']

Add the config file at config/bull.js:

'use strict'

const Env = use('Env')

module.exports = {
  // redis connection
  connection: Env.get('BULL_CONNECTION', 'bull'),
  bull: {
    redis: {
      host: '127.0.0.1',
      port: 6379,
      password: null,
      db: 0,
      keyPrefix: '',
    },
  },
  remote: 'redis://redis.example.com?password=correcthorsebatterystaple',
}

In the above file you can define redis connections, there you can pass all Bull queue configurations described here.

Create a file to initiate Bull at preloads/bull.js:

const Bull = use('Rocketseat/Bull')

Bull.process()
  // Optionally you can start BullBoard:
  .ui(9999) // http://localhost:9999
// You don't need to specify the port, the default number is 9999

Add .preLoad in server.js to initialize the bull preload

new Ignitor(require('@adonisjs/fold'))
  .appRoot(__dirname)
  .preLoad('preloads/bull') // Add This Line
  .fireHttpServer()
  .catch(console.error)

Creating your job

Create a class that mandatorily has the methods key and handle.

The key method is the unique identification of each job. It has to be a static get method.

The handle is the method that contains the functionality of your job.

const Mail = use('Mail')

class UserRegisterEmail {
  static get key() {
    return 'UserRegisterEmail-key'
  }

  async handle(job) {
    const { data } = job // the 'data' variable has user data

    await Mail.send('emails.welcome', data, (message) => {
      message
        .to(data.email)
        .from('<from-email>')
        .subject('Welcome to yardstick')
    })

    return data
  }
}

module.exports = UserRegisterEmail

You can use the connection static get method to specify which connection your job will work.

class UserRegisterEmail {
  // ...
  static get connection() {
    return 'remote'
  }
}

Events

The package has support for all events triggered in the bull, just add "on" and complete with the name of the event Ex: onCompleted(), onActive(), onWaiting() and etc.

class UserRegisterEmail {
  ...
  onCompleted(job, result) {}
  onActive(job) {}
  ...
}

module.exports = UserRegisterEmail;

Processing the jobs

Simple job

You can share the job of any controller, hook or any other place you might like:

const User = use('App/Models/User')
const Bull = use('Rocketseat/Bull')
const Job = use('App/Jobs/UserRegisterEmail')

class UserController {
  store ({ request, response }) {
    const data = request.only(['email', 'name', 'password'])

    const user = await User.create(data)


    Bull.add(Job.key, user)
  }
}

module.exports = UserController

Scheduled job

Sometimes it is necessary to schedule a job instead of shooting it imediately. You should use schedule for that:

const User = use('App/Models/User')
const Bull = use('Rocketseat/Bull')
const Job = use('App/Jobs/HolidayOnSaleEmail')

class HolidayOnSaleController {
  store ({ request, response }) {
    const data = request.only(['date', 'product_list']) // 2019-11-15 12:00:00

    const products = await ProductOnSale.create(data)


    Bull.schedule(Job.key, products, data.date)
  }
}

module.exports = HolidayOnSaleController

This job will be sent only on the specific date, wich for example here is on November 15th at noon.

When finishing a date, never use past dates because it will cause an error.

other ways of using schedule:

Bull.schedule(key, data, new Date('2019-11-15 12:00:00'))
Bull.schedule(key, data, '2 hours') // 2 hours from now
Bull.schedule(key, data, 60 * 1000) // 1 minute from now.

Advanced jobs

You can use the own Bull configs to improve your job:

Bull.add(key, data, {
  repeat: {
    cron: '0 30 12 * * WED,FRI',
  },
})

This job will be run at 12:30 PM, only on wednesdays and fridays.

Exceptions

To have a bigger control over errors that might occur on the line, the events that fail can be manipulated at the file App/Exceptions/QueueHandler.js:

const Sentry = use('Sentry')

class QueueHandler {
  async report(error, job) {
    Sentry.configureScope((scope) => {
      scope.setExtra(job)
    })

    Sentry.captureException(error)
  }
}

module.exports = QueueHandler

Contributing

Thank you for being interested in making this package better. We encourage everyone to help improve this project with new features, bug fixes, or performance improvements. Please take a little bit of your time to read our guide to make this process faster and easier.

Contribution Guidelines

To understand how to submit an issue, commit and create pull requests, check our Contribution Guidelines.

Code of Conduct

We expect you to follow our Code of Conduct. You can read it to understand what kind of behavior will and will not be tolerated.

License

MIT License © Rocketseat

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].