-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
140 lines (130 loc) · 4.07 KB
/
worker.js
File metadata and controls
140 lines (130 loc) · 4.07 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env node
var argv = require('optimist').argv;
var _ = require('underscore');
var restler = require('restler');
var fs = require('fs');
var file_path = argv.path;
var exec = require('child_process').exec;
var options = {
host: argv.host || '127.0.0.1',
port: +(argv.port || 80),
tmp : argv.tmp || 'tmp',
bash : argv.bash || 'bash.exe', //so.. tar, mkdir, rm etc work fine, but ./doit.sh will not
method: 'POST'
};
var base_url = 'http://' + options.host + ':' + options.port;
function report_result(work,retry_all){
var file_path = options.tmp +'/work-' + work.id + '/output';
fs.stat(file_path, function(err, stats) {
if(err){
console.log("Error when preforming stat on " + file_path);
console.log(err);
retry_all();
}else{
var url = base_url + '/solved/' + work.id;
restler.post(url, {
multipart: true,
data: {
"result": restler.file(file_path, null, stats.size)
}
}).on("success", function(data) {
console.log("Reported success!");
console.log(data);
loop();
}).on("fail", function(data,response){
console.log("Failed request to " + url);
console.log(data);
console.log(response);
retry_all();
}).on("error",function(err,response){
console.log("Error when performing request to " + url);
console.log(err);
console.log(response);
retry_all();
});
}
});
}
function do_work(work,retry_all){
var dir = options.tmp +'/work-' + work.id;
exec('cd ' + dir + ' && ' + options.bash + ' -c "./doit.sh"', function (error, stdout, stderr) {
console.log(stdout);
console.log(stderr);
if(error){
console.log("Could not perform work in " + dir);
console.log(error);
retry_all();
}else{
console.log("Work done:)");
report_result(work,retry_all);
}
});
}
function decompress_work(work,filename,retry_all){
var dir = 'work-' + work.id;
exec('cd ' + options.tmp + ' && rm -f ' + dir + ' && mkdir '+dir+' && tar --strip-components=1 -C '+dir+' -zxvf ' + filename, function (error, stdout, stderr) {
console.log(stdout);
console.log(stderr);
if(error){
console.log("Could not decompress work to dir " + dir);
console.log(error);
retry_all();
}else{
console.log("Decompressed work!");
do_work(work,retry_all);
}
});
}
function download_work(work,retry_all){
var url = base_url + '/download_work/' + work.id;
var retry = function(){_.delay(function(){download_work(work,retry_all)},1000);};
restler.get(url,{
}).on('success',function(data,response){
var filename = 'work-' + work.id + '.tar.gz';
var path = options.tmp + '/' + filename;
fs.writeFile(path, response.raw, 'binary', function (err) {
if(err){
console.log("Error: could not save work file " + path);
retry();
}else{
console.log("Saved file with work as " + path);
decompress_work(work,filename,retry_all);
}
});
}).on("fail", function(data,response){
console.log("Failed request to " + url);
console.log(data);
console.log(response);
retry_all();
}).on("error",function(err,response){
console.log("Error when performing request to " + url);
console.log(err);
console.log(response);
retry();
});
}
function loop(){
var url = base_url + '/give_me_work';
var retry = function(){_.delay(loop,1000);};
restler.get(url,{
}).on('success',function(work){
console.log(work);
if(work === null){
console.log("nothing to do ..");
retry();
}else{
download_work(work,retry);
}
}).on("fail", function(data,response){
console.log("Failed request to " + url);
console.log(data);
console.log(response);
retry();
}).on("error",function(err,response){
console.log("Error when performing request to " + url);
console.log(err);
console.log(response);
retry();
});
}
loop();