Skip to content
Browse files

Added deleting/cancelling of jobs

  • Loading branch information...
1 parent 8af40c1 commit d3e8fe422c3eb8dd2d8cec6f005b5c9172356e08 @tieleman tieleman committed Aug 23, 2011
Showing with 82 additions and 11 deletions.
  1. +4 −0 lib/db-handler.js
  2. +21 −0 lib/job-handler.js
  3. +23 −1 lib/job.js
  4. +29 −6 lib/server.js
  5. +5 −4 test/job-spec.js
View
4 lib/db-handler.js
@@ -30,6 +30,10 @@ exports.getJob = function(id, callback) {
db.get("SELECT * FROM jobs WHERE id LIKE ? LIMIT 1", [id], callback);
}
+exports.deleteJob = function(id, callback) {
+ db.run("DELETE FROM jobs WHERE id=?", [id], callback);
+}
+
exports.closeDatabase = function() {
db.close();
}
View
21 lib/job-handler.js
@@ -14,6 +14,27 @@ exports.freeSlots = function() {
return config['slots'] - slots.length;
}
+exports.cancelAndRemove = function(id, callback) {
+ for (var item in slots) {
+ if (slots[item].id == id) {
+ slots[item].cancel();
+ slots[item].delete();
+ callback(null, slots[item]);
+ return;
+ }
+ }
+
+ var job = Job.find(id, function(err, job) {
+ if (job) {
+ job.delete();
+ callback(null, job);
+ return;
+ }
+ });
+
+ callback("No such job to delete", null);
+}
+
exports.slots = slots;
exports.processJobRequest = function(postData, callback) {
View
24 lib/job.js
@@ -40,7 +40,23 @@ Job.create = function(opts) {
}
Job.find = function(id, callback) {
- dbHandler.getJob(id, callback);
+ dbHandler.getJob(id, function(err, job) {
+ if (job) {
+ var j = new Job();
+ for (var field in job) {
+ j[field] = job[field];
+ }
+ callback(err, j);
+ } else {
+ callback(err, job);
+ }
+ });
+}
+
+Job.prototype.delete = function() {
+ dbHandler.deleteJob(this.id, function(err, result) {
+ if (err) throw("Error while deleting job.");
+ });
}
Job.generateId = function() {
@@ -106,6 +122,12 @@ Job.prototype.spawn = function() {
}
}
+Job.prototype.cancel = function() {
+ if (this.the_process) {
+ this.the_process.kill();
+ }
+}
+
Job.prototype.finalize = function(code, tmpFile) {
var job = this;
if (code == 0) {
View
35 lib/server.js
@@ -11,9 +11,10 @@ var badRequestMessage = 'The data supplied was not valid.';
var notFoundMessage = 'The specified job could not be found.';
var routes = function(app) {
- app.get( '/jobs', getJobs);
- app.post('/jobs', postNewJob);
- app.get( '/jobs/:id', getJobStatus);
+ app.get( '/jobs', getJobs);
+ app.post( '/jobs', postNewJob);
+ app.get( '/jobs/:id', getJobStatus);
+ app.delete('/jobs/:id', removeJob);
}
var logfile = null;
@@ -56,15 +57,15 @@ getJobs = function(request, response) {
// GET /jobs/:id
getJobStatus = function(request, response) {
- jobHandler.find(request.params.id, function onResult(err, result) {
+ jobHandler.find(request.params.id, function onResult(err, job) {
var body = {};
- if (err || !result) {
+ if (err || !job) {
response.statusCode = 404;
body['message'] = notFoundMessage;
} else {
response.statusCode = 200;
- body = result;
+ body = job;
}
try {
@@ -76,6 +77,28 @@ getJobStatus = function(request, response) {
});
}
+// DELETE /jobs/:id
+removeJob = function(request, response) {
+ jobHandler.cancelAndRemove(request.params.id, function onResult(err, job) {
+ var body = {};
+
+ if (err || !job) {
+ response.statusCode = 404;
+ body['message'] = notFoundMessage;
+ } else {
+ response.statusCode = 200;
+ body = job;
+ }
+
+ try {
+ response.setHeader('Content-Type', 'application/json; charset=utf-8');
+ response.end(JSON.stringify(body), 'utf8');
+ } catch(e) {
+ util.log("Error while deleting job: " + e + e.stack);
+ }
+ });
+}
+
processPostedJob = function(postData, response) {
response.setHeader('Content-Type', 'application/json; charset=utf-8');
View
9 test/job-spec.js
@@ -45,12 +45,13 @@ describe("A job", function() {
});
describe("when finding", function() {
- it("should return the job", function() {
+ it("should call the dbhandler", function() {
spyOn(dbHandler, "getJob");
+ var callbackSpy = jasmine.createSpy("callback");
+
+ Job.find('1234', callbackSpy);
- Job.find('1234');
-
- expect(dbHandler.getJob).toHaveBeenCalledWith('1234', undefined);
+ expect(dbHandler.getJob).toHaveBeenCalled();
});
});

0 comments on commit d3e8fe4

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