From 0934b9187f9552e5dd560bdc824e42e592348bd7 Mon Sep 17 00:00:00 2001 From: Mattia Lobertini Date: Sat, 26 Nov 2016 14:34:52 +0100 Subject: [PATCH] First working release Added basic integrations, still need some refactor, tests, improvements Things can break --- README.md | 94 ++++++++++++++++++++++++++- api/index.js | 1 + api/services/KueService.js | 31 +++++++++ api/services/index.js | 1 + config/index.js | 1 + config/trailpack.js | 31 +++++++++ index.js | 43 ++++++++++++ lib/Task.js | 7 ++ lib/index.js | 1 + package.json | 43 ++++++++++++ test/.eslintrc.json | 3 + test/app.js | 26 ++++++++ test/index.js | 12 ++++ test/mocha.opts | 8 +++ test/trailpack.test.js | 22 +++++++ test/unit/services/KueService.test.js | 9 +++ 16 files changed, 332 insertions(+), 1 deletion(-) create mode 100644 api/index.js create mode 100644 api/services/KueService.js create mode 100644 api/services/index.js create mode 100644 config/index.js create mode 100644 config/trailpack.js create mode 100644 index.js create mode 100644 lib/Task.js create mode 100644 lib/index.js create mode 100644 package.json create mode 100644 test/.eslintrc.json create mode 100644 test/app.js create mode 100644 test/index.js create mode 100644 test/mocha.opts create mode 100644 test/trailpack.test.js create mode 100755 test/unit/services/KueService.test.js diff --git a/README.md b/README.md index 4623548..fb5df55 100644 --- a/README.md +++ b/README.md @@ -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() +``` diff --git a/api/index.js b/api/index.js new file mode 100644 index 0000000..131c517 --- /dev/null +++ b/api/index.js @@ -0,0 +1 @@ +exports.services = require('./services') diff --git a/api/services/KueService.js b/api/services/KueService.js new file mode 100644 index 0000000..e654572 --- /dev/null +++ b/api/services/KueService.js @@ -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) + } +}; diff --git a/api/services/index.js b/api/services/index.js new file mode 100644 index 0000000..baac3dd --- /dev/null +++ b/api/services/index.js @@ -0,0 +1 @@ +exports.KueService = require('./KueService') diff --git a/config/index.js b/config/index.js new file mode 100644 index 0000000..fbf95d6 --- /dev/null +++ b/config/index.js @@ -0,0 +1 @@ +exports.trailpack = require('./trailpack') diff --git a/config/trailpack.js b/config/trailpack.js new file mode 100644 index 0000000..81f8f89 --- /dev/null +++ b/config/trailpack.js @@ -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: [] + } + } +} + diff --git a/index.js b/index.js new file mode 100644 index 0000000..747f8d7 --- /dev/null +++ b/index.js @@ -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 diff --git a/lib/Task.js b/lib/Task.js new file mode 100644 index 0000000..1e8ad59 --- /dev/null +++ b/lib/Task.js @@ -0,0 +1,7 @@ +'use strict' + +module.exports = class Task { + constructor() { + + } +} diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..4e8d53f --- /dev/null +++ b/lib/index.js @@ -0,0 +1 @@ +exports.Task = require('./Task') diff --git a/package.json b/package.json new file mode 100644 index 0000000..0a7130c --- /dev/null +++ b/package.json @@ -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 ", + "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" +} diff --git a/test/.eslintrc.json b/test/.eslintrc.json new file mode 100644 index 0000000..be161bb --- /dev/null +++ b/test/.eslintrc.json @@ -0,0 +1,3 @@ +{ + "extends": "trails/test" +} diff --git a/test/app.js b/test/app.js new file mode 100644 index 0000000..61f210d --- /dev/null +++ b/test/app.js @@ -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) + + diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..eb18ecc --- /dev/null +++ b/test/index.js @@ -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() +}) diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 0000000..9b9576b --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1,8 @@ +--reporter spec +--recursive +--full-trace +--no-exit +--slow 50 +--check-leaks +--globals app +--fgrep app.js --invert diff --git a/test/trailpack.test.js b/test/trailpack.test.js new file mode 100644 index 0000000..261aeaa --- /dev/null +++ b/test/trailpack.test.js @@ -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') + }) +}) diff --git a/test/unit/services/KueService.test.js b/test/unit/services/KueService.test.js new file mode 100755 index 0000000..8849dcb --- /dev/null +++ b/test/unit/services/KueService.test.js @@ -0,0 +1,9 @@ +'use strict' +/* global describe, it */ +const assert = require('assert') + +describe('KueService', () => { + it('should exist', () => { + assert(global.app.api.services['KueService']) + }) +})