Skip to content

Commit

Permalink
refactor: Remove top-level bot loop (take two) (#1404)
Browse files Browse the repository at this point in the history
This change was previously merged and backed out shortly after because
it was seen to result in some bot instances hanging. This is almost
certainly because the original commit removed two instances of
process.exit(), and replaced them with prococess.exitCode = . There
appear to be some instances where the bot can have some dangling
promises that take a long time to resolve, or that don't resolve for 
some reason. Replaying the change, but without removing the 
process.exit() calls should be OK.
  • Loading branch information
pxrl authored Apr 12, 2024
1 parent d6af852 commit ba5a2b2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 59 deletions.
52 changes: 16 additions & 36 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import minimist from "minimist";
import { CommonConfig } from "./src/common";
import {
AnyObject,
config,
delay,
retrieveSignerFromCLIArgs,
help,
Logger,
processCrash,
usage,
winston,
} from "./src/utils";
import { config, delay, exit, retrieveSignerFromCLIArgs, help, Logger, usage, winston } from "./src/utils";
import { runRelayer } from "./src/relayer";
import { runDataworker } from "./src/dataworker";
import { runMonitor } from "./src/monitor";
import { runFinalizer } from "./src/finalizer";
import { version } from "./package.json";

let logger: winston.Logger;
let cmd: string;

export async function run(args: { [k: string]: boolean | string }): Promise<void> {
logger = Logger;

const config = new CommonConfig(process.env);

const cmds = {
dataworker: runDataworker,
finalizer: runFinalizer,
Expand All @@ -35,7 +23,7 @@ export async function run(args: { [k: string]: boolean | string }): Promise<void
// todo Make the mode of operation an operand, rather than an option.
// i.e. ts-node ./index.ts [options] <relayer|...>
// Note: ts does not produce a narrow type from Object.keys, so we have to help.
const cmd = Object.keys(cmds).find((_cmd) => !!args[_cmd]);
cmd = Object.keys(cmds).find((_cmd) => !!args[_cmd]);

if (cmd === "help") {
cmds[cmd]();
Expand All @@ -47,19 +35,10 @@ export async function run(args: { [k: string]: boolean | string }): Promise<void
// todo: Update usage() to provide a hint that wallet is missing/malformed.
usage(""); // no return
} else {
// One global signer for use with a specific per-chain provider.
// todo: Support a void signer for monitor mode (only) if no wallet was supplied.
const signer = await retrieveSignerFromCLIArgs();
do {
try {
// One global signer for use with a specific per-chain provider.
// todo: Support a void signer for monitor mode (only) if no wallet was supplied.
await cmds[cmd](logger, signer);
} catch (error) {
if (await processCrash(logger, cmd, config.pollingDelay, error as AnyObject)) {
// eslint-disable-next-line no-process-exit
process.exit(1);
}
}
} while (config.pollingDelay !== 0);
await cmds[cmd](logger, signer);
}
}

Expand All @@ -78,19 +57,20 @@ if (require.main === module) {

const args = minimist(process.argv.slice(2), opts);

let exitCode = 0;
run(args)
.then(() => {
// eslint-disable-next-line no-process-exit
process.exit(0);
})
.catch(async (error) => {
exitCode = 1;
logger.error({
at: "InfrastructureEntryPoint",
message: "There was an error in the main entry point!",
at: cmd ?? "unknown process",
message: "There was an execution error!",
reason: error,
e: error,
error,
args,
notificationPath: "across-error",
});
await delay(5);
await run(args);
});
await delay(5); // Wait for transports to flush. May or may not be necessary.
})
.finally(() => exit(exitCode));
}
29 changes: 6 additions & 23 deletions src/utils/ExecutionUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { AnyObject, delay, winston } from "./";
import { delay, winston } from "./";

export function exit(code: number) {
// eslint-disable-next-line no-process-exit
process.exit(code);
}

export async function processEndPollingLoop(
logger: winston.Logger,
Expand All @@ -16,28 +21,6 @@ export async function processEndPollingLoop(
return false;
}

export async function processCrash(
logger: winston.Logger,
fileName: string,
pollingDelay: number,
error: AnyObject
): Promise<boolean> {
logger.error({
at: `${fileName}#index`,
message: `There was an execution error! ${pollingDelay != 0 ? "Re-running loop" : ""}`,
reason: error,
e: error,
error,
notificationPath: "across-error",
});
await delay(5);
if (pollingDelay === 0) {
return true;
}

return false;
}

export function startupLogLevel(config: { pollingDelay: number }): string {
return config.pollingDelay > 0 ? "info" : "debug";
}
Expand Down

0 comments on commit ba5a2b2

Please sign in to comment.