Skip to content

Commit

Permalink
Service wars setup
Browse files Browse the repository at this point in the history
  • Loading branch information
csuvajit committed Oct 1, 2023
1 parent fb4133b commit 3368818
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 20 deletions.
39 changes: 23 additions & 16 deletions apps/service-capital/src/service-capital.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -47,14 +49,34 @@ export class CapitalService {
private cached = new Map<string, TrackedClanList>();
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();

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.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;
Expand Down Expand Up @@ -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();
Expand Down
42 changes: 40 additions & 2 deletions apps/service-clans/src/service-clans.service.ts
Original file line number Diff line number Diff line change
@@ -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 ClansService {
constructor(
private configService: ConfigService,
@Inject(Tokens.MONGODB) private db: Db,
@Inject(Tokens.REDIS) private redis: RedisClient,
@Inject(Tokens.REST) private restHandler: RestHandler,
Expand All @@ -22,4 +24,40 @@ export class ClansService {
getHello(): string {
return 'Hello World!';
}

private logger = new Logger(ClansService.name);
private cached = new Map<string, TrackedClanList>();
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.fetchClan(clanTag);
}
} finally {
setTimeout(() => this.startPolling.bind(this), this.pollingInterval).unref();
}
}

private async fetchClan(clanTag: string) {
if (clanTag) return null;
}
}
42 changes: 40 additions & 2 deletions apps/service-wars/src/service-wars.service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -22,4 +24,40 @@ export class WarsService {
getHello(): string {
return 'Hello World!';
}

private logger = new Logger(WarsService.name);
private cached = new Map<string, TrackedClanList>();
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;
}
}

0 comments on commit 3368818

Please sign in to comment.