Skip to content

Commit 9fe41c3

Browse files
committed
Update cluster concurrency to use Cluster.CONCURRENCY_CONTEXT
- Update the cluster concurrency to use Cluster.CONCURRENCY_CONTEXT instead of Cluster.CONCURRENCY_BROWSER for better context handling and error logging. Generated by gpt-3.5-turbo
1 parent 3c35f2d commit 9fe41c3

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,22 @@ Headless-cluster enables authenticated proxy support. Pass a data object to clus
77

88
```js
99
// Create a cluster with 2 workers
10+
// You can also use Cluster.CONCURRENCY_BROWSER
1011
const cluster = await Cluster.launch({
11-
concurrency: Cluster.CONCURRENCY_BROWSER,
12+
concurrency: Cluster.CONCURRENCY_CONTEXT,
1213
maxConcurrency: 2,
1314
});
1415

1516
// Define a task
1617
await cluster.task(async ({ page, data }) => {
18+
try {
1719
await page.goto(data.url);
18-
const pageTitle = await page.evaluate(() => document.title);
19-
return pageTitle;
20+
} catch (err) {
21+
console.log(err);
22+
return 'Failed to load the page';
23+
}
24+
const pageTitle = await page.evaluate(() => document.title);
25+
return pageTitle;
2026
});
2127

2228
// Use try-catch block as "execute" will throw instead of using events

examples/execute-proxy.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@ const { Cluster } = require('../dist');
22

33
(async () => {
44
// Create a cluster with 2 workers
5+
// You can also use Cluster.CONCURRENCY_BROWSER
56
const cluster = await Cluster.launch({
6-
concurrency: Cluster.CONCURRENCY_BROWSER,
7+
concurrency: Cluster.CONCURRENCY_CONTEXT,
78
maxConcurrency: 2,
89
});
910

1011
// Define a task
1112
await cluster.task(async ({ page, data }) => {
13+
try {
1214
await page.goto(data.url);
13-
const pageTitle = await page.evaluate(() => document.title);
14-
return pageTitle;
15+
} catch (err) {
16+
console.log(err);
17+
return 'Failed to load the page';
18+
}
19+
const pageTitle = await page.evaluate(() => document.title);
20+
return pageTitle;
1521
});
1622

1723
// Use try-catch block as "execute" will throw instead of using events
@@ -32,4 +38,4 @@ const { Cluster } = require('../dist');
3238
// Shutdown after everything is done
3339
await cluster.idle();
3440
await cluster.close();
35-
})();
41+
})();

src/concurrency/built-in/Context.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import SingleBrowserImplementation from '../SingleBrowserImplementation';
66

77
export default class Context extends SingleBrowserImplementation {
88

9-
protected async createResources(): Promise<ResourceData> {
9+
protected async createResources(data: any): Promise<ResourceData> {
1010
const context = await (this.browser as puppeteer.Browser)
11-
.createBrowserContext();
11+
.createBrowserContext(data.contextOptions || {});
1212
const page = await context.newPage();
13+
if (data.authentication)
14+
page.authenticate(data.authentication);
1315
return {
1416
context,
1517
page,

0 commit comments

Comments
 (0)