-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
120 lines (113 loc) · 3.36 KB
/
index.js
File metadata and controls
120 lines (113 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var request = require('request'),
qs = require('querystring'),
util = require('util');
var spark_jobserver = function(options) {
this.options = options;
this.endpoint = options.host || 'localhost:8090';
};
spark_jobserver.prototype = {
get jars() {
var instance = this;
return {
list: function(callback) {
return instance.command('jars', {}, '', callback);
},
upload: function(app_name, jar_file, callback) {
callback(new Error('not yet implemented'));
}
};
},
get contexts() {
var instance = this;
return {
list: function(callback) {
return instance.command('contexts', {}, '', callback);
},
create: function(name, config, callback) {
return instance.command('contexts/' + name, config, '', callback, 'POST');
},
stop: function(name, callback) {
return instance.command('contexts/' + name, {}, '', callback, 'DELETE');
}
};
},
get jobs() {
var instance = this;
return {
list: function(callback) {
return instance.command('jobs', {}, '', callback);
},
start: function(app_name, class_path, options, body, callback) {
var qs = {
appName: app_name,
classPath: class_path
};
for (var attr in options) { qs[attr] = options[attr]; }
return instance.command('jobs', qs, body, callback, 'POST');
},
queue: function(app_name, class_path, options, body, callback) {
var AWS = '';
try {
AWS = require('aws-sdk');
} catch (ex) {
throw new Error("aws-sdk module not installed.");
}
var queue_config = instance.options.queue || '';
var queue = '';
var queue_url = '';
if (queue_config !== '') {
var region = queue_config.region || 'us-east-1';
queue_url = 'https://sqs.' + region + '.amazonaws.com' + queue_config.name;
queue = new AWS.SQS(queue_config);
}
if (queue === '') {
throw new Error("Queue not configured.");
}
var message = {
appName: app_name,
classPath: class_path,
options: options,
body: body
};
queue.sendMessage({
QueueUrl: queue_url,
MessageBody: JSON.stringify(message)
}, callback);
},
result: function(job_id, callback) {
return instance.command('jobs/' + job_id, {}, '', callback);
},
kill: function(job_id, callback) {
return instance.command('jobs/' + job_id, {}, '', callback);
},
config: function(job_id, callback) {
return instance.command('jobs/' + job_id + '/config', {}, '', callback);
}
};
},
command: function(path, querystring, body, callback, method) {
var options = {
method: method || 'GET',
uri: this.endpoint + "/" + path,
timeout: 5000
};
params = qs.stringify(querystring);
options.uri += '?' + params;
if('POST' == method) {
options.body = body;
}
request(options, function(err, res, body) {
if (typeof body != 'undefined') {
try {
body = JSON.parse(body);
}
catch (e) {
return callback(new Error('Invalid JSON from Jobserver'), null);
}
}
callback(err, body);
});
return this;
}
};
module.exports = spark_jobserver;