-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
242 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { | ||
Column, | ||
CreateDateColumn, | ||
Entity, JoinColumn, | ||
ManyToOne, OneToMany, | ||
PrimaryGeneratedColumn, | ||
UpdateDateColumn | ||
} from "typeorm"; | ||
import {UserEntity} from "../user/user.entity"; | ||
import {UserBetEntity} from "../bet/user-bet.entity"; | ||
|
||
|
||
@Entity({name: 'cronJobs'}) | ||
export class CronJobsEntity { | ||
@PrimaryGeneratedColumn('increment') | ||
id: number; | ||
|
||
@Column("varchar") | ||
name: string; | ||
|
||
@CreateDateColumn() | ||
date: Date; | ||
|
||
@Column("integer") | ||
betId: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {forwardRef, Module} from '@nestjs/common'; | ||
import { CronJobsService } from './cron-jobs.service'; | ||
import {TypeOrmModule} from "@nestjs/typeorm"; | ||
import {CronJobsRepository} from "./cron-jobs.repository"; | ||
import {BetModule} from "../bet/bet.module"; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([CronJobsRepository]), | ||
forwardRef(()=>BetModule)], | ||
providers: [CronJobsService], | ||
exports: [CronJobsModule, CronJobsService] | ||
}) | ||
export class CronJobsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {AbstractRepository, EntityRepository, MoreThan} from "typeorm"; | ||
import {CronJobsEntity} from "./cron-jobs.entity"; | ||
|
||
|
||
@EntityRepository(CronJobsEntity) | ||
export class CronJobsRepository extends AbstractRepository<CronJobsEntity> { | ||
async saveCronJob(cronJobData: CronJobsEntity): Promise<void> { | ||
await this.repository.save(cronJobData) | ||
} | ||
|
||
async findAll(): Promise<CronJobsEntity[]> { | ||
return this.repository.find(); | ||
} | ||
|
||
async findOne(name: string): Promise<CronJobsEntity[]> { | ||
return this.repository.find({name: name}) | ||
} | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import {forwardRef, Inject, Injectable, Logger, OnModuleInit} from '@nestjs/common'; | ||
import {CronJobsRepository} from "./cron-jobs.repository"; | ||
import {CronJob} from "cron"; | ||
import {SchedulerRegistry} from "@nestjs/schedule"; | ||
import {BetRepository} from "../bet/bet.repository"; | ||
import {BetService} from "../bet/bet.service"; | ||
import {CronJobsEntity} from "./cron-jobs.entity"; | ||
|
||
@Injectable() | ||
export class CronJobsService implements OnModuleInit { | ||
constructor( | ||
@Inject(forwardRef(() => BetService)) | ||
private readonly betService: BetService, | ||
private readonly cronJobsRepository: CronJobsRepository, | ||
private schedulerRegistry: SchedulerRegistry | ||
) {} | ||
|
||
private readonly logger: Logger = new Logger(BetRepository.name); | ||
|
||
async onModuleInit(): Promise<void> { | ||
const savedCronJobs: CronJobsEntity[] = await this.findAll(); | ||
|
||
savedCronJobs.forEach((cronJob => { | ||
try { | ||
this.schedulerRegistry.getCronJob(cronJob.name) | ||
} | ||
catch (e) { | ||
if (cronJob.date < new Date(Date.now())) { | ||
// достали из бд CronJob - дата старая - отправляю в betService проверить (на случай если сервер долго спал) | ||
this.betService.updateColumnIsActive(cronJob.betId); | ||
} else { | ||
//достали из бд CronJob - дата новая - отправляю на добавление | ||
this.addCronJob(cronJob.name, cronJob.date, cronJob.betId) | ||
} | ||
} | ||
})) | ||
} | ||
|
||
async saveCronJob(name: string, expireBet: Date, savedBetId: number): Promise<void> { | ||
const cronJobData: CronJobsEntity = new CronJobsEntity; | ||
Object.assign(cronJobData,{ | ||
name: name, | ||
date: (new Date(expireBet)), | ||
betId: savedBetId | ||
}) | ||
|
||
await this.cronJobsRepository.saveCronJob(cronJobData) | ||
} | ||
|
||
async findAll(): Promise<CronJobsEntity[]> { | ||
return this.cronJobsRepository.findAll() | ||
} | ||
|
||
async addCronJob(name: string, expireBet: Date, savedBetId: number): Promise<void> { | ||
// создаём CronJob | ||
const job: CronJob = new CronJob(new Date(expireBet), () => { | ||
this.betService.updateColumnIsActive(savedBetId); | ||
this.logger.warn(`time for job ${name} to run`); | ||
}); | ||
|
||
this.schedulerRegistry.addCronJob(name, job); | ||
|
||
const foundCronJob = await this.cronJobsRepository.findOne(name); | ||
if (foundCronJob.length === 0) { | ||
// сохраняем новый CronJob в бд | ||
await this.saveCronJob(name, expireBet, savedBetId) | ||
} | ||
|
||
job.start(); | ||
|
||
this.logger.warn( | ||
`job ${name} added for update column at ${new Date(expireBet)}!`, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {MigrationInterface, QueryRunner} from "typeorm"; | ||
|
||
export class AddCronJobs1654696084741 implements MigrationInterface { | ||
name = 'AddCronJobs1654696084741' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`CREATE TABLE "cronJobs" ("id" SERIAL NOT NULL, "name" character varying(300) NOT NULL, "date" TIMESTAMP NOT NULL DEFAULT now(), "betId" character varying NOT NULL, CONSTRAINT "PK_8d6395d577c72fdb5fc97a9d956" PRIMARY KEY ("id"))`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP TABLE "cronJobs"`); | ||
} | ||
|
||
} |