Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #116 from rgarcia/post-api

POST /job API
  • Loading branch information...
commit 3a1f4719566928f979233805fd8a213037f7ecce 2 parents ecdbedd + 2b27002
@tj tj authored
Showing with 44 additions and 0 deletions.
  1. +20 −0 Readme.md
  2. +1 −0  lib/http/index.js
  3. +23 −0 lib/http/routes/json.js
View
20 Readme.md
@@ -312,6 +312,26 @@ kue.app.set('title', 'My Application');
$ curl -X DELETE http://local:3000/job/2
{"message":"job 2 removed"}
+### POST /job
+
+ Create a job:
+
+ $ curl -H "Content-Type: application/json" -X POST -d \
+ '{
+ "type": "email",
+ "data": {
+ "title": "welcome email for tj",
+ "to": "tj@learnboost.com",
+ "template": "welcome-email"
+ },
+ "options" : {
+ "attempts": 5,
+ "priority": "high"
+ }
+ }' http://localhost:3000/job
+ {"message":"job 3 created"}
+
+
## Parallel Processing With Cluster
The example below shows how you may use [Cluster](http://learnboost.github.com/cluster) to spread the job processing load across CPUs. By default cluster will create one worker per CPU, however you can specify this number via `.set('workers', N)`.
View
1  lib/http/index.js
@@ -61,6 +61,7 @@ app.get('/job/:id/log', provides('json'), json.log);
app.put('/job/:id/state/:state', provides('json'), json.updateState);
app.put('/job/:id/priority/:priority', provides('json'), json.updatePriority);
app.del('/job/:id', provides('json'), json.remove);
+app.post('/job', provides('json'), express.bodyParser(), json.createJob);
// routes
View
23 lib/http/routes/json.js
@@ -123,6 +123,29 @@ exports.job = function(req, res){
};
/**
+ * Create a job.
+ */
+
+exports.createJob = function(req, res) {
+ if (!req.body.type)
+ return res.send({ error: 'Must provide job type' }, 400);
+
+ var job = new Job(req.body.type, req.body.data || {});
+ var options = req.body.options;
+ if (options && options.attempts)
+ job.attempts(parseInt(options.attempts));
+ if (options && options.priority)
+ job.priority(options.priority);
+
+ job.save(function(err, resp) {
+ if (err)
+ return res.send({ error: err.message }, 500);
+ res.send({ message: 'job ' + job.id + ' created' });
+ });
+};
+
+
+/**
* Remove job :id.
*/

0 comments on commit 3a1f471

Please sign in to comment.
Something went wrong with that request. Please try again.