-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic integrations, still need some refactor, tests, improvements Things can break
- Loading branch information
Showing
16 changed files
with
332 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,94 @@ | ||
# trailpack-kue | ||
📦 Trails Kue integration | ||
📦 Easily set up background workers with [Kue](https://github.com/Automattic/kue) and [Trails](http://trailsjs.io) | ||
This project is built on top of the [Kue](https://github.com/Automattic/kue) library | ||
|
||
## WARNING still work in progress, things can break | ||
|
||
## Install | ||
|
||
```sh | ||
$ npm install --save trailpack-kue | ||
``` | ||
|
||
## Configure | ||
|
||
### Add Trailpack | ||
```js | ||
// config/main.js | ||
module.exports = { | ||
packs: [ | ||
// ... other trailpacks | ||
require('trailpack-tasker') | ||
] | ||
} | ||
``` | ||
|
||
### Configure Task Settings | ||
|
||
```js | ||
// config/kue.js | ||
module.exports = { | ||
|
||
/** | ||
* Define worker profiles. Each worker of a given type listens for the | ||
* "tasks" defined in its profile below. The task names represent a Task | ||
* defined in api.services.tasks. | ||
*/ | ||
tasks: { | ||
hello_world: { | ||
concurrency: 1, | ||
controller: "HelloWorldTask" | ||
} | ||
}, | ||
webui: { | ||
active: false, | ||
port: 8080 | ||
} | ||
} | ||
``` | ||
|
||
### Include tasks in the app object | ||
Create a directory `api/tasks`. Any task definitions will be created as classes in this directory. | ||
Create `api/tasks/index.js` to export all of the tasks. | ||
Include this directory in `api/index.js`. Here is an example: | ||
|
||
```js | ||
// api/index.js | ||
|
||
exports.controllers = require('./controllers') | ||
exports.models = require('./models') | ||
exports.policies = require('./policies') | ||
exports.services = require('./services') | ||
exports.tasks = require('./tasks') | ||
``` | ||
|
||
## Usage | ||
|
||
Define tasks in `api.tasks`. Tasks are run by kue. | ||
|
||
```js | ||
// api/tasks/HelloWorldTask.js | ||
|
||
const Task = require("trailpack-kue").Task | ||
|
||
module.exports = class HelloWorldTask extends Task { | ||
contructor(){ | ||
super() | ||
} | ||
|
||
run(job, done) { | ||
console.log("Hello ", job.data.name); | ||
done(); | ||
} | ||
} | ||
``` | ||
|
||
|
||
To start a job, create a job object via `app.services.KueService.addJob` interface and save it: | ||
Job can be set with all Kue Job options | ||
|
||
``` | ||
let job = app.services.KueService.addJob("hello_world",{name:"Jon Doe"}) | ||
job.priority("high") | ||
job.save() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports.services = require('./services') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
const Service = require('trails-service'); | ||
const Kue = require("kue"); | ||
|
||
/** | ||
* @module KueService | ||
* @description TODO document Service | ||
*/ | ||
module.exports = class KueService extends Service { | ||
init() { | ||
const config = this.app.config.kue; | ||
|
||
this.kueInstance = Kue.createQueue(); | ||
this.tasks = {} | ||
|
||
const tasks = Object.keys(config.tasks) | ||
tasks.forEach(task => { | ||
this.addTask(task, config.tasks[task]) | ||
}) | ||
} | ||
|
||
addTask(name, task) { | ||
let _task = new this.app.api.tasks[task.controller] | ||
this.kueInstance.process(name, _task.run) | ||
} | ||
|
||
addJob(name, obj){ | ||
return this.kueInstance.createJob(name, obj) | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports.KueService = require('./KueService') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports.trailpack = require('./trailpack') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Trailpack Configuration | ||
* | ||
* @see {@link http://trailsjs.io/doc/trailpack/config | ||
*/ | ||
module.exports = { | ||
type: 'misc', | ||
/** | ||
* Configure the lifecycle of this pack; that is, how it boots up, and which | ||
* order it loads relative to other trailpacks. | ||
*/ | ||
lifecycle: { | ||
configure: { | ||
/** | ||
* List of events that must be fired before the configure lifecycle | ||
* method is invoked on this Trailpack | ||
*/ | ||
listen: [], | ||
|
||
/** | ||
* List of events emitted by the configure lifecycle method | ||
*/ | ||
emit: [] | ||
}, | ||
initialize: { | ||
listen: [], | ||
emit: [] | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
|
||
const Trailpack = require('trailpack'); | ||
const lib = require("./lib"); | ||
|
||
module.exports = class KueTrailpack extends Trailpack { | ||
|
||
/** | ||
* TODO document method | ||
*/ | ||
validate() { | ||
if (!this.app.config.kue) throw new Error("config.kue not defined") | ||
if (!this.app.config.kue.tasks) throw new Error("config.kue.tasks not defined") | ||
if (!this.app.config.kue.webui) throw new Error("config.kue.webui not defined") | ||
} | ||
|
||
/** | ||
* TODO document method | ||
*/ | ||
configure() { | ||
|
||
} | ||
|
||
/** | ||
* TODO document method | ||
*/ | ||
initialize() { | ||
this.app.on("trails:ready", () => { | ||
this.app.services.KueService.init(); | ||
}) | ||
return Promise.resolve(); | ||
} | ||
|
||
constructor(app) { | ||
super(app, { | ||
config: require('./config'), | ||
api: require('./api'), | ||
pkg: require('./package') | ||
}) | ||
} | ||
} | ||
|
||
module.exports.Task = lib.Task |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict' | ||
|
||
module.exports = class Task { | ||
constructor() { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports.Task = require('./Task') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "trailpack-kue", | ||
"version": "0.0.0", | ||
"description": "Kue implementation for TrailsJs.io", | ||
"homepage": "http://trailsjs.io", | ||
"author": { | ||
"name": "Mattia Lobertini <lobetia@gmail.com>", | ||
"email": "lobetia@gmail.com", | ||
"url": "https://github.com/lobetia/trailpack-kue" | ||
}, | ||
"main": "index.js", | ||
"keywords": [ | ||
"trails", | ||
"trailsj", | ||
"kue", | ||
"trailpack", | ||
"trailjs" | ||
], | ||
"dependencies": { | ||
"kue": "0.11.5", | ||
"trailpack": "2.0.1" | ||
}, | ||
"devDependencies": { | ||
"lodash": "^4.11.1", | ||
"trails": "latest", | ||
"eslint": "^2.4", | ||
"eslint-config-trails": "^1.0.4", | ||
"mocha": "^2.4.5", | ||
"smokesignals": "^1.0.9", | ||
"trailpack-core": "^1.0.0-beta-6" | ||
}, | ||
"scripts": { | ||
"test": "eslint . && mocha" | ||
}, | ||
"engines": { | ||
"node": ">= 4.0.0" | ||
}, | ||
"eslintConfig": { | ||
"extends": "trails" | ||
}, | ||
"repository": "ssh://git@bitbucket.org/subluminar/gestionaleagenti_core", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "trails/test" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict' | ||
|
||
const _ = require('lodash') | ||
const smokesignals = require('smokesignals') | ||
|
||
module.exports = _.defaultsDeep({ | ||
pkg: { | ||
name: require('../package').name + '-test' | ||
}, | ||
api: { | ||
models: { }, | ||
controllers: { }, | ||
services: { } | ||
}, | ||
config: { | ||
main: { | ||
packs: [ | ||
smokesignals.Trailpack, | ||
require('trailpack-core'), | ||
require('../') | ||
] | ||
} | ||
} | ||
}, smokesignals.FailsafeConfig) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict' | ||
|
||
const TrailsApp = require('trails') | ||
|
||
before(() => { | ||
global.app = new TrailsApp(require('./app')) | ||
return global.app.start().catch(global.app.stop) | ||
}) | ||
|
||
after(() => { | ||
return global.app.stop() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--reporter spec | ||
--recursive | ||
--full-trace | ||
--no-exit | ||
--slow 50 | ||
--check-leaks | ||
--globals app | ||
--fgrep app.js --invert |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict' | ||
|
||
const assert = require('assert') | ||
|
||
describe('Trailpack', () => { | ||
let pack | ||
before(() => { | ||
// pack = global.app.packs.waterline | ||
}) | ||
it.skip('TODO should be loaded into the app.packs collection', () => { | ||
assert(pack) | ||
}) | ||
describe('#validate', () => { | ||
it.skip('TODO test') | ||
}) | ||
describe('#configure', () => { | ||
it.skip('TODO test') | ||
}) | ||
describe('#initialize', () => { | ||
it.skip('TODO test') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict' | ||
/* global describe, it */ | ||
const assert = require('assert') | ||
|
||
describe('KueService', () => { | ||
it('should exist', () => { | ||
assert(global.app.api.services['KueService']) | ||
}) | ||
}) |