Skip to content

Commit

Permalink
First working release
Browse files Browse the repository at this point in the history
Added basic integrations, still need some refactor, tests, improvements
Things can break
  • Loading branch information
LobeTia committed Nov 26, 2016
1 parent d3e6614 commit 0934b91
Show file tree
Hide file tree
Showing 16 changed files with 332 additions and 1 deletion.
94 changes: 93 additions & 1 deletion README.md
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()
```
1 change: 1 addition & 0 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.services = require('./services')
31 changes: 31 additions & 0 deletions api/services/KueService.js
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)
}
};
1 change: 1 addition & 0 deletions api/services/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.KueService = require('./KueService')
1 change: 1 addition & 0 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.trailpack = require('./trailpack')
31 changes: 31 additions & 0 deletions config/trailpack.js
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: []
}
}
}

43 changes: 43 additions & 0 deletions index.js
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
7 changes: 7 additions & 0 deletions lib/Task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = class Task {
constructor() {

}
}
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.Task = require('./Task')
43 changes: 43 additions & 0 deletions package.json
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"
}
3 changes: 3 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "trails/test"
}
26 changes: 26 additions & 0 deletions test/app.js
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)


12 changes: 12 additions & 0 deletions test/index.js
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()
})
8 changes: 8 additions & 0 deletions test/mocha.opts
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
22 changes: 22 additions & 0 deletions test/trailpack.test.js
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')
})
})
9 changes: 9 additions & 0 deletions test/unit/services/KueService.test.js
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'])
})
})

0 comments on commit 0934b91

Please sign in to comment.