From 1d7c466480cb4955c2676debfe344cfaf2c0b335 Mon Sep 17 00:00:00 2001 From: bludnic Date: Sun, 28 Jul 2024 06:17:28 +0100 Subject: [PATCH] feat(processor): clean orphaned bots on process start (#49) --- packages/bot/src/processing/processor.ts | 29 +++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/packages/bot/src/processing/processor.ts b/packages/bot/src/processing/processor.ts index c9c1b212..ac5f7d1c 100644 --- a/packages/bot/src/processing/processor.ts +++ b/packages/bot/src/processing/processor.ts @@ -21,6 +21,8 @@ export class Processor { } async onApplicationBootstrap() { + await this.cleanOrphanedBots(); + logger.info("[Processor] OrdersProcessor created"); await this.exchangeAccountsWatcher.create(); @@ -34,7 +36,7 @@ export class Processor { } async beforeApplicationShutdown() { - await this.stopAllBots(); + await this.stopEnabledBots(); logger.info("[Processor] OrdersProcessor destroyed"); await this.exchangeAccountsWatcher.destroy(); @@ -48,12 +50,17 @@ export class Processor { this.unsubscribeFromEventBus(); } - async stopAllBots() { + /** + * Stops enabled bots gracefully. + * Does execute "stop" command on each bot, and then sets the bot status as disabled. + */ + async stopEnabledBots() { const bots = await xprisma.bot.custom.findMany({ where: { enabled: true }, include: { exchangeAccount: true }, }); - logger.info(`[Processor] There a still ${bots.length} bots running, stopping them...`); + if (bots.length === 0) return; + logger.info(`[Processor] Stopping ${bots.length} bots gracefully…`); for (const bot of bots) { const botProcessor = new BotProcessing(bot); @@ -68,6 +75,22 @@ export class Processor { } } + /** + * When the app starts, check if there are any enabled bots and stop them gracefully. + * This is necessary because the previous process might have been interrupted, + * and there are open orders that need to be closed on the exchange. + */ + async cleanOrphanedBots() { + const anyBotEnabled = await xprisma.bot.custom.findFirst({ + where: { enabled: true }, + }); + + if (anyBotEnabled) { + logger.warn(`[Processor] The previous process was interrupted, there are orphaned bots. Performing cleanup…`); + await this.stopEnabledBots(); + } + } + /** * Subscribes to event bus events: *