Skip to content

Commit

Permalink
feat: added support for jobs/index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
niftylettuce committed Jul 9, 2020
1 parent e1008fb commit 3766c41
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,35 @@ The option `jobs` passed to a new instance of `Bree` (as shown below) is an Arra
```js
const path = require('path');

const ms = require('ms'); // optional
const dayjs = require('dayjs'); // optional
const Graceful = require('@ladjs/graceful'); // optional
// optional
const ms = require('ms');
const dayjs = require('dayjs');
const Graceful = require('@ladjs/graceful');
const Cabin = require('cabin');

// required
const Bree = require('bree');

//
// NOTE: see index.js for full list of options and defaults
//
const bree = new Bree({
//
// NOTE: by default the `logger` is set to `console`
// however we recommend you to use CabinJS as it
// will automatically add application and worker metadata
// to your log output, and also masks sensitive data for you
// <https://cabinjs.com>
//
logger: new Cabin(),

//
// NOTE: instead of passing this Array as an option
// you can create a `./jobs/index.js` file, exporting
// this exact same array as `module.exports = [ ... ]`
// doing so will allow you to keep your job configuration and the jobs
// themselves all in the same folder and very organized
//
jobs: [
// runs `./jobs/foo.js` on start
'foo',
Expand Down Expand Up @@ -230,6 +249,8 @@ If you'd like jobs to retry, simply wrap your usage of promises with [p-retry][]

We leave it up to you to have as much fine-grained control as you wish.

See [@ladjs/graceful][lad-graceful] for more insight into how this package works.


## Interval, Timeout, and Cron Validation

Expand Down Expand Up @@ -350,3 +371,5 @@ Kudos to the authors of all these packages, however they did not work well enoug
[async-await]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

[mongodb]: https://www.mongodb.com/

[lad-graceful]: https://github.com/ladjs/graceful
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ class Bree {
this.config.interval = this.getInterval(this.config.interval);
debug('interval', this.config.interval);

//
// if `this.config.jobs` is an empty array
// then we should try to load `jobs/index.js`
//
if (
this.config.root &&
(!Array.isArray(this.config.jobs) || this.config.jobs.length === 0)
) {
try {
this.config.jobs = require(this.config.root);
} catch (err) {
this.config.logger.error(err);
}
}

//
// validate jobs
//
Expand Down

0 comments on commit 3766c41

Please sign in to comment.