-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscheduler.js
325 lines (286 loc) · 9.06 KB
/
scheduler.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
var util = require("./util.js")
var log = require("./log.js")
var fs = require("fs")
EventEmitter = require("events").EventEmitter
module.exports = exports = new EventEmitter()
exports.setMaxListeners(0)
// Maximum number of jobs to run at same time.
var params = { concurrency : 40 }
var runningWorks = []
var worksQueue = []
function addURLs(source, res, callback){
callback = callback || function () {}
var finished = []
source.forEach(function (url, partnum) {
addURL(url, partnum, res, function (work) {
finished.push(work)
if (finished.length == source.length) {
finished.sort(function (a,b) {
return +a.id.split("-")[1] - b.id.split("-")[1]
})
callback(finished)
}
})
})
}
exports.addURLs = addURLs
function addURL(url, partnum, res, callback) {
log.logres("Called with url = " + url, res.options)
var work = newWork(url, partnum, res.options, callback)
work.res = res
work.partnum = partnum
if (work.error !== "") {
callback(work)
return
}
worksQueue.push(work)
log.logres("Calling run().", res.options)
run()
}
exports.addURL = addURL
var plugins = []
var defaultPlugin = require("./plugins/default.js")
fs.readdir(__dirname + "/plugins", function (err, files) {
if (!err) {
files.forEach(function (file) {
if (file !== "default.js") {
var p = require("./plugins/"+file)
noExtract = false;
if (!p.extractSignature) {
noExtract = true;
}
p.__proto__ = defaultPlugin
if (noExtract) {
// This will trigger an error in newWork function
p.extractSignature = "";
}
plugins.push(p)
}
})
}
})
exports.plugins = plugins;
function run() {
while (runningWorks.length < params.concurrency && worksQueue.length > 0) {
var work = worksQueue.shift()
runningWorks.push(work)
if (work.options.respectHeaders) {
work.cacheCheckStartTime = new Date();
}
log.logres("Calling util.isCached.", work.options, "scheduler")
util.isCached(work, function (work) {
work.cacheCheckFinishedTime = new Date()
log.logres("Callback.", work.options, "scheduler")
log.logres("work.foundInCache = " + work.foundInCache, work.options, "scheduler")
log.logres("options.forceUpdate = " + work.options.forceUpdate, work.options, "scheduler")
log.logres("options.forceWrite = " + work.options.forceWrite, work.options, "scheduler")
log.logres("options.respectHeaders = " + work.options.respectHeaders, work.options, "scheduler")
log.logres("work.isExpired = " + work.isExpired, work.options, "scheduler")
if (!work.foundInCache || work.options.forceUpdate || (work.options.respectHeaders && work.isExpired)) {
log.logres("Calling work.preprocess().", work.options, "scheduler")
work.preprocess(function (err, work) {
work.processStartTime = new Date()
log.logres("Calling work.process().", work.options, "scheduler")
work.process(function (err, work) {
log.logres("Calling work.postprocess().", work.options, "scheduler")
work.postprocess(function (err, work) {
work.processFinishedTime = new Date()
log.logres("Calling workFinish().", work.options, "scheduler")
workFinish(work)
})
})
})
} else {
log.logres("Cache hit.", work.options, "scheduler")
log.logres("Setting work.isFromCache = true.", work.options, "scheduler")
log.logres("Calling workFinish().", work.options, "scheduler")
work.isFromCache = true
workFinish(work)
}
function workFinish(work) {
runningWorks.remove(work)
log.logres("Called.", work.options, "scheduler")
log.logres("work.error = " + work.error, work.options, "scheduler")
log.logres("work.retries = " + work.retries, work.options, "scheduler")
if (work.error === "") {
log.logres("Calling work.callback(work2result(work))", work.options, "scheduler")
work.callback(work2result(work))
return
}
if (work.abort) {
log.logres("Aborting work due to error: "+work.error, work.options, "scheduler")
work.callback(work2result(work))
} else if (work.error !== "" && work.retries < work.options.maxTries) {
log.logres("Will retry " + work.url, work.options, "scheduler")
work.retries += 1
worksQueue.push(work)
} else {
log.logres("Finished max number of tries (" + work.options.maxTries + ") for "+work.url, work.options, "scheduler")
work.callback(work2result(work))
}
run()
}
})
}
if (worksQueue.length > 0) {
if (typeof(setImmediate) !== "undefined") {
setImmediate(run)
} else {
process.nextTick(run)
}
}
}
function work2result(work) {
work.work2ResultStartTime = new Date()
delete work.res
delete work.body
if (!work.options.includeData) {
delete work.data
}
if (work.options.includeMeta) {
if (!work.hasOwnProperty("meta")) {
work["meta"] = {}
}
if (!work.hasOwnProperty("datax")) {
work["datax"] = {}
}
if (typeof(work["meta"]) === "undefined") {
work["meta"] = work["datax"]
} else if (work["meta"].length == 0) {
work["meta"] = work["datax"]
} else {
work["meta"] = work["meta"]
}
}
work.work2ResultFinishedTime = new Date()
work.jobFinishedTime = new Date()
return work
}
function getPlugin(options, url) {
var plugin
if (options.plugin) {
// Find plug-in by name
plugin = plugins.find(function (d) {return d.name === options.plugin}) || ""
} else {
// Find plug-in that returns true given a URL
plugin = plugins.find(function (d) {return d.match(url)}) || defaultPlugin
}
return plugin
}
exports.getPlugin = getPlugin;
function getId() {
if (!getId.jobId) {getId.jobId = 1}
var now = new Date().toISOString().substring(0,10).replace(/-/g,"")+"-"+getId.jobId
getId.jobId = getId.jobId + 1
return now
}
function newWork(url, partnum, options, callback){
// TODO: Check if plugin changed on disk. If so, re-load it.
plugin = getPlugin(options, url)
// Make a copy of options so we can add partnum that will not change.
var optionsc = JSON.parse(JSON.stringify(options))
optionsc.partnum = partnum
pluginerror = ""
if (plugin === "") {
log.logres("Error: plugin "+options.plugin+" not found.", options)
pluginerror = "Error: plugin "+options.plugin+" not found."
} else {
pluginerror = ""
}
if (pluginerror !== "") {
return {error: pluginerror, errorcode: 500, options: optionsc}
}
var extractSignature = ""
//console.log(plugin)
// TODO: This will never happen unless default.js has extractSignature function removed.
if (plugin.extractSignature) {
extractSignature = plugin.extractSignature(options)
log.logres("MD5(URL): " + util.md5(url), options, "scheduler")
log.logres("MD5(URL + extractSignature): " + util.md5(url+extractSignature), options, "scheduler")
} else {
pluginerror = "Error: plugin "+options.plugin+" does not have an extractSignature."
}
if (pluginerror !== "") {
return {error: pluginerror, errorcode: 500, options: optionsc}
}
var work = {
id: getId(),
plugin : plugin,
url : url,
options : optionsc ? optionsc : {},
dataMd5 : "",
dataLength : -1,
urlMd5 : util.md5(url+extractSignature),
urlMd5base: util.md5(url),
time: 0,
data: "",
header: {},
dir: "",
lstat: {},
versions: [],
isFromCache : false,
isExpired : false,
foundInCache: false,
error: "",
jobStartTime : new Date(),
processStartTime : 0,
cacheCheckStartTime : 0,
cacheCheckFinishedTime : 0,
cacheCheckError : "",
cacheWriteStartTime : 0,
cacheWriteFinishedTime : 0,
cacheWriteError : "",
headCheckStartTime : 0,
headCheckFinishedTime : 0,
headLastModified : 0,
headInCacheLastModified : 0,
headCheckError: false,
getStartTime : 0,
getFirstChunkTime: 0,
getFinishedTime : 0,
work2ResultStartTime: 0,
work2ResultFinishedTime: 0,
processFinishedTime : 0,
jobFinishedTime : 0,
abort: false,
retries : 0,
callback : callback || function () {},
process : function (callback) {
exports.emit("process", this);
this.plugin.process(this, callback);
},
preprocess : function (callback) {
exports.emit("preprocess", this);
this.plugin.preprocess(this, callback);
},
postprocess : function (callback) {
exports.emit("postprocess", this);
this.plugin.postprocess(this, callback);
},
extractData: function (data, options) {
exports.emit("extractdata", this);
if (data.length == 0) {
return "\n";
}
return this.plugin.extractData(data, options);
},
extractDataBinary: function (data, options) {
exports.emit("extractdatabinary", this);
return this.plugin.extractDataBinary(data, options);
},
extractDataJson: function (data, options) {
return this.plugin.extractDataJson(data, options);
},
extractMeta: function (data, options) {
return this.plugin.extractMeta(data, options);
},
extractMetaJson: function (data, options) {
return this.plugin.extractMetaJson(data, options);
},
extractRem: function (data, options) {
return this.plugin.extractRem(data, options);
}
}
return work
}
exports.newWork = newWork