OpenPaaS Documentation logo OpenPaaS Documentation

Table of contents

Overview

In OpenPaaS ESN system, there are scenarios or tasks which consume a lot of system resources such as CPU time, memory, network bandwidth or IO. For example: reindex elasticsearch indices, import contacts and events from files or social accounts, send alarm emails to calendar event attendees, etc.

Processing such tasks in NodeJS becomes even more challenging due to its single threaded and if you try to run them in parallel, system might become laggy or even crash.

Solution to handle such tasks is to build a queue of tasks and process each task one by one so that each task is given good enough system resources.

linagora.esn.jobqueue is a OpenPaaS ESN core module which base on Kue.This module provides APIs to create and manage queue job in OpenPaaS ESN.

How it works?

Register a worker

The worker object to register is defined as:


const jobName = 'contact-import';

dependencies('jobqueue').lib.addWorker({
  name: jobName,
  handler: {
    handle
    getTitle
  }
})

function handle(job) {
  const { user, account } = job.data;

  // must return a promise
  return importContact(user, account);
}

function getTitle(jobData) {
  return `Import ${jobData.type} contacts for user ${jobData.user._id}`;
}

Calling a worker to do his job

Once registered, you can call worker job by his name and data:


const jobData = { user, account };

dependencies('jobqueue').lib.submitJob(jobName, jobData);

Note that the jobData must be as lightweight as possible since it is stored in Redis

Job object

To get job object by his id:


dependencies('jobqueue').lib.getJobById(jobId);