0.1.0 • Published 4 years ago

strapi-plugin-bull v0.1.0

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
4 years ago

strapi-plugin-bull

Build Status

:construction_worker: Strapi plugin to support background jobs thanks to the bull the queue engine.

  • Simpe, thought for prototyping.
  • Generic enough to support different kind of applications.
  • GraphQL.
  • Job inputs, results and metadata storage in MongoDB.
  • Jobs info download in JSON format.
  • Worker implementation helper.
    • Databases and query connections management.
    • Job status update.
    • Bug reporting to Slack.

Once the plugin is added to your application a new collection Jobs (jobs_Job in the database) will appear and a new plugin with the same name.

Strapi panel

Collection fields

job fields

To learn about Strapi plugins you can visit the official documentation.

Note: This component is in beta (v0.x.x), so please check the CHANGELOG to learn about any change in your code.

Install & use

:pizza: Inside a project folder.

npm i --save strapi-plugin-bull
npm i --save-dev axios # temporal workaround
npm run build

Environment

The plugin rely on external services, so it expects to find the next environment variables.

  • DATABASE_URI: MongoDB URI, as the main database engine. For now we don't support the Strapi in-memory provider (useful to develop).
  • QUEUE_URI: The queue (Redis) connection string for the queue.

DB setup

A new model Job is included, so you need to add the next entry in your User.settings.json.

"jobs": {
  "collection": "job",
  "via": "owner",
  "plugin": "users-permissions"
}

Input checks

Any object is supported as input.

Optionally, you can define a global policy to check the input field of the jobs. The file needs to be called isValid.js. In example:

module.exports = async (ctx, next) => {
  const { input } = ctx.request.body; // marked as required

  // TODO: Add to the doc.
  if (!input.myField1 || !input.myField2) {
    return ctx.throw(400);
  }

  if (typeof input.myField1 !== 'string' || typeof input.myField2 !== 'string') {
    return ctx.throw(400);
  }

  return next();
};

Worker

The use of this plugin implies the need of a worker to resolve the jobs put in the queue by this HTTP API. It should also write the result directly into the database.

It uses to be very attached to each project, so we provide this helper to make the things easier.