Skip to content

Commit

Permalink
feat(processor): clean orphaned bots on process start (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
bludnic committed Jul 28, 2024
1 parent cbdc3e4 commit 1d7c466
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions packages/bot/src/processing/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export class Processor {
}

async onApplicationBootstrap() {
await this.cleanOrphanedBots();

logger.info("[Processor] OrdersProcessor created");
await this.exchangeAccountsWatcher.create();

Expand All @@ -34,7 +36,7 @@ export class Processor {
}

async beforeApplicationShutdown() {
await this.stopAllBots();
await this.stopEnabledBots();

logger.info("[Processor] OrdersProcessor destroyed");
await this.exchangeAccountsWatcher.destroy();
Expand All @@ -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);
Expand All @@ -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:
*
Expand Down

0 comments on commit 1d7c466

Please sign in to comment.