This repository was archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathcrawlerService.js
189 lines (163 loc) · 4.8 KB
/
crawlerService.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const Q = require('q');
const Request = require('./request');
class CrawlerService {
constructor(crawler, options) {
this.crawler = crawler;
this.options = options;
this.loops = [];
}
ensureInitialized() {
// deferred initialization of the options and crawler. When they are available, swap them in and listen for changes
if (typeof this.crawler.then !== 'function') {
return Q();
}
return this.crawler.then(([crawler, options]) => {
this.options = options;
this.options.crawler._config.on('changed', this._reconfigure.bind(this));
this.crawler = crawler;
}).then(() => {
return this.crawler.initialize ? this.crawler.initialize() : null;
});
}
run() {
return this.ensureInitialized().then(() => {
return this.ensureLoops();
});
}
_loopComplete(loop) {
console.log(`Done loop ${loop.options.name}`);
}
ensureLoops() {
this.loops = this.loops.filter(loop => loop.running());
const running = this.status();
const delta = this.options.crawler.count - running;
if (delta < 0) {
for (let i = 0; i < Math.abs(delta); i++) {
const loop = this.loops.shift();
loop.stop();
}
} else {
for (let i = 0; i < delta; i++) {
const loop = new CrawlerLoop(this.crawler, i.toString());
loop.run().finally(this._loopComplete.bind(this, loop));
this.loops.push(loop);
}
}
return Q();
}
status() {
return this.loops.reduce((running, loop) => {
return running + (loop.running ? 1 : 0);
}, 0);
}
stop() {
return this.ensureLoops();
}
queues() {
return this.crawler.queues;
}
queue(requests, name) {
return this.crawler.queue(requests, name);
}
flushQueue(name) {
const queue = this.crawler.queues.getQueue(name);
if (!queue) {
return Q(null);
}
return queue.flush();
}
getQueueInfo(name) {
const queue = this.crawler.queues.getQueue(name);
if (!queue) {
return Q.reject(`No queue found: ${name}`);
}
return queue.getInfo();
}
getRequests(name, count, remove = false) {
const queue = this.crawler.queues.getQueue(name);
if (!queue) {
return Q(null);
}
const result = [];
for (let i = 0; i < count; i++) {
result.push(queue.pop());
}
return Q.all(result).then(requests => {
const filtered = requests.filter(request => request);
return Q.all(filtered.map(request => remove ? queue.done(request) : queue.abandon(request))).thenResolve(filtered);
});
}
listDeadletters() {
return this.crawler.deadletters.list('deadletter');
}
getDeadletter(urn) {
return this.crawler.deadletters.get('deadletter', urn);
}
deleteDeadletter(urn) {
return this.crawler.deadletters.delete('deadletter', urn);
}
requeueDeadletter(url, queue) {
const self = this;
return this.getDeadletter(url)
.then(document => {
const request = Request.adopt(document).createRequeuable();
request.attemptCount = 0;
return self.crawler.queues.push([request], queue)
})
.then(() => {
return self.deleteDeadletter(url);
});
}
getDeadletterCount() {
return this.crawler.deadletters.count('deadletter');
}
_reconfigure(current, changes) {
// if the loop count changed, make it so
if (changes.some(patch => patch.path === '/count')) {
return this.options.crawler.count.value > 0 ? this.run() : this.stop();
}
return null;
}
}
class CrawlerLoop {
constructor(crawler, name) {
this.crawler = crawler;
this.options = { name: name, delay: 0 };
this.done = null;
this.state = null;
}
running() {
return this.state === 'running';
}
run() {
if (this.state) {
throw new Error(`Loop ${this.options.name} can only be run once`);
}
this.state = 'running';
// Create callback that when run, resolves a promise and completes this loop
const doneDeferred = Q.defer();
this.done = value => doneDeferred.resolve(value);
this.options.done = this.done;
const donePromise = doneDeferred.promise;
donePromise.finally(() => {
this.state = 'stopped';
});
// Kick off the loop and don't worry about the return value.
// donePromise will be resolved when the loop is complete.
this.crawler.run(this.options);
return donePromise;
}
stop() {
if (this.state === 'stopped' || this.state === 'stopping') {
return;
}
this.state = 'stopping';
// set delay to tell the loop to stop next time around
// TODO consider explicitly waking sleeping loops but they will check whether they
// should keep running when they wake up.
this.options.delay = -1;
}
}
module.exports = CrawlerService;