-
Notifications
You must be signed in to change notification settings - Fork 15
/
worker_api.js
179 lines (153 loc) · 7.08 KB
/
worker_api.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
/**
* WORKER API
* This thing is being queried by the workers.
* The whole thing is hooked up to /workers from server.js.
*
* Baseline facts:
* - job_ids have the format: problemId_phaseType_inputKey
*/
var express = require("express"),
sys = require("sys"),
Problem = require('./models/problem').Problem,
Job = require('./models/job').Job,
dataa = require("./models/data_abstraction"),
Step = require("step"),
Datum = require("./models/datum").Datum;
module.exports = function(socketio) {
var jobs_per_minute = 0;
setInterval(function(){
socketio.broadcast(JSON.stringify({time: new Date().getTime(), jobs_per_minute: jobs_per_minute}));
}, 2000);
var app = express.createServer();
/**
* Here's what a job look like:
*
* {
id: "some_job_id",
problem_id: "some_problem_id",
algorithm: "function map(k,v) { ... }",
algorithm_type: "map",
created_at: "ms since epoch",
input: {
key: "value"
}
* }
*
*/
app.get("/job", function(req, res) {
jobs_per_minute++;
setTimeout(function() {
jobs_per_minute--;
}, 60000)
dataa.getNextJob(function(err, job) {
if (err) {
res.send({
"message": err.message,
"stack": err.stack
}, 500);
} else {
// NO MORE JOBS
if (job === null) {
res.send({
"message": "(singing) No jobs today, the jobs have gone away, lalalalala, it wasn't always so."
}, 500);
return;
}
res.send({
id: job._id,
problem_id: job.problem_id,
input: job.input,
algorithm: job.algorithm,
algorithm_type: job.algorithm_type
});
//job.status = "processing";
// dataa.saveJob(job, function(err, data) {
// // I don't care
// console.log("saved job after get /job with arguments " + sys.inspect(arguments));
// });
}
});
});
/**
* Posts the result of a job back.
* NOTE: The client is responsible for pulling a new job. We don't do any redirect vodoo or whatever
* TODO: Do redirect vodoo. Let's squeeze out another 100ms of proc power
//*/
app.post("/job/:jobId", function(req, res) {
console.log("Received results "+sys.inspect(req.body));
dataa.findJob(req.params.jobId, function (err, job) {
if (job) {
var results = req.body;
// create a datum for each k,v pair
Step(
function() {
var group = this.group();
// mark job as done
job.status = 'completed';
//job.status = 'queued';
dataa.saveJob(job, group());
for (var i=0, l=results.length; i<l; i++) {
dataa.saveDatum(new Datum({
problemId: job.problem_id,
key: results[i].key,
values: [results[i].value],
dataType: (job.algorithm_type === 'map' ? 'intermediate' : 'output')
}), group());
}
},
// once all the datums are saved
function() {
dataa.hasUnfinishedJobsByProblemId(job.problem_id, function (err, hasUnfinishedJobs) {
res.send("ok");
if (!hasUnfinishedJobs) {
// kick off next step by
// 1) find intermediate datum
dataa.findProblem(job.problem_id, function (err, problem) {
if (!err) {
dataa.findIntermediateDataByProblemId(job.problem_id, function (err, data) {
if (job.algorithm_type === "reduce"){ // we're done!
problem.status = 'complete';
dataa.saveProblem(problem, function() {
// TODO this doesn't work at all
console.log("HOLYCRAP WERE DONE!")
});
return;
}
else if (!err) {
for (var i in data) {
if (data[i].key !== "") {
var input = {
key: data[i].key,
value: data[i].values
};
// TODO 2) add a bunch of reduce jobs to the queue
dataa.saveJob(new Job({
problem_id: job.problem_id,
algorithm: problem.reduce_function,
algorithm_type: 'reduce',
input: input
}), function(err, res) {
// i don't care
})
}
}
}
return;
});
} else {
return;
}
});
}
});
}
);
} else {
res.send(err.message + "\n" + err.stack, 500);
// TODO mark this job as failed. roll it back. whatever
}
});
});
//*/
return app;
};