Skip to content

Commit 172cb55

Browse files
committed
Merge pull request #2 from preact/feature/add_queueing
When you need more jobs than cores on the jobserver
2 parents ec35f16 + 5c03cd5 commit 172cb55

File tree

4 files changed

+108
-7
lines changed

4 files changed

+108
-7
lines changed

example.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1-
var spark_jobserver = require('./spark_jobserver');
2-
var jobserver = new spark_jobserver();
1+
var spark_jobserver = require('spark_jobserver');
2+
var config = {
3+
host: "localhost:8090",
4+
queue: {
5+
key: "<aws key>",
6+
secret: "<aws secret>",
7+
region: "us-east-1",
8+
name: "/838405463324/jobs-queued"
9+
}
10+
};
11+
12+
var jobserver = new spark_jobserver(config);
313

414
jobserver.jobs.start('theapp', 'com.foo.blah.awesome', {context: 'awesome', sync: true}, '', function(err, data){
515
console.log(data);
616
});
717

818
jobserver.jobs.result('5ab8f3f9-c7d1-42ec-87cb-344d0d0b5c5c', function(err, data){
919
console.log(data);
20+
});
21+
22+
var config = '{"stress": {"test": {"duration": "15"}}}';
23+
jobserver.jobs.queue('testjob', 'spark.jobserver.test', {context: 'awesome'}, config, function(err, data){
24+
console.log(data);
1025
});

example_worker.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* NOTE: run `npm i sqs-worker` before doing this example
3+
*/
4+
var spark_jobserver = require('spark_jobserver'),
5+
SQSWorker = require('sqs-worker');
6+
7+
var config = {
8+
host: "localhost:8090",
9+
queue: {
10+
key: "<aws key>",
11+
secret: "<aws secret>",
12+
region: "us-east-1",
13+
name: "/838405463324/jobs-queued"
14+
}
15+
};
16+
17+
var endpoint = 'https://sqs.' + config.queue.region + '.amazonaws.com';
18+
var opts = {
19+
endpoint: endpoint,
20+
region: config.queue.region,
21+
url: endpoint + config.queue.name
22+
};
23+
24+
var queue_worker = new SQSWorker(opts, worker);
25+
var jobserver = new spark_jobserver({host: config.host});
26+
27+
function worker(message, done) {
28+
try {
29+
message = JSON.parse(message);
30+
console.log(message);
31+
} catch (err) {
32+
throw err;
33+
}
34+
35+
jobserver.jobs.start(message.appName, message.classPath, message.options, message.body, function(err, data){
36+
console.log(data);
37+
38+
var success = false;
39+
success = (data.status == 'STARTED') || (data.status == 'ERROR') || (data.status == 'VALIDATION FAILED');
40+
41+
if ((data.status == 'ERROR') || (data.status == 'VALIDATION FAILED')) {
42+
// let the humans know
43+
}
44+
45+
if (data.status == 'NO SLOTS AVAILABLE') {
46+
// sleep and try again later?
47+
}
48+
49+
done(null, success);
50+
});
51+
52+
}

index.js

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
// ala https://github.com/chadsmith/node-namecheap/blob/master/namecheap.js
21
var request = require('request'),
32
qs = require('querystring'),
43
util = require('util');
54

6-
var spark_jobserver = function(host) {
7-
host = typeof host !== 'undefined' ? host : 'localhost:8090';
8-
9-
this.endpoint = host;
5+
var spark_jobserver = function(options) {
6+
this.options = options;
7+
this.endpoint = options.host || 'localhost:8090';
108
};
119

1210
spark_jobserver.prototype = {
@@ -49,6 +47,39 @@ spark_jobserver.prototype = {
4947
for (var attr in options) { qs[attr] = options[attr]; }
5048
return instance.command('jobs', qs, body, callback, 'POST');
5149
},
50+
queue: function(app_name, class_path, options, body, callback) {
51+
var AWS = '';
52+
try {
53+
AWS = require('aws-sdk');
54+
} catch (ex) {
55+
throw new Error("aws-sdk module not installed.");
56+
}
57+
58+
var queue_config = instance.options.queue || '';
59+
var queue = '';
60+
var queue_url = '';
61+
if (queue_config !== '') {
62+
var region = queue_config.region || 'us-east-1';
63+
queue_url = 'https://sqs.' + region + '.amazonaws.com' + queue_config.name;
64+
queue = new AWS.SQS(queue_config);
65+
}
66+
67+
if (queue === '') {
68+
throw new Error("Queue not configured.");
69+
}
70+
71+
var message = {
72+
appName: app_name,
73+
classPath: class_path,
74+
options: options,
75+
body: body
76+
};
77+
78+
queue.sendMessage({
79+
QueueUrl: queue_url,
80+
MessageBody: JSON.stringify(message)
81+
}, callback);
82+
},
5283
result: function(job_id, callback) {
5384
return instance.command('jobs/' + job_id, {}, '', callback);
5485
},

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@
1818
"license": "ISC",
1919
"dependencies": {
2020
"request": "^2.66.0"
21+
},
22+
"optionalDependencies": {
23+
"aws-sdk": "^2.2.24"
2124
}
2225
}

0 commit comments

Comments
 (0)