forked from odota/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.js
80 lines (78 loc) · 2.23 KB
/
requests.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
var utility = require('./utility');
var queue = require('./queue');
var getData = utility.getData;
var queries = require('./queries');
var redis = require('./redis');
var insertMatch = queries.insertMatch;
var db = require('./db');
var queueReq = utility.queueReq;
queue.request.process(100, processRequest);
function processRequest(job, cb)
{
var payload = job.data.payload;
if (payload.match_id)
{
//request match id, get data from API
getData(job.data.url, function(err, body)
{
if (err)
{
//couldn't get data from api, non-retryable
return cb(JSON.stringify(err));
}
//match details response
var match = body.result;
match.parse_status = 0;
insertMatch(db, redis, queue, match,
{
type: "api",
attempts: 1
}, waitParse);
});
}
else
{
//direct upload
queueReq(queue, "parse", payload,
{
attempts: 1
}, waitParse);
}
function waitParse(err, job2)
{
if (err)
{
console.error(err.stack || err);
return cb(err);
}
//job2 is the parse job
if (job.data.request && job2)
{
var poll = setInterval(function()
{
queue.parse.getJob(job2.jobId).then(function(job2)
{
job.progress(job2.progress());
job2.getState().then(function(state)
{
console.log("waiting for parse job %s, currently in %s", job2.jobId, state);
if (state === "completed")
{
clearInterval(poll);
return cb();
}
else if (state !== "active" && state !== "waiting")
{
clearInterval(poll);
return cb("failed");
}
}).catch(cb);
}).catch(cb);
}, 2000);
}
else
{
cb(err);
}
}
}