Skip to content

Commit

Permalink
feat(server): delete expiration data regularly every day
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyao27 committed Aug 29, 2022
1 parent f820135 commit ffc77e6
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
"@nestjs/core": "^9.0.11",
"@nestjs/mapped-types": "^1.1.0",
"@nestjs/platform-fastify": "^9.0.11",
"@nestjs/schedule": "^2.1.0",
"@nestjs/schematics": "^9.0.1",
"@types/bull": "^3.15.9",
"@types/cron": "^2.0.0",
"@types/express": "^4.17.13",
"@types/markdown-it": "^12.2.3",
"@types/nodemailer": "^6.4.5",
Expand Down
9 changes: 8 additions & 1 deletion packages/server/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Module } from '@nestjs/common'
import { ApiModule } from '~/api/api.module'
import { TasksModule } from '~/tasks/tasks.module'
import { SharedModule } from '~/shared.module'

@Module({ imports: [ApiModule, SharedModule] })
@Module({
imports: [
ApiModule,
TasksModule,
SharedModule,
],
})
export class AppModule {}
7 changes: 6 additions & 1 deletion packages/server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ async function bootstrap() {
console.warn(`Application is running on: ${await app.getUrl()}`)
}

Cluster.register(4, bootstrap)
if (process.env.NODE_ENV === 'production') {
Cluster.register(4, bootstrap)
}
else {
bootstrap()
}
3 changes: 2 additions & 1 deletion packages/server/src/shared.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Global, Module } from '@nestjs/common'
import { ScheduleModule } from '@nestjs/schedule'
import { BullModuleConfig } from '~/common'

@Global()
@Module({ imports: [BullModuleConfig] })
@Module({ imports: [BullModuleConfig, ScheduleModule.forRoot()] })
export class SharedModule {}
6 changes: 6 additions & 0 deletions packages/server/src/tasks/tasks.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Module } from '@nestjs/common'
import { TasksService } from './tasks.service'
import { PrismaService } from '~/common'

@Module({ providers: [TasksService, PrismaService] })
export class TasksModule {}
21 changes: 21 additions & 0 deletions packages/server/src/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable } from '@nestjs/common'
import { Cron, CronExpression } from '@nestjs/schedule'
import dayjs from 'dayjs'
import { PrismaService } from '~/common'

const interval = 30

@Injectable()
export class TasksService {
constructor(private readonly prisma: PrismaService) {}

@Cron(CronExpression.EVERY_DAY_AT_3AM)
async handleDeleteExpiredData() {
const options = { where: { createdAt: { lt: dayjs().subtract(interval, 'days').toDate() } } }
await this.prisma.metric.deleteMany(options)
await this.prisma.feedback.deleteMany(options)
await this.prisma.pageView.deleteMany(options)
await this.prisma.userView.deleteMany(options)
await this.prisma.issue.deleteMany(options)
}
}
39 changes: 39 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions prisma/migrations/20220829065319_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- DropForeignKey
ALTER TABLE "EventUsersOnIssues" DROP CONSTRAINT "EventUsersOnIssues_eventUserId_fkey";

-- DropForeignKey
ALTER TABLE "EventUsersOnIssues" DROP CONSTRAINT "EventUsersOnIssues_issueId_fkey";

-- AddForeignKey
ALTER TABLE "EventUsersOnIssues" ADD CONSTRAINT "EventUsersOnIssues_eventUserId_fkey" FOREIGN KEY ("eventUserId") REFERENCES "EventUser"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "EventUsersOnIssues" ADD CONSTRAINT "EventUsersOnIssues_issueId_fkey" FOREIGN KEY ("issueId") REFERENCES "Issue"("id") ON DELETE CASCADE ON UPDATE CASCADE;
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ model EventUsersOnIssues {
issueId String
eventUserId String
assignedAt DateTime @default(now())
eventUser EventUser @relation(fields: [eventUserId], references: [id])
issue Issue @relation(fields: [issueId], references: [id])
eventUser EventUser @relation(fields: [eventUserId], references: [id], onDelete: Cascade)
issue Issue @relation(fields: [issueId], references: [id], onDelete: Cascade)
@@id([issueId, eventUserId])
}
Expand Down

0 comments on commit ffc77e6

Please sign in to comment.