-
Notifications
You must be signed in to change notification settings - Fork 1
/
triggerjob.js
302 lines (271 loc) · 8.15 KB
/
triggerjob.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
process.env.NODE_PATH = "/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript";
//__dirname;
require('module').Module._initPaths();
var os = require("os");
var url = require('url');
var http = require('http');
var https = require('https');
var request = require('request');
var follow = require('follow');
var triggers = {};
var sysclient = http;
var thishost = [os.hostname()];
var args = process.argv.slice(2);
var config = {};
var port = 0;
var sysopt = {};
var feeds = [];
var loadTrigger = function(type) {
if (typeof triggers[type]!=="undefined") return triggers[type];
try {
triggers[type] = require('./trigger-'+type);
}catch(ex){
log("CANNOT FIND .JS FILE FOR TRIGGER TYPE: \""+type.toString()+"\"");
triggers[type] = false;
}
return triggers[type];
};
var log = function(mesg) {
if (args.length>0) console.log(mesg);
else console.log(JSON.stringify(["log", mesg]));
};
var escapeRegExp = function(string) {
return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
};
var subst_config = function(obj) {
// replace config values: <!myvar!> will be replaced with value of <triggerjob.myvar>
if (typeof obj!="object") return obj;
var newtr = JSON.stringify(obj);
if (typeof obj.allowed == "object") {
for (var i in obj.allowed) {
var k = obj.allowed[i];
if (config[k]) {
newtr = newtr.replace(new RegExp(escapeRegExp("<!"+k+"!>"),'gi'),config[k]);
}
}
}
newtr = newtr.replace(/<![^!]+!>/gi,"<!VAR-UNKNOWN-OR-NOT-ALLOWED!>");
return JSON.parse(newtr);
};
var markTriggerAs = function(pathurl,tries, dockey, trkey, action, code, out, cb) {
//log(tries.toString()+" Marking trigger "+trkey+"@"+dockey+" as "+action);
var ropt = {
method: 'PUT'
, url:pathurl+"/trigger/"+dockey
, headers : sysopt.headers
//, json : true
, body : JSON.stringify({
'action' : action,
'trkey' : trkey,
'code' : code,
'out' : out
})
};
//log("PREP REQUEST: "+JSON.stringify(ropt));
//log("Trigger url: "+pathurl+"/trigger/"+dockey);
request(ropt , function (error, response, body) {
var e = (error||"").toString();
if (e.length>0) {
cb("REQUEST EXCEPTION:"+e);
} else {
//log("RESPONSE("+(response.statusCode||"").toString()+"):"+(body||"").substring(0,200));
try {
switch(parseInt(response.statusCode)) {
case 200:
case 201:
cb(false,body);
break;
case 409:
if (tries < 3) {
setTimeout(function(){
markTriggerAs(pathurl,tries+1, dockey, trkey, action, code, out, cb);
},Math.random()*4000);
} else {
cb("Conflict persists after 3 attempts");
}
break;
default:
cb(body)
break;
}
} catch(ex){
cb(ex);
}
}
});
};
var executeTrigger = function(pathurl,dockey,trkey,tr,executecb){
var cbdone = function(code,out){
var iserrmsg = typeof code != "number";
markTriggerAs(pathurl,0,dockey,trkey,'done',iserrmsg?false:code,iserrmsg?code:out,function(error,data){
if (!error) {
log("DONE "+ trkey+"@"+dockey+": "+(data||""));
executecb();
} else {
executecb(error);
}
});
};
var fn = loadTrigger((tr.type||"http").toString());
if (fn) {
markTriggerAs(pathurl,0,dockey,trkey,'queued',null,null,function(error,data){
if (!error) {
log("QUEUED "+ trkey+"@"+dockey+": "+(data||""));
fn(log,sysopt,pathurl,tr,dockey,trkey,cbdone);
} else {
executecb(error);
}
})
} else {
executecb("Cannot find trigger type \""+(tr.type||"http")+"\"");
}
};
var executeAt =function(pathurl,id,trkey,tr,cb) {
var now = new Date().getTime();
//tr.start = now+20000;
if ((tr.delay||0)>0) {
setTimeout(function(){
markTriggerAs(pathurl,0,id,trkey,'delay',null,null,function(error,data){
if (error) cb(error);
});
},2000+Math.random()*2000);
} else {
log("Should start at :"+(tr.start||0).toString()+" now:"+(now).toString());
if ((tr.start||0) < now){
setTimeout(function(){
executeTrigger(pathurl,id,trkey,tr,cb);
},Math.random()*4000);
} else {
var toelapse = tr.start-now;
log("Postponing of :"+toelapse.toString()+" ms");
setTimeout(function(){
executeTrigger(pathurl,id,trkey,tr,cb);
},toelapse);
}
}
};
var followChanges = function(path) {
var cb=function(err){
if (typeof err !== "undefined") {
log("EXECUTEAT-CB: "+(err||"UNK-ERROR"));
}
}, opt=sysopt, pathurl="";
opt.pathname = path;
pathurl = url.format(opt);
log("Following path: "+path);
log("Following url: "+pathurl);
return follow({db:pathurl+"/follow",headers:opt.headers}, function(error, change) {
//log("follow changes return a doc");
if(!error) {
log("Got change number " + change.seq + ": " + change.id);
for (var trkey in change.doc&&change.doc.triggers?change.doc.triggers:{}){
var tr = change.doc.triggers[trkey];
if (typeof tr.queued==="undefined" && (!tr.h||thishost.indexOf(tr.h||"")>=0)) {
log("CALLING ExecuteAt "+trkey)
executeAt(pathurl,change.id,trkey,subst_config(tr),cb);
}
}
} else {
log(error.toString());
}
});
};
var start = function(paths){
if (config.job_path) delete config.job_path;
if (config.job_authorization) delete config.job_authorization;
if (typeof paths == "string" && paths.length>0) {
paths = paths.split(',');
for (var i in paths) {
if (paths[i].length>0) {
feeds[paths[i]] = followChanges(paths[i]);
}
}
} else {
log("Invalid triggerjob.job_path");
}
};
var cmdlineinit = function(){
sysopt = url.parse(args[0]||"");
sysopt.method = "GET";
var credentials = "Basic "+(new Buffer(args[1]||"").toString('base64'));
sysopt.headers = {authorization:credentials?credentials:""}
var schema = ["http","https"].indexOf((sysopt.protocol||"").toLowerCase().split(":")[0]);
if (schema === 1) {
sysclient = https;
sysopt.protocol = "https:";
//sysopt.hostname = sysopt.host;
//sysopt.port = sysopt.port||443;
}
var opt=sysopt;
opt.path = "/_config/triggerjob"; opt.method = "GET";
var req = sysclient.request(opt, function(response) {
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function(d) {
try {
if (response.statusCode!=200) throw(body);
config = JSON.parse(body);
start(config.job_path);
} catch(ex) {
log("init exception: "+ ex.toString());
}
});
}).on('error', function(ex) {
log(ex);
}).end();
};
// We use stdin in a couple ways. First, we
// listen for data that will be the requested
// port information. We also listen for it
// to close which indicates that CouchDB has
// exited and that means its time for us to
// exit as well.
var stdin = process.openStdin();
stdin.on('data', function(msg) {
var parsed = {};
//log("Receiving stdin message: " + msg);
try {
parsed = JSON.parse(msg);
if (parseInt(parsed)==parsed) {
port = parseInt(parsed);
console.log(JSON.stringify(["register", "triggerjob"]));
console.log(JSON.stringify(["get", "triggerjob"]));
} else if (parsed.couchdb) { // welcome message
//log("welcome message: " + msg);
} else if (parsed.job_path) {
//log("triggerjob config: " + msg);
config = parsed;
var credentials = "Basic "+(new Buffer(config.job_authorization||"").toString('base64'));
sysclient = http;
sysopt = url.parse("http://127.0.0.1:"+port.toString());
//sysopt.host = "127.0.0.1:"+port;
//sysopt.hostname = "127.0.0.1";
delete sysopt.host;
//sysopt.port = port;
sysopt.method = "GET";
sysopt.headers = {authorization:credentials?credentials:""}
start(config.job_path);
} else {
log("Discarding unknown message: " + msg);
}
} catch(ex){
log("Not a JSON string: " + msg);
}
});
stdin.on('end', function () {
for (var i in feeds){
log("Stopping feed "+feeds[i]);
feeds[i].stop();
delete feeds[i];
}
process.exit(0);
});
if (args.length>0) {
// port and config from command line
cmdlineinit();
} else {
// port and config from stdin
console.log(JSON.stringify(["get", "httpd", "port"]));
}