npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-bull

v1.3.8

Published

Create jobs stably, quickly and easily

Downloads

84

Readme

Simple Bull 🐂

Create jobs stably, quickly and easily

Motivation

Bull.js is a great library to programming jobs on NodeJS, it is quite light and occupies an event system that allows manipulating a job in all its stages. But, properly programming a job in Bull is a bit complex.

I made this library in order to reduce this difficulty, but to preserve the bull's functionality and good practices as much as possible.

Why use Simple Bull?

  • 🌟 Very easy to implement
  • 🔄 Stables recurrent jobs
  • 🛰 Remote event jobs
  • 🔎 Great log system

Install

Versions > 1.3.3

npm install --save simple-bull

- Or - 

yarn add simple-bull

Versions < 1.3.3

npm install --save @rokketlabs/simple-bull

- Or - 

yarn add @rokketlabs/simple-bull

Usage

Simple Bull trabaja con colas, las cuales se registran y subscriben en redis, cada Job es una función de Node que se dispara de forma recurrente o remota.

Redis Connection

Simple Bull es muy flexible a la hora de configurar la conexión a redis. Además reusa y deja cerrada las conexiones para garantizar un uso correcto del Redis.

Configuración con Variables de Ambiente

Puedes configurar por medio de variables de ambiente o archivos .env los parámetros de conexión de tu Redis.

# You have 2 ways to stablish connection with your redis

# Redis connection params without SSL
export REDIS_HOST='localhost'
export REDIS_PORT='13376'
export REDIS_PASSWORD='youpass'  [optional]
export REDIS_USERNAME='username' [optional]

# Or, Redis url without ssl
export REDIS_STRING_URL='redis://username:password@host:port'

# If your connection uses SSL you can use the two previous ways
export REDIS_HOST='localhost'
export REDIS_PORT='13376'
export REDIS_PASSWORD='youpass'  
export REDIS_USERNAME='username' 
export REDIS_SSL=true

# Redis connection params with SSL
export REDIS_STRING_URL='rediss://username:password@host:port'
Configuración desde el código

Alternativamente también puedes configurar los parámetros de conexión al Redis al momento de inicializar una cola de la siguiente forma:

import { initQueue } from '@rokketlabs/simple-bull';

initQueue({
  queueName: 'email-queue',
  jobs: [sendEmail, checkEmail],
  redis: {
    host: 'your-redis-host',
    port: 'your-redis-url',
    username: 'username',
    password: 'username',
    ssl: true || false
  }
});

initQueue({
  queueName: 'email-queue',
  jobs: [sendEmail, checkEmail],
  redis: {
    url: 'redis://username:password@host:port',
  }
});

Registro de colas

import { initQueue } from '@rokketlabs/simple-bull';

initQueue({
  queueName: 'email-queue',
  jobs: [sendEmail, checkEmail]
});

Se registra la cola email-queue en redis cada vez que el servidor se levanta, esta cola se encarga de orquestar los jobs sendEmail y checkEmail.

Recurrent Jobs

import checkEmail from './checkEmail';

export default {
  name: 'checkEmail',    // Nombre del job
  processor: checkEmail, // Función que se ejecuta el job
  frequency: 5000,       // Recurrencia en ms del job
  delay: 1000,           // Tiempo de espera
  limit: 10000           // Límite de repeticiones del job
};

Los jobs recurrentes ejecutan una función cada cierto tiempo, en este ejemplo, el job ejecuta una función que revisa emails, se ejecuta cada 5 segundos y espera 1 segundo antes de comenzar la ejecución.

Event Jobs

import sendEmail from './sendEmail';

export default {
  name: 'sendEmail', // Nombre del job
  processor: sendEmail // Función que se desea ejecutar
};

Cuando un job no tiene el parametro frequency, simple-bull lo deja disponible para ser llamado de forma remota.

import { fireJob } from '@rokketlabs/simple-bull';

await fireJob({
  queueName: 'email-queue',
  jobName: 'send-email',
  data: { email: '[email protected]', message: 'Hi' }
});

Para dispararlo de forma remota desde otro servidor, configurar las variables de ambiente para apuntar al mismo redis, y usar la función fireJob la cual deja un evento en cola.

Custom Hooks

En algunos casos es necesario tener control de lo que ocurre luego de la ejecución de un job, simple-bull resuelve esto en dos niveles

Queue Hooks

import { initQueue } from '@rokketlabs/simple-bull';

const onSuccess = ({ result, job, successMessage }) => {}
const onFail =  ({ err, job, errorMessage }) => {}

initQueue({
  queueName: 'email-queue',
  onSuccess,
  onFail,
  jobs: [sendEmail, checkEmail]
});

Cada vez que sendEmail y checkEmail se hayan ejecutado, alguno de los dos hooks se dispara y con esto puedes realizar acciones después de la ejecución.

Job Hooks

import checkEmail from './checkEmail';

const onSuccess = ({ result, job, successMessage }) => {}
const onFail =  ({ err, job, errorMessage }) => {}

export default {
  name: 'checkEmail',    // Nombre del job
  processor: checkEmail, // Función que se ejecuta el job
  frequency: 5000,       // Recurrencia en ms del job
  delay: 1000,           // Tiempo de espera
  limit: 10000,          // Límite de repeticiones del job
  onSuccess,
  onFail
};

Cada vez que checkEmail se ejecute, alguno de los dos hooks se dispara y con esto puedes realizar acciones después de la ejecución de este job específico.

Important Notes:

Cada vez que utilizas alguno de los dos hooks, simple-bull dejará de mostrar los logs por defecto de exito o error en el job o la cola que hayas activado los hooks, dado que le pasa el contro a tus funciones, tu te tienes que encargar de loggear correctamente el resultado de las funciones.

Loggers

Las funciones successMessage y errorMessage son loggers que tienen un estilo mucho más legible para usarlas necesitan los siguientes parámetros:

successMessage(job: JobObject, message: string)
errorMessage(job: JobObject, message: string)

TODO:

  • [x] Reuse Redis connections.
  • [ ] Test Implementation.
  • [ ] Test Coverage.
  • [ ] English Documentation [WIP].
  • [ ] Redis Cluster connection.
  • [ ] Concurrent Jobs.
  • [ ] timeLimit implementation.
  • [ ] onProgress Hook.

Hecho con:

  • Bull - Bull jobs library

Autor

License

Simple Bull lincence it's MIT, happy coding ;) Roberto Zibert ©