♻️ Factor out platform startup/shutdown, make reindexable more obviously extensible (#438)

This commit is contained in:
Eric Doughty-Papassideris
2024-03-26 12:06:22 +01:00
committed by ericlinagora
parent bde7e50779
commit 380e20c205
2 changed files with 126 additions and 59 deletions
@@ -0,0 +1,36 @@
import ora from "ora";
import config from "../../core/config";
import tdrive from "../../tdrive";
import gr from "../../services/global-resolver";
import type { TdrivePlatform } from "../../core/platform/platform";
/**
* Start the platform and its services, run the command (passed as
* the `handler` callback), then cleanly shut down the platform.
* @param prefix Prefix text to set on the Ora spinner
* @param handler Callback to run the actual command.
* - If it returns a number, that will be the exit code of the process.
* - No attempt to catch errors is done here. If it throws, the platform
* shutdown will be skipped and normal unhandled exception stuff will go on.
*/
export default async function runWithPlatform(
prefix: string,
handler: (args: {
spinner: ora.Ora;
config: config.IConfig;
platform: TdrivePlatform;
}) => Promise<number | undefined> | Promise<void>,
) {
const spinner = ora({ prefixText: prefix + " -" });
spinner.start("Platform: starting...");
const platform = await tdrive.run(config.get("services"));
await gr.doInit(platform);
spinner.succeed("Platform: started");
const exitCode = await handler({ spinner, config, platform });
if (typeof exitCode === "number") process.exitCode = exitCode;
spinner.start("Platform: shutting down...");
await platform.stop();
spinner.succeed("Platform: shutdown");
spinner.stop();
}