Skip to content

Commit

Permalink
feat: add cronjob controller for viewer and warcraft
Browse files Browse the repository at this point in the history
  • Loading branch information
ToxicToast committed Nov 17, 2024
1 parent 8d7f47a commit 5d00a95
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 13 deletions.
15 changes: 15 additions & 0 deletions apps/cronjob-service/src/app/cronjob/viewer/viewer.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Controller, Logger } from '@nestjs/common';
import { EventPattern, MessagePattern } from '@nestjs/microservices';
import { CronjobTopics } from '@toxictoast/azkaban-broker-rabbitmq';
import { ViewerCron } from './viewer.cron';

@Controller('viewer')
export class ViewerController {
constructor(private readonly cronjob: ViewerCron) {}

@EventPattern(CronjobTopics.VIEWER)
async handleViewerCronjob(): Promise<void> {
Logger.log('Received Viewer Cronjob', 'ViewerController');
await this.cronjob.runQueue();
}
}
12 changes: 8 additions & 4 deletions apps/cronjob-service/src/app/cronjob/viewer/viewer.cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ export class ViewerCron {
@InjectQueue('twitch-viewer') private readonly queue: Queue,
) {}

@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT, {
name: 'Delete Inactive Twitch Viewers',
})
async checkForInactiveViewers(): Promise<void> {
async runQueue(): Promise<void> {
const viewers = await this.service.getAllViewers();
for (const viewer of viewers) {
await this.queue.add('twitch-viewer', viewer);
}
}

@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT, {
name: 'Delete Inactive Twitch Viewers',
})
async checkForInactiveViewers(): Promise<void> {
await this.runQueue();
}
}
2 changes: 2 additions & 0 deletions apps/cronjob-service/src/app/cronjob/viewer/viewer.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ViewerService } from './viewer.service';
import { ViewerProcessor } from './viewer.processor';
import { ViewerCron } from './viewer.cron';
import { ViewerQueueModule } from '../../queue/viewer-queue.module';
import { ViewerController } from './viewer.controller';

@Module({
imports: [
Expand All @@ -30,5 +31,6 @@ import { ViewerQueueModule } from '../../queue/viewer-queue.module';
ViewerQueueModule,
],
providers: [ViewerService, ViewerProcessor, ViewerCron],
controllers: [ViewerController],
})
export class ViewerModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Controller, Logger } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';
import { CronjobTopics } from '@toxictoast/azkaban-broker-rabbitmq';
import { WarcraftCron } from './warcraft.cron';

@Controller('warcraft')
export class WarcraftController {
constructor(private readonly cronjob: WarcraftCron) {}

@MessagePattern(CronjobTopics.WARCRAFT)
async handleWarcraftCronjob(): Promise<void> {
Logger.log('Received Warcraft Cronjob', 'WarcraftController');
await this.cronjob.runQueue();
}
}
24 changes: 15 additions & 9 deletions apps/cronjob-service/src/app/cronjob/warcraft/warcraft.cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@ export class WarcraftCron {
@InjectQueue('warcraft-api') private readonly queue: Queue,
) {}

async runQueue(): Promise<void> {
const characters = await this.service.getAllCharacters();
for (const character of characters) {
if (!character.isDeleted) {
await this.queue.add('warcraft-api', {
id: character.id,
region: character.region,
realm: character.realm,
name: character.name,
});
}
}
}

@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT, {
name: 'Update All Warcraft Characters',
})
async charactersCronjob(): Promise<void> {
const characters = await this.service.getAllCharacters();
for (const character of characters) {
await this.queue.add('warcraft-api', {
id: character.id,
region: character.region,
realm: character.realm,
name: character.name,
});
}
await this.runQueue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { WarcraftService } from './warcraft.service';
import { WarcraftProcessor } from './warcraft.processor';
import { WarcraftCron } from './warcraft.cron';
import { WarcraftApiQueueModule } from '../../queue/warcraftapi-queue.module';
import { WarcraftController } from './warcraft.controller';

@Module({
imports: [
Expand All @@ -39,5 +40,6 @@ import { WarcraftApiQueueModule } from '../../queue/warcraftapi-queue.module';
WarcraftApiQueueModule,
],
providers: [WarcraftService, WarcraftProcessor, WarcraftCron],
controllers: [WarcraftController],
})
export class WarcraftModule {}

0 comments on commit 5d00a95

Please sign in to comment.