fix: tweak health check

This commit is contained in:
Yanlong Wang 2024-04-30 18:56:46 +08:00
parent 528b3e5fed
commit 55b954ffeb
No known key found for this signature in database
GPG Key ID: C0A623C0BADF9F37

View File

@ -151,21 +151,24 @@ export class PuppeteerControl extends AsyncService {
@maxConcurrency(1) @maxConcurrency(1)
async healthCheck() { async healthCheck() {
const healthyPage = await Promise.race([this.pagePool.acquire(3), delay(60_000).then(() => null)]).catch((err) => { this.pagePool.max += 1;
this.logger.error(`Health check failed`, { err: marshalErrorLike(err) }); const healthyPage = await this.pagePool.acquire(3).catch((err) => {
this.logger.warn(`Health check failed`, { err: marshalErrorLike(err) });
return null; return null;
}); });
this.pagePool.max -= 1;
if (healthyPage) { if (healthyPage) {
this.pagePool.release(healthyPage); this.pagePool.release(healthyPage);
return; return;
} }
this.logger.warn(`Health check failed, trying to clean up.`); this.logger.warn(`Trying to clean up...`);
await this.pagePool.clear(); await this.pagePool.clear();
this.browser.process()?.kill('SIGKILL'); this.browser.process()?.kill('SIGKILL');
Reflect.deleteProperty(this, 'browser'); Reflect.deleteProperty(this, 'browser');
this.emit('crippled'); this.emit('crippled');
this.logger.warn(`Browser killed`);
} }
async newPage() { async newPage() {