From 949a80150c022c2e37d6f9f6ee574073b6435ef8 Mon Sep 17 00:00:00 2001 From: suvajit Date: Sun, 1 Oct 2023 11:33:58 +0530 Subject: [PATCH] Service wars setup --- .../src/service-capital.service.ts | 39 ++++++++++------- apps/service-wars/src/service-wars.service.ts | 42 ++++++++++++++++++- 2 files changed, 63 insertions(+), 18 deletions(-) diff --git a/apps/service-capital/src/service-capital.service.ts b/apps/service-capital/src/service-capital.service.ts index 3f1fbe5..9c750aa 100644 --- a/apps/service-capital/src/service-capital.service.ts +++ b/apps/service-capital/src/service-capital.service.ts @@ -16,6 +16,7 @@ import { import { RestService } from '@app/rest'; import RestHandler from '@app/rest/rest.module'; import { Inject, Injectable, Logger, NotFoundException } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; import { APICapitalRaidSeason, APIClan, @@ -28,6 +29,7 @@ import { Collection, Db } from 'mongodb'; @Injectable() export class CapitalService { constructor( + private configService: ConfigService, @Inject(Tokens.MONGODB) private db: Db, @Inject(Tokens.REDIS) private redis: RedisClient, @Inject(Tokens.REST) private restHandler: RestHandler, @@ -47,7 +49,12 @@ export class CapitalService { private cached = new Map(); private pollingInterval = 1 * 60 * 1000; // 1 minute - private async onModuleInit() { + protected onApplicationBootstrap() { + const disabled = this.configService.get('SERVICE_CAPITAL_DISABLED'); + if (!disabled) this.init(); // Start polling from the API + } + + private async init() { this.logger.debug('Loading clans...'); await this.loadClans(); @@ -55,6 +62,21 @@ export class CapitalService { await this.startPolling(); } + private async loadClans() { + const clans = await this.mongoService.getTrackedClans(); + for (const clan of clans) this.cached.set(clan.tag, clan); + } + + private async startPolling() { + try { + for (const clanTag of this.cached.keys()) { + await this.fetchCapitalRaidWeekend(clanTag); + } + } finally { + setTimeout(() => this.startPolling.bind(this), this.pollingInterval).unref(); + } + } + private async fetchCapitalRaidWeekend(clanTag: string) { const clan = await this.restService.getClan(clanTag); if (!clan) return null; @@ -269,21 +291,6 @@ export class CapitalService { ); } - private async loadClans() { - const clans = await this.mongoService.getTrackedClans(); - for (const clan of clans) this.cached.set(clan.tag, clan); - } - - private async startPolling() { - try { - for (const clanTag of this.cached.keys()) { - await this.fetchCapitalRaidWeekend(clanTag); - } - } finally { - setTimeout(() => this.startPolling.bind(this), this.pollingInterval).unref(); - } - } - private getRaidWeekendTiming() { const start = moment(); const day = start.day(); diff --git a/apps/service-wars/src/service-wars.service.ts b/apps/service-wars/src/service-wars.service.ts index 677ecc9..cef42fb 100644 --- a/apps/service-wars/src/service-wars.service.ts +++ b/apps/service-wars/src/service-wars.service.ts @@ -1,14 +1,16 @@ import { Collections, Tokens } from '@app/constants'; import { LastSeenEntity } from '@app/entities'; -import { MongodbService } from '@app/mongodb'; +import { MongodbService, TrackedClanList } from '@app/mongodb'; import { RedisClient, RedisService } from '@app/redis'; import RestHandler from '@app/rest/rest.module'; -import { Inject, Injectable } from '@nestjs/common'; +import { Inject, Injectable, Logger } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; import { Collection, Db } from 'mongodb'; @Injectable() export class WarsService { constructor( + private configService: ConfigService, @Inject(Tokens.MONGODB) private db: Db, @Inject(Tokens.REDIS) private redis: RedisClient, @Inject(Tokens.REST) private restHandler: RestHandler, @@ -22,4 +24,40 @@ export class WarsService { getHello(): string { return 'Hello World!'; } + + private logger = new Logger(WarsService.name); + private cached = new Map(); + private pollingInterval = 1 * 60 * 1000; // 1 minute + + protected onApplicationBootstrap() { + const disabled = this.configService.get('SERVICE_WARS_DISABLED'); + if (!disabled) this.init(); // Start polling from the API + } + + private async init() { + this.logger.debug('Loading clans...'); + await this.loadClans(); + + this.logger.debug('Start polling...'); + await this.startPolling(); + } + + private async loadClans() { + const clans = await this.mongoService.getTrackedClans(); + for (const clan of clans) this.cached.set(clan.tag, clan); + } + + private async startPolling() { + try { + for (const clanTag of this.cached.keys()) { + await this.fetchClanWar(clanTag); + } + } finally { + setTimeout(() => this.startPolling.bind(this), this.pollingInterval).unref(); + } + } + + private async fetchClanWar(clanTag: string) { + if (clanTag) return null; + } }