Skip to content

Commit 0a0a8fe

Browse files
committed
feat: refactor expired trash items job to bullmq
1 parent 460d68c commit 0a0a8fe

12 files changed

Lines changed: 783 additions & 675 deletions

.env.template

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ RDS_USERNAME=postgres
2424
RECAPTCHA_V3=captcha_secret
2525
RECAPTCHA_V3_ENDPOINT=https://www.google.com/recaptcha/api/siteverify
2626
REDIS_CONNECTION_STRING=redis://@drive-cache:6380
27+
2728
SENDGRID_API_KEY=sendgrid_api_key
2829
SENDGRID_MODE_SANDBOX=true
2930
SENDGRID_TEMPLATE_DRIVE_WELCOME_EMAIL_VERIFICATION=sendgrid_template_id
@@ -71,3 +72,6 @@ APN_BUNDLE_ID="APN_BUNDLE_ID"
7172
APN_KEY_ID=APN_KEY_ID
7273
APN_ENCRYPTION_KEY=APN_ENCRYPTION_KEY
7374
APN_SECRET=APN_JWT_SECRET
75+
76+
## Not required if EXECUTE_JOBS != true
77+
REDIS_JOBS_CONNECTION_STRING=redis://@drive-cache:6380

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@keyv/redis": "^5.1.6",
4141
"@nest-lab/throttler-storage-redis": "^1.2.0",
4242
"@nestjs/axios": "^4.0.1",
43+
"@nestjs/bullmq": "^11.0.4",
4344
"@nestjs/cache-manager": "^3.1.0",
4445
"@nestjs/cli": "^11.0.16",
4546
"@nestjs/common": "^11.1.14",
@@ -59,6 +60,7 @@
5960
"ajv": "^8.18.0",
6061
"axios": "^1.13.6",
6162
"bcryptjs": "^2.4.3",
63+
"bullmq": "^5.71.0",
6264
"cache-manager": "^6.4.3",
6365
"class-transformer": "^0.5.1",
6466
"class-validator": "^0.15.1",

src/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import { AuthGuard } from './modules/auth/auth.guard';
3333
import { CacheManagerModule } from './modules/cache-manager/cache-manager.module';
3434
import { ReferralModule } from './modules/referral/referral.module';
3535

36+
const isCronjobInstance = process.env.EXECUTE_JOBS === 'true';
37+
3638
@Module({
3739
imports: [
3840
LoggerModule.forRoot({
@@ -124,7 +126,7 @@ import { ReferralModule } from './modules/referral/referral.module';
124126
}),
125127
}),
126128
EventEmitterModule.forRoot({ wildcard: true, delimiter: '.' }),
127-
JobsModule,
129+
...(isCronjobInstance ? [JobsModule] : []),
128130
NotificationModule,
129131
NotificationsModule,
130132
FileModule,

src/config/configuration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default () => ({
3434
},
3535
cache: {
3636
redisConnectionString: process.env.REDIS_CONNECTION_STRING,
37+
redisJobsConnection: process.env.REDIS_JOBS_CONNECTION_STRING,
3738
},
3839
secrets: {
3940
cryptoSecret: process.env.CRYPTO_SECRET,

src/modules/jobs/jobs.module.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Module } from '@nestjs/common';
22
import { SequelizeModule } from '@nestjs/sequelize';
33
import { ScheduleModule } from '@nestjs/schedule';
4+
import { BullModule } from '@nestjs/bullmq';
5+
import { ConfigService } from '@nestjs/config';
46
import { DeletedItemsCleanupTask } from './tasks/deleted-items-cleanup.task';
57
import { FileModule } from '../file/file.module';
68
import { FolderModule } from '../folder/folder.module';
@@ -14,12 +16,33 @@ import { MailerModule } from '../../externals/mailer/mailer.module';
1416
import { FeatureLimitModule } from '../feature-limit/feature-limit.module';
1517
import { SecurityModule } from '../security/security.module';
1618
import { DeleteExpiredFileVersionsTask } from './tasks/delete-expired-file-versions.task';
17-
import { DeleteExpiredTrashItemsTask } from './tasks/delete-expired-trash-items.task';
18-
19+
import { TRASH_CLEANUP_QUEUE } from './tasks/trash-cleanup/trash-cleanup.scheduler';
20+
import { TrashCleanupScheduler } from './tasks/trash-cleanup/trash-cleanup.scheduler';
21+
import { TrashCleanupProcessor } from './tasks/trash-cleanup/trash-cleanup.processor';
1922
@Module({
2023
imports: [
2124
SequelizeModule.forFeature([JobExecutionModel]),
2225
ScheduleModule.forRoot(),
26+
BullModule.forRootAsync({
27+
inject: [ConfigService],
28+
useFactory: (configService: ConfigService) => {
29+
const url = new URL(
30+
configService.get<string>('cache.redisJobsConnection'),
31+
);
32+
return {
33+
connection: {
34+
host: url.hostname,
35+
port: Number(url.port) || 6379,
36+
password: url.password || undefined,
37+
username: url.username || undefined,
38+
maxRetriesPerRequest: null,
39+
enableOfflineQueue: false,
40+
tls: {},
41+
},
42+
};
43+
},
44+
}),
45+
BullModule.registerQueue({ name: TRASH_CLEANUP_QUEUE }),
2346
FileModule,
2447
FolderModule,
2548
UserModule,
@@ -34,7 +57,8 @@ import { DeleteExpiredTrashItemsTask } from './tasks/delete-expired-trash-items.
3457
RetroActiveDeleteItemsCleanupTask,
3558
InactiveUsersEmailTask,
3659
DeleteExpiredFileVersionsTask,
37-
DeleteExpiredTrashItemsTask,
60+
TrashCleanupScheduler,
61+
TrashCleanupProcessor,
3862
],
3963
})
4064
export class JobsModule {}

0 commit comments

Comments
 (0)