Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add restart time for leaking Chromium in Ubuntu #1880

Merged
merged 4 commits into from Feb 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/reference/application.md
Expand Up @@ -18,6 +18,7 @@
| `PROXY_PROTOCOL` | Protocol of proxy server, such as `socks5`. Default: `http` |
| `PROXY_ADDRESS` | IP Address or fqdn of proxy server |
| `PROXY_PORT` | TCP Port number on which the proxy is listening for connections. Default: `80` |
| `RESTART_TIME` | Restarts chrome after defined milliseconds. `0` for never, default: `0` |
| `SCREENSHOT` | Capture screenshot of page if a card is found. Default: `true` |
| `WEB_PORT` | Starts a webserver to be able to control the bot while it is running. Setting this value starts this service. |

Expand Down
1 change: 1 addition & 0 deletions dotenv-example
Expand Up @@ -88,6 +88,7 @@ PUSHOVER_RETRY=
PUSHOVER_TOKEN=
PUSHOVER_USER=
PUSHOVER_PRIORITY=
RESTART_TIME=
SCREENSHOT=
SHOW_ONLY_BRANDS=
SHOW_ONLY_MODELS=
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Expand Up @@ -435,6 +435,8 @@ const store = {
}),
};

const restartTime = envOrNumber(process.env.RESTART_TIME, 0);

export const defaultStoreData = {
maxPageSleep: browser.maxSleep,
minPageSleep: browser.minSleep,
Expand All @@ -449,6 +451,7 @@ export const config = {
page,
proxy,
store,
restartTime,
};

export function setConfig(newConfig: any) {
Expand Down
16 changes: 16 additions & 0 deletions src/index.ts
Expand Up @@ -13,6 +13,21 @@ puppeteer.use(stealthPlugin());

let browser: Browser | undefined;

async function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}

/**
* Schedules a restart of the bot
*/
async function restartMain() {
if (config.restartTime > 0) {
await sleep(config.restartTime);
await stop();
loopMain();
}
}

/**
* Starts the bot.
*/
Expand Down Expand Up @@ -92,6 +107,7 @@ async function stopAndExit() {
*/
async function loopMain() {
try {
restartMain();
await main();
} catch (error: unknown) {
logger.error(
Expand Down