Skip to content

Commit

Permalink
better browser object mgnmgnt
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturWierzbicki committed Jan 10, 2022
1 parent 60c0693 commit 27c3af2
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/browser/BrowserPerRequestHashConcurrency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,18 @@ type WorkerDeps = {
class Worker<JobData> implements WorkerInstance<JobData> {
private currentBrowser: puppeteer.Browser;
private repairing = false;
private isClosed = false;
private openInstances = 0;
private waitingForRepairResolvers: Array<(val?: unknown) => void> = [];
private browserHealthCheckInterval: NodeJS.Timeout;

constructor(private deps: WorkerDeps) {
this.currentBrowser = deps.browser;
this.browserHealthCheckInterval = setInterval(() => {
if (!this.currentBrowser.isConnected()) {
this.close();
}
}, 20000);
}

async repair() {
Expand All @@ -163,6 +170,11 @@ class Worker<JobData> implements WorkerInstance<JobData> {
this.deps.log.debug('Unable to close browser', 'jobHash', this.deps.jobHash, 'workerId', this.deps.id, 'err', err.stack);
}

if (this.isClosed) {
this.deps.onShutdown();
return;
}

try {
this.currentBrowser = await this.deps.puppeteer.launch(this.deps.launchOptions);
this.deps.log.debug('Worker finished repair', 'jobHash', this.deps.jobHash, 'workerId', this.deps.id);
Expand Down Expand Up @@ -215,7 +227,14 @@ class Worker<JobData> implements WorkerInstance<JobData> {
}

async close(): Promise<void> {
this.isClosed = true;

clearInterval(this.browserHealthCheckInterval);
await this.deps.onShutdown();

if (this.repairing) {
await this.repair();
}
this.deps.log.debug(`Closing worker`, 'jobHash', this.deps.jobHash, 'workerId', this.deps.id);
await this.currentBrowser.close();
}
Expand Down

0 comments on commit 27c3af2

Please sign in to comment.