Skip to content

Commit

Permalink
refactor: config
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Oct 25, 2024
1 parent 702f5ce commit 0a5d17d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docker-compose.provernet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ services:
BOT_NO_WAIT_FOR_TRANSFERS: true
BOT_NO_START: false
BOT_MAX_CONSECUTIVE_ERRORS: 3
BOT_STOP_WHEN_UNHEALTHY: true
PXE_PROVER_ENABLED: "${PROVER_REAL_PROOFS:-false}"
PROVER_REAL_PROOFS: "${PROVER_REAL_PROOFS:-false}"
BB_SKIP_CLEANUP: "${BB_SKIP_CLEANUP:-0}" # Persist tmp dirs for debugging
Expand All @@ -151,7 +152,7 @@ services:
test: [ "CMD", "curl", "-fSs", "http://127.0.0.1:80/status" ]
interval: 3s
timeout: 30s
start_period: 10s
start_period: 90s
restart: on-failure:5
command: [ "start", "--bot", "--pxe" ]

Expand Down
7 changes: 7 additions & 0 deletions yarn-project/bot/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export type BotConfig = {
contract: SupportedTokenContracts;
/** The maximum number of consecutive errors before the bot shuts down */
maxConsecutiveErrors: number;
/** Stops the bot if service becomes unhealthy */
stopWhenUnhealthy: boolean;
};

export const botConfigMappings: ConfigMappingsType<BotConfig> = {
Expand Down Expand Up @@ -171,6 +173,11 @@ export const botConfigMappings: ConfigMappingsType<BotConfig> = {
description: 'The maximum number of consecutive errors before the bot shuts down',
...numberConfigHelper(0),
},
stopWhenUnhealthy: {
env: 'BOT_STOP_WHEN_UNHEALTHY',
description: 'Stops the bot if service becomes unhealthy',
...booleanConfigHelper(false),
},
};

export function getBotConfigFromEnv(): BotConfig {
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/bot/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ import { type BotRunner } from './runner.js';
* @returns An JSON-RPC HTTP server
*/
export function createBotRunnerRpcServer(botRunner: BotRunner) {
return new JsonRpcServer(botRunner, { AztecAddress, EthAddress, Fr, TxHash }, {}, [], () => botRunner.isRunning());
return new JsonRpcServer(botRunner, { AztecAddress, EthAddress, Fr, TxHash }, {}, [], () => botRunner.isHealthy());
}
15 changes: 12 additions & 3 deletions yarn-project/bot/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class BotRunner {
private node: AztecNode;
private runningPromise: RunningPromise;
private consecutiveErrors = 0;
private healthy = true;

public constructor(private config: BotConfig, dependencies: { pxe?: PXE; node?: AztecNode }) {
this.pxe = dependencies.pxe;
Expand Down Expand Up @@ -53,6 +54,10 @@ export class BotRunner {
this.log.info(`Stopped bot`);
}

public isHealthy() {
return this.runningPromise.isRunning() && this.healthy;
}

/** Returns whether the bot is running. */
public isRunning() {
return this.runningPromise.isRunning();
Expand Down Expand Up @@ -134,10 +139,14 @@ export class BotRunner {
} catch (err) {
// Already logged in run()
if (this.config.maxConsecutiveErrors > 0 && this.consecutiveErrors >= this.config.maxConsecutiveErrors) {
this.log.error(`Too many consecutive errors, stopping bot`);
await this.stop();
process.exit(1); // workaround docker not restarting the container if its unhealthy. We have to exit instead
this.log.error(`Too many errors bot is unhealthy`);
this.healthy = false;
}
}

if (!this.healthy && this.config.stopWhenUnhealthy) {
this.log.error(`Stopping bot due to errors`);
process.exit(1); // workaround docker not restarting the container if its unhealthy. We have to exit instead
}
}
}
1 change: 1 addition & 0 deletions yarn-project/foundation/src/config/env_var.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type EnvVar =
| 'BOT_TX_INTERVAL_SECONDS'
| 'BOT_TX_MINED_WAIT_SECONDS'
| 'BOT_MAX_CONSECUTIVE_ERRORS'
| 'BOT_STOP_WHEN_UNHEALTHY'
| 'COINBASE'
| 'DATA_DIRECTORY'
| 'DEBUG'
Expand Down

0 comments on commit 0a5d17d

Please sign in to comment.