From 9db0afd9dfc1846b4b378f52941e3ceff2c54f1a Mon Sep 17 00:00:00 2001 From: Benjamin Wilson Date: Mon, 9 Sep 2024 20:18:37 -0400 Subject: [PATCH] removed NODE_APP_INSTANCE 0 dependency --- ecosystem.config.js | 22 ++++++++++++++++++++++ src/ORM/rpc-block/rpc-block.entity.ts | 3 --- src/ORM/rpc-block/rpc-block.service.ts | 4 ---- src/services/app.service.ts | 2 +- src/services/bitcoin-rpc.service.ts | 14 ++------------ src/services/discord.service.ts | 10 +++++----- src/services/stratum-v1.service.ts | 2 +- 7 files changed, 31 insertions(+), 26 deletions(-) create mode 100644 ecosystem.config.js diff --git a/ecosystem.config.js b/ecosystem.config.js new file mode 100644 index 0000000..e1e82ec --- /dev/null +++ b/ecosystem.config.js @@ -0,0 +1,22 @@ +module.exports = { + apps: [ + // Master instance + { + name: 'master', + script: './dist/main.js', + instances: 1, + env: { + MASTER: 'true', + }, + }, + // Worker instances + { + name: 'workers', + script: './dist/main.js', + instances: 31, + env: { + MASTER: 'false', + }, + }, + ], + }; \ No newline at end of file diff --git a/src/ORM/rpc-block/rpc-block.entity.ts b/src/ORM/rpc-block/rpc-block.entity.ts index 2f65850..d87ecc3 100644 --- a/src/ORM/rpc-block/rpc-block.entity.ts +++ b/src/ORM/rpc-block/rpc-block.entity.ts @@ -6,9 +6,6 @@ export class RpcBlockEntity { @PrimaryColumn() blockHeight: number; - @Column({ nullable: true }) - lockedBy?: string; - @Column({ nullable: true }) data?: string; } \ No newline at end of file diff --git a/src/ORM/rpc-block/rpc-block.service.ts b/src/ORM/rpc-block/rpc-block.service.ts index f6de1db..65d8d4a 100644 --- a/src/ORM/rpc-block/rpc-block.service.ts +++ b/src/ORM/rpc-block/rpc-block.service.ts @@ -18,10 +18,6 @@ export class RpcBlockService { }); } - public lockBlock(blockHeight: number, process: string) { - return this.rpcBlockRepository.save({ blockHeight, data: null, lockedBy: process }); - } - public saveBlock(blockHeight: number, data: string) { return this.rpcBlockRepository.update(blockHeight, { data }) } diff --git a/src/services/app.service.ts b/src/services/app.service.ts index e055ebb..e8cb243 100644 --- a/src/services/app.service.ts +++ b/src/services/app.service.ts @@ -22,7 +22,7 @@ export class AppService implements OnModuleInit { } async onModuleInit() { - if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') { + if (process.env.MASTER == 'true') { setInterval(async () => { await this.deleteOldStatistics(); diff --git a/src/services/bitcoin-rpc.service.ts b/src/services/bitcoin-rpc.service.ts index 5a1bdff..2372494 100644 --- a/src/services/bitcoin-rpc.service.ts +++ b/src/services/bitcoin-rpc.service.ts @@ -99,22 +99,12 @@ export class BitcoinRpcService implements OnModuleInit { const completeBlock = block?.data != null; // If the block has already been loaded, and the same instance is fetching the template again, we just need to refresh it. - if (completeBlock && block.lockedBy == process.env.NODE_APP_INSTANCE) { + if (completeBlock && process.env.MASTER == 'true') { result = await this.loadBlockTemplate(blockHeight); } else if (completeBlock) { return Promise.resolve(JSON.parse(block.data)); - } else if (!completeBlock) { - if (process.env.NODE_APP_INSTANCE != null) { - // There is a unique constraint on the block height so if another process tries to lock, it'll throw - try { - await this.rpcBlockService.lockBlock(blockHeight, process.env.NODE_APP_INSTANCE); - } catch (e) { - result = await this.waitForBlock(blockHeight); - } - } - result = await this.loadBlockTemplate(blockHeight); - } else { + }else { //wait for block result = await this.waitForBlock(blockHeight); } diff --git a/src/services/discord.service.ts b/src/services/discord.service.ts index 999076a..522f706 100644 --- a/src/services/discord.service.ts +++ b/src/services/discord.service.ts @@ -34,7 +34,7 @@ export class DiscordService implements OnModuleInit { constructor(private readonly configService: ConfigService) { - if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') { + if (process.env.MASTER == 'true') { this.token = this.configService.get('DISCORD_BOT_TOKEN'); this.clientId = this.configService.get('DISCORD_BOT_CLIENTID'); this.guildId = this.configService.get('DISCORD_BOT_GUILD_ID'); @@ -61,7 +61,7 @@ export class DiscordService implements OnModuleInit { async onModuleInit(): Promise { - if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') { + if (process.env.MASTER == 'true') { if (this.bot == null) { return; } @@ -93,7 +93,7 @@ export class DiscordService implements OnModuleInit { } private async registerCommands() { - if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') { + if (process.env.MASTER == 'true') { const rest = new REST().setToken(this.token); try { console.log(`Started refreshing ${commands.length} application (/) commands.`); @@ -113,7 +113,7 @@ export class DiscordService implements OnModuleInit { } public async notifyRestarted() { - if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') { + if (process.env.MASTER == 'true') { if (this.bot == null) { return; } @@ -125,7 +125,7 @@ export class DiscordService implements OnModuleInit { } public async notifySubscribersBlockFound(height: number, block: Block, message: string) { - if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') { + if (process.env.MASTER == 'true') { if (this.bot == null) { return; } diff --git a/src/services/stratum-v1.service.ts b/src/services/stratum-v1.service.ts index 68269f2..493f919 100644 --- a/src/services/stratum-v1.service.ts +++ b/src/services/stratum-v1.service.ts @@ -30,7 +30,7 @@ export class StratumV1Service implements OnModuleInit { async onModuleInit(): Promise { - if (process.env.NODE_APP_INSTANCE == '0') { + if (process.env.MASTER == 'true') { await this.clientService.deleteAll(); } setTimeout(() => {