-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
224 lines (185 loc) · 5.01 KB
/
app.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
var nr = require('newrelic');
var _ = require('underscore');
var moment = require('moment');
var express = require('express');
var log = require('./logger');
var TIMEOUT = process.env.TIMEOUT || 500;
var SUCCESS = 200;
var ERROR = 500;
var conn = process.env.COUCH || 'http://localhost:5984';
// Basic Auth - for now, in v3 implement user/queue based auth
var auth = require('./lib/auth')(process.env.TOKEN, process.env.SECRET, process.env.SECRET2);
var agentkeepalive = require('agentkeepalive');
var myagent = new agentkeepalive({
maxSockets: 50,
maxKeepAliveRequests: 0,
maxKeepAliveTime: 30000
});
var nano = require('nano')(conn);
var db = nano.use(process.env.DB || 'cloudq');
var app = express();
var workers = {};
// TODO: User API
app.configure('development', function () {
app.use(logger());
});
app.configure('production', function () {
app.use(logger());
});
app.configure(function () {
app.use(express.json());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
// TODO: User API
// Cloudq API
// stats
app.get('/stats', function (req, res) {
db.view('queues', 'all', { group: true, reduce: true }, function (err, body) {
if (err) {
log.error(err);
return res.send(500, err.message);
}
var stats = statify(body.rows);
res.send(SUCCESS, stats);
});
});
// publish job
app.post('/:queue', auth, publish);
app.put('/:queue', auth, publish);
// consume job
app.get('/:queue', auth, function (req, res) {
db.view('queue', 'next', {
startkey: [req.params.queue, 1],
endkey: [req.params.queue, 100],
limit: 1
}, function (err, body) {
if (err) {
log.error(err);
return res.send(500, err);
}
// return 0 rows
if (!body.rows.length) {
// queue worker instead of returning response
if (!workers[req.params.queue]) workers[req.params.queue] = [];
workers[req.params.queue].push(res);
function dequeueResponse () {
workers[req.params.queue] = _(workers[req.params.queue]).without(res);
}
var responseTimeoutId = setTimeout(function () {
log.info({req: req}, 'Queue request timeout');
dequeueResponse();
res.send(SUCCESS, {status: 'empty'});
}, TIMEOUT);
res.once('close', function () {
log.info({req: req}, 'Queue request terminated');
clearTimeout(responseTimeoutId);
dequeueResponse();
});
return;
}
// have jobs so pass first one to resp worker...
var doc = body.rows[0];
db.atomic('dequeue', 'id', doc.id, function (err) {
if (err) {
log.error(err);
return res.send(500, err.message);
}
doc.value.id = doc.id;
doc.value.ok = true;
res.send(SUCCESS, doc.value);
});
});
});
// delete job - set state to complete
app.del('/:queue/:id', auth, function (req, res) {
db.atomic('complete', 'id', req.params.id, function (err, body) {
if (err) {
log.error(err);
return res.send(ERROR, err.message);
}
res.send({status: body});
});
});
module.exports = app;
//app.listen(process.env.PORT || 3000);
// lib
function logger () {
return function (req, res, next) {
var _start = new Date();
function logRequest () {
log.info({req: req, res: res});
log.info('Exec Time', (new Date()) - _start, 'ms');
}
res.once('finish', logRequest);
res.once('close', logRequest);
next();
};
}
function publish (req, res) {
if (!req.body) {
log.error('could not find body');
return res.send(ERROR, {error: 'must submit a job'});
}
var o = req.body;
if (!o.job) {
log.error('could not find job');
return res.send(ERROR, {error: 'job not found!'});
}
_.extend(o, {
type: req.params.queue,
state: 'published',
publishedAt: new Date(),
expires_in: moment().add('days', 2),
priority: o.priority || 100
});
db.insert(o, function (err, body) {
if (err) {
log.error(err);
return res.send(500, err);
}
res.send(SUCCESS, body);
o._id = body.id;
// could emit event for job added if changes queue doesn't work
notify(o);
});
}
function statify (rows) {
return _(rows).chain().filter(function (row) {
return _(row.key).has('state');
}).map(function (row) {
return {
type: row.key.type,
state: row.key.state,
value: row.value
};
})
.groupBy('type')
.map(function (v, k) {
var _value = {};
_(v).each(function (r) {
_value[r.state] = r.value;
});
return {key: k, value: _value};
})
.value();
}
// if worker is listening - notify..
function notify (doc) {
// find queue, find worker...
if (_.isArray(workers[doc.type]) && !_.isEmpty(workers[doc.type])) {
var wkr = workers[doc.type].shift();
// update doc as processing
db.atomic('dequeue', 'id', doc._id, function (err, body) {
if (err) {
log.error(err);
return wkr.send(ERROR, err);
}
var job = _.extend(doc.job, {
id: doc._id,
ok: true
});
wkr.send(SUCCESS, job);
});
}
}