-
Notifications
You must be signed in to change notification settings - Fork 312
/
execute.js
31 lines (27 loc) · 947 Bytes
/
execute.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
const { Cluster } = require('../dist');
(async () => {
// Create a cluster with 2 workers
const cluster = await Cluster.launch({
concurrency: Cluster.CONCURRENCY_CONTEXT,
maxConcurrency: 2,
});
// Define a task
await cluster.task(async ({ page, data: url }) => {
await page.goto(url);
const pageTitle = await page.evaluate(() => document.title);
return pageTitle;
});
// Use try-catch block as "execute" will throw instead of using events
try {
// Execute the tasks one after another via execute
const result1 = await cluster.execute('https://www.google.com');
console.log(result1);
const result2 = await cluster.execute('https://www.wikipedia.org');
console.log(result2);
} catch (err) {
// Handle crawling error
}
// Shutdown after everything is done
await cluster.idle();
await cluster.close();
})();