Skip to content

Commit

Permalink
Remove cancelable-promise, use p-timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
claabs committed Feb 28, 2024
1 parent eed38bd commit ba96736
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 55 deletions.
12 changes: 1 addition & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
},
"dependencies": {
"axios": "^1.6.7",
"cancelable-promise": "^4.3.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"cron-parser": "^4.9.0",
Expand All @@ -39,6 +38,7 @@
"open": "^10.0.3",
"otpauth": "^9.2.2",
"p-queue": "^8.0.1",
"p-timeout": "^6.1.2",
"pidtree": "^0.6.0",
"pino": "^8.19.0",
"pino-pretty": "^10.3.1",
Expand Down
66 changes: 30 additions & 36 deletions src/common/puppeteer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { Page, Protocol, Browser, executablePath } from 'puppeteer';
import objectAssignDeep from 'object-assign-deep';
import StealthPlugin from 'puppeteer-extra-plugin-stealth';
import { Logger } from 'pino';
// eslint-disable-next-line import/extensions
import { cancelable } from 'cancelable-promise/esm/CancelablePromise.mjs';
import pTimeout, { TimeoutError } from 'p-timeout';
import pidtree from 'pidtree';
import findProcess from 'find-process';
import { ETCCookie, ToughCookieFileStore } from './cookie.js';
Expand Down Expand Up @@ -135,42 +134,37 @@ const retryFunction = async <T>(
const TIMEOUT = config.browserLaunchTimeout * 1000;
const MAX_ATTEMPTS = config.browserLaunchRetryAttempts;
const beforeProcesses = await pidtree(process.pid);
const newPageCancelable = cancelable(f());
let timeoutInstance: NodeJS.Timeout | undefined;
const res = await Promise.race([
newPageCancelable,
new Promise((resolve) => {
timeoutInstance = setTimeout(resolve, TIMEOUT);
}).then(() => 'timeout'),
]);
if (typeof res !== 'string') {
if (timeoutInstance) clearTimeout(timeoutInstance);
return res;
}
newPageCancelable.cancel();
const afterProcesses = await pidtree(process.pid);
const newProcesses = await Promise.all(
afterProcesses
.filter((p) => !beforeProcesses.includes(p))
.map(async (p) => (await findProcess('pid', p))[0]),
);
const chromiumProcesses = newProcesses.filter(
(p): p is NonNullable<typeof p> =>
p !== undefined && ['chromium', 'chrome', 'headless_shell'].some((n) => p.name.includes(n)),
);
L.debug({ chromiumProcesses }, 'Killing new browser processes spawned');
chromiumProcesses.forEach((p) => process.kill(p.pid));
if (attempts >= MAX_ATTEMPTS) {
L.error(
`If not already, consider using the Debian (:bullseye-slim) version of the image. More: https://github.com/claabs/epicgames-freegames-node#docker-configuration`,
try {
return await pTimeout(f(), { milliseconds: TIMEOUT });
} catch (err) {
if (!(err instanceof TimeoutError)) {
throw err;
}
// If the function timed out
const afterProcesses = await pidtree(process.pid);
const newProcesses = await Promise.all(
afterProcesses
.filter((p) => !beforeProcesses.includes(p))
.map(async (p) => (await findProcess('pid', p))[0]),
);
throw new Error(`Could not do ${outputName} after ${MAX_ATTEMPTS + 1} failed attempts.`);
const chromiumProcesses = newProcesses.filter(
(p): p is NonNullable<typeof p> =>
p !== undefined && ['chromium', 'chrome', 'headless_shell'].some((n) => p.name.includes(n)),
);
L.debug({ chromiumProcesses }, 'Killing new browser processes spawned');
chromiumProcesses.forEach((p) => process.kill(p.pid));
if (attempts >= MAX_ATTEMPTS) {
L.error(
`If not already, consider using the Debian (:bullseye-slim) version of the image. More: https://github.com/claabs/epicgames-freegames-node#docker-configuration`,
);
throw new Error(`Could not do ${outputName} after ${MAX_ATTEMPTS + 1} failed attempts.`);
}
L.warn(
{ attempts, MAX_ATTEMPTS },
`${outputName} did not work after ${TIMEOUT}ms. Trying again.`,
);
return retryFunction(f, L, outputName, attempts + 1);
}
L.warn(
{ attempts, MAX_ATTEMPTS },
`${outputName} did not work after ${TIMEOUT}ms. Trying again.`,
);
return retryFunction(f, L, outputName, attempts + 1);
};

/**
Expand Down
6 changes: 0 additions & 6 deletions src/types/cancelable-promise.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// Types
"typeRoots": [
"node_modules/@types",
"src/types"
], /* List of folders to include type definitions from. */
// Output
"outDir": "./dist", /* Redirect output structure to the directory. */
Expand Down

0 comments on commit ba96736

Please sign in to comment.