Semaphore/lib/routes/task/tasks.js

80 lines
1.5 KiB
JavaScript
Raw Normal View History

var models = require('../../models')
var mongoose = require('mongoose')
var task = require('./task')
2014-08-26 00:58:09 +02:00
var app = require('../../app')
var jobRunner = require('../../runner');
exports.unauthorized = function (app, template) {
template([
'tasks'
], {
prefix: 'task'
});
task.unauthorized(app, template);
}
exports.httpRouter = function (app) {
task.httpRouter(app);
}
exports.router = function (app) {
2014-08-26 00:58:09 +02:00
app.get('/playbook/:playbook_id/tasks', getTasks)
.get('/playbook/:playbook_id/job/:job_id/tasks', get)
.post('/playbook/:playbook_id/job/:job_id/tasks', add)
2014-08-26 00:58:09 +02:00
.post('/playbook/:playbook_id/job/:job_id/run', runJob)
task.router(app);
}
function get (req, res) {
models.Task.find({
job: req.job._id
2014-08-26 00:58:09 +02:00
}).populate('job').sort('-created').exec(function (err, tasks) {
res.send(tasks)
})
}
function getTasks (req, res) {
models.Task.find({
playbook: req.playbook._id
}).populate('job').sort('-created').exec(function (err, tasks) {
res.send(tasks)
})
}
function add (req, res) {
var task = new models.Task({
job: req.job._id,
status: 'Queued'
})
task.save(function () {
res.send(task);
});
2014-08-26 00:58:09 +02:00
}
function runJob (req, res) {
var task = new models.Task({
job: req.job._id,
playbook: req.playbook._id,
status: 'Queued'
});
task.save(function (err) {
task.populate('job', function () {
app.io.emit('playbook.update', {
task_id: task._id,
playbook_id: req.playbook._id,
task: task
});
jobRunner.queue.push(task);
2014-08-26 00:58:09 +02:00
})
});
res.send(201)
}