🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

vf-tasks-queue

Package Overview
Dependencies
Maintainers
3
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vf-tasks-queue

Tasks queue module

latest
Source
npmnpm
Version
0.1.0
Version published
Maintainers
3
Created
Source

Tasks Queue

This module is an abstraction of RabbitMQ.

It supports both promises and callbacks.

Install

npm install --save vf-tasks-queue

API

TaskQueueLib.connect(options, [callback])

Returns a task queue, ready to send and process tasks.

Parameters

  • options (Object): Tasks queue configuration
    • prefix (Object): Appends a string to all task types to create a namespace.
      • rabbitmq (Object): Configuration to connect to RabbitMQ server.
        • host (String): Server host.
        • port (String): Server port.
        • user (String): Server user.
        • password (String): Server password.

tasksQueue.send(type, data, [callback])

Stores a new task in the queue.

Parameters

  • type (String): Task type, this is used to match task processors.
  • data (Object): An data object that will be sent to processor when takes this task.

tasksQueue.process(type, processor, [concurrency])

Takes tasks and process them.

Parameters

  • options (String): Options to start processing tasks.
    • type (String): Task type, this is used to fetch only this type of tasks.
    • concurrency (Integer): An integer that indicates the number of tasks that can be processed at the same time. Defaults to 1.
  • processor (Function): A function that will process a task. This function will receive 1 parameter: data. You sould return a promise, otherwise task won't be marked as resolved and will be processed again.

tasksQueue.clearQueue(type, [callback])

Removes all unprocessed tasks of a type.

Parameters

  • type (String): Task type, this is used to remove only this type of tasks.

tasksQueue.close([callback])

Closes the connection with database.

Example:

config.js

module.exports = {
  prefix: 'queue',
  rabbitmq: {
    host: 'localhost'
  }
};

tasks_sender.js

const config = require('config.js');
const tasksQueueConnector = require('vf-tasks-queue');
const taskType = 'test-task';
const taskData = {
  name: 'Chewbacca'
};

tasksQueueConnector.connect(config)
  .then(tasksQueue => {
    tasksQueue.send(taskType, taskData)
  });

tasks_processor.js

const config = require('config.js');
const tasksQueueConnector = require('vf-tasks-queue');
const taskType = 'test-task';

const processor = (data, callback) => {
  console.log(data.name); // Prints "Chewbacca"
  return Promise.resolve();
}

const options = {
  type: taskType,
  concurrency: 1  
};

tasksQueueConnector.connect(config)
  .then(tasksQueue => {
    tasksQueue.process(options, processor);
  });

Tests

npm test

License

MIT

Keywords

tasks

FAQs

Package last updated on 22 Aug 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts