Skip to content

Commit

Permalink
add scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
AzizMukhtorjonov committed Aug 17, 2024
1 parent 80fc423 commit cba7d63
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/architecture/service/main/scheduler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Injectable, OnModuleInit } from '@nestjs/common';
import { Config } from 'src/components/config/config';
import { Logger } from '../../../components/utils/logger';

@Injectable()
export class Scheduler implements OnModuleInit {
constructor(
private readonly config: Config,
private readonly logger: Logger,
) {}

async onModuleInit(): Promise<void> {
setTimeout(this.job.bind(this), this.getMsUntilNextHour());
}

private async job(): Promise<void> {
this.logger.info('Scheduler job');
}

private getMsUntilNextHour(): number {
const now = new Date();
const nextHour = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours() + 1);
return nextHour.getTime() - now.getTime();
}
}
7 changes: 5 additions & 2 deletions src/modules/service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Module } from '@nestjs/common';
import { UserService } from '../architecture/service/main/user';
import { RepositoryModule } from './repository';
import { Scheduler } from '../architecture/service/main/scheduler';
import { ConfigModule } from './config';
import { InfrastructureModule } from './infrastructure';

@Module({
imports: [RepositoryModule],
imports: [ConfigModule, InfrastructureModule, RepositoryModule],
controllers: [],
providers: [UserService],
providers: [UserService, Scheduler],
exports: [UserService],
})
export class ServiceModule {}

0 comments on commit cba7d63

Please sign in to comment.