Skip to content

Commit

Permalink
Use nestjs scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
CaramelFur committed Dec 26, 2022
1 parent 9845fae commit 72b6b2a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 33 deletions.
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -43,6 +44,7 @@ const imageCorsOverride = (
useExisting: ServeStaticConfigService,
imports: [EarlyConfigModule],
}),
ScheduleModule.forRoot(),
DatabaseModule,
AuthManagerModule,
UsageManagerModule,
Expand Down
15 changes: 7 additions & 8 deletions backend/src/managers/demo/demo.module.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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() {
Expand All @@ -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);
}
}
18 changes: 5 additions & 13 deletions backend/src/managers/image/image.module.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
}
25 changes: 13 additions & 12 deletions backend/src/managers/usage/usage.module.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -11,32 +12,32 @@ 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() {
if (!(await this.usageConfigService.getMetricsEnabled())) {
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);
}
}
31 changes: 31 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 72b6b2a

Please sign in to comment.