# strapi-plugin-jobs

[![Build Status](https://travis.ibm.com/ETX/strapi-plugin-jobs.svg?token=GMH4xFrA9iezVJKqw2zH&branch=master)](https://travis.ibm.com/ETX/strapi-plugin-jobs)

:construction_worker: [Strapi](http://strapi.io) plugin to support background jobs.

- Simpe, thought for prototyping.
- Generic enough to support different kind of applications.
- Support [bull](https://optimalbits.github.io/bull) as the queue engine.
- Job inputs, results and metadata storage in MongoDB.
- Jobs info download in JSON format.
  - Filtering by date.
- Worker implementation example provided.

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](https://i.ibb.co/Ny1PbJr/strapi-jobs.png)

Collection fields

![job fields](https://i.ibb.co/b76JFQx/fields.png)

To learn about Strapi plugins you can visit [the official documentation](https://strapi.io/documentation/concepts/plugins.html).

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

## Install & use

:pizza: Inside a project folder.

```sh
npm i --save strapi-plugin-jobs
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`.

```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:

```js
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](worker) to make the things easier.
