From 72b6b2a3d7d9952c07be6cf9994fa9d55e5505ed Mon Sep 17 00:00:00 2001 From: Rubikscraft Date: Mon, 26 Dec 2022 12:48:23 +0100 Subject: [PATCH] Use nestjs scheduler --- backend/package.json | 1 + backend/src/app.module.ts | 2 ++ backend/src/managers/demo/demo.module.ts | 15 +++++------ backend/src/managers/image/image.module.ts | 18 ++++--------- backend/src/managers/usage/usage.module.ts | 25 ++++++++--------- yarn.lock | 31 ++++++++++++++++++++++ 6 files changed, 59 insertions(+), 33 deletions(-) diff --git a/backend/package.json b/backend/package.json index 5fb13f0d..7e11e93e 100644 --- a/backend/package.json +++ b/backend/package.json @@ -32,6 +32,7 @@ "@nestjs/jwt": "^9.0.0", "@nestjs/passport": "^9.0.0", "@nestjs/platform-fastify": "^9.2.1", + "@nestjs/schedule": "^2.1.0", "@nestjs/serve-static": "^3.0.0", "@nestjs/throttler": "^3.1.0", "@nestjs/typeorm": "^9.0.1", diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index c956b417..2db11b06 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -1,4 +1,5 @@ import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; +import { ScheduleModule } from '@nestjs/schedule'; import { ServeStaticModule } from '@nestjs/serve-static'; import cors from 'cors'; import { IncomingMessage, ServerResponse } from 'http'; @@ -43,6 +44,7 @@ const imageCorsOverride = ( useExisting: ServeStaticConfigService, imports: [EarlyConfigModule], }), + ScheduleModule.forRoot(), DatabaseModule, AuthManagerModule, UsageManagerModule, diff --git a/backend/src/managers/demo/demo.module.ts b/backend/src/managers/demo/demo.module.ts index b2e5028a..e5b16a50 100644 --- a/backend/src/managers/demo/demo.module.ts +++ b/backend/src/managers/demo/demo.module.ts @@ -1,4 +1,5 @@ -import { Logger, Module, OnModuleDestroy, OnModuleInit } from '@nestjs/common'; +import { Logger, Module, OnModuleInit } from '@nestjs/common'; +import { SchedulerRegistry } from '@nestjs/schedule'; import { ImageDBModule } from '../../collections/image-db/image-db.module'; import { RoleDbModule } from '../../collections/role-db/role-db.module'; import { EarlyConfigModule } from '../../config/early/early-config.module'; @@ -9,13 +10,13 @@ import { DemoManagerService } from './demo.service'; imports: [ImageDBModule, EarlyConfigModule, RoleDbModule], providers: [DemoManagerService], }) -export class DemoManagerModule implements OnModuleInit, OnModuleDestroy { +export class DemoManagerModule implements OnModuleInit { private readonly logger = new Logger(DemoManagerModule.name); - private interval: NodeJS.Timeout; constructor( private readonly demoManagerService: DemoManagerService, private readonly hostConfigService: HostConfigService, + private readonly schedulerRegistry: SchedulerRegistry, ) {} async onModuleInit() { @@ -27,14 +28,12 @@ export class DemoManagerModule implements OnModuleInit, OnModuleDestroy { private async setupDemoMode() { this.demoManagerService.setupRoles(); - this.interval = setInterval( + + const interval = setInterval( // Run demoManagerService.execute() every interval this.demoManagerService.execute.bind(this.demoManagerService), this.hostConfigService.getDemoInterval(), ); - } - - onModuleDestroy() { - if (this.interval) clearInterval(this.interval); + this.schedulerRegistry.addInterval('demo', interval); } } diff --git a/backend/src/managers/image/image.module.ts b/backend/src/managers/image/image.module.ts index 0afb5a65..ef4001cb 100644 --- a/backend/src/managers/image/image.module.ts +++ b/backend/src/managers/image/image.module.ts @@ -1,4 +1,5 @@ -import { Logger, Module, OnModuleDestroy, OnModuleInit } from '@nestjs/common'; +import { Logger, Module, OnModuleInit } from '@nestjs/common'; +import { Interval } from '@nestjs/schedule'; import ms from 'ms'; import { SysPreference } from 'picsur-shared/dist/dto/sys-preferences.enum'; import { HasFailed } from 'picsur-shared/dist/types'; @@ -20,9 +21,8 @@ import { ImageManagerService } from './image.service'; ], exports: [ImageManagerService], }) -export class ImageManagerModule implements OnModuleInit, OnModuleDestroy { +export class ImageManagerModule implements OnModuleInit { private readonly logger = new Logger(ImageManagerModule.name); - private interval: NodeJS.Timeout; constructor( private readonly prefManager: SysPreferenceDbService, @@ -31,14 +31,10 @@ export class ImageManagerModule implements OnModuleInit, OnModuleDestroy { ) {} async onModuleInit() { - this.interval = setInterval( - // Run demoManagerService.execute() every interval - this.imageManagerCron.bind(this), - 1000 * 60, - ); - await this.imageManagerCron(); + await this.imageManagerCron() } + @Interval(1000 * 60) private async imageManagerCron() { await this.cleanupDerivatives(); await this.cleanupExpired(); @@ -77,8 +73,4 @@ export class ImageManagerModule implements OnModuleInit, OnModuleDestroy { if (cleanedUp > 0) this.logger.log(`Cleaned up ${cleanedUp} expired images`); } - - onModuleDestroy() { - if (this.interval) clearInterval(this.interval); - } } diff --git a/backend/src/managers/usage/usage.module.ts b/backend/src/managers/usage/usage.module.ts index ba8a490a..dfe6f560 100644 --- a/backend/src/managers/usage/usage.module.ts +++ b/backend/src/managers/usage/usage.module.ts @@ -1,4 +1,5 @@ -import { Logger, Module, OnModuleDestroy, OnModuleInit } from '@nestjs/common'; +import { Logger, Module, OnModuleInit } from '@nestjs/common'; +import { SchedulerRegistry } from '@nestjs/schedule'; import { ImageDBModule } from '../../collections/image-db/image-db.module'; import { SystemStateDbModule } from '../../collections/system-state-db/system-state-db.module'; import { UserDbModule } from '../../collections/user-db/user-db.module'; @@ -11,13 +12,13 @@ import { UsageService } from './usage.service'; providers: [UsageService], exports: [UsageService], }) -export class UsageManagerModule implements OnModuleInit, OnModuleDestroy { +export class UsageManagerModule implements OnModuleInit { private readonly logger = new Logger(UsageManagerModule.name); - private interval: NodeJS.Timeout; constructor( private readonly usageService: UsageService, private readonly usageConfigService: UsageConfigService, + private readonly schedulerRegistry: SchedulerRegistry, ) {} async onModuleInit() { @@ -25,18 +26,18 @@ export class UsageManagerModule implements OnModuleInit, OnModuleDestroy { this.logger.log('Telemetry is disabled'); } - this.interval = setInterval(() => { - this.usageService.execute().catch((err) => { - this.logger.warn(err); - }); - }, await this.usageConfigService.getMetricsInterval()); + const interval = setInterval( + this.cronJob.bind(this), + await this.usageConfigService.getMetricsInterval(), + ); + this.schedulerRegistry.addInterval('usage', interval); + this.cronJob(); + } + + private cronJob() { this.usageService.execute().catch((err) => { this.logger.warn(err); }); } - - onModuleDestroy() { - if (this.interval) clearInterval(this.interval); - } } diff --git a/yarn.lock b/yarn.lock index dafcdc22..821f33f4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3062,6 +3062,20 @@ __metadata: languageName: node linkType: hard +"@nestjs/schedule@npm:^2.1.0": + version: 2.1.0 + resolution: "@nestjs/schedule@npm:2.1.0" + dependencies: + cron: 2.0.0 + uuid: 8.3.2 + peerDependencies: + "@nestjs/common": ^7.0.0 || ^8.0.0 || ^9.0.0 + "@nestjs/core": ^7.0.0 || ^8.0.0 || ^9.0.0 + reflect-metadata: ^0.1.12 + checksum: 43423eb0491c692c08dcdb6d18d34ec758fe29cda52f4674a945e06933ec5b4e23229193c1b071971842b50db57024d6f1c55fe8f4c6392754b6a597b31eb423 + languageName: node + linkType: hard + "@nestjs/schematics@npm:^9.0.0, @nestjs/schematics@npm:^9.0.3": version: 9.0.3 resolution: "@nestjs/schematics@npm:9.0.3" @@ -5224,6 +5238,15 @@ __metadata: languageName: node linkType: hard +"cron@npm:2.0.0": + version: 2.0.0 + resolution: "cron@npm:2.0.0" + dependencies: + luxon: ^1.23.x + checksum: 179ec137ada4ceb44cafe51c55491e84954308d7012d2a44539f0dadbbb1ffbbe3072c2f7aaa88595d60bd56e0d536aae2e4aaa4430c869317d6c2abd966988b + languageName: node + linkType: hard + "cross-spawn@npm:^7.0.0, cross-spawn@npm:^7.0.2, cross-spawn@npm:^7.0.3": version: 7.0.3 resolution: "cross-spawn@npm:7.0.3" @@ -7982,6 +8005,13 @@ __metadata: languageName: node linkType: hard +"luxon@npm:^1.23.x": + version: 1.28.0 + resolution: "luxon@npm:1.28.0" + checksum: 5250cb9f138b6048eeb0b3a9044a4ac994d0058f680c72a0da4b6aeaec8612460385639cba2b1052ef6d5564879e9ed144d686f26d9d97b38ab920d82e18281c + languageName: node + linkType: hard + "macos-release@npm:^2.5.0": version: 2.5.0 resolution: "macos-release@npm:2.5.0" @@ -9420,6 +9450,7 @@ __metadata: "@nestjs/jwt": ^9.0.0 "@nestjs/passport": ^9.0.0 "@nestjs/platform-fastify": ^9.2.1 + "@nestjs/schedule": ^2.1.0 "@nestjs/schematics": ^9.0.3 "@nestjs/serve-static": ^3.0.0 "@nestjs/testing": ^9.2.1