Skip to content

Commit

Permalink
refactor(be): 🚚 rename env
Browse files Browse the repository at this point in the history
  • Loading branch information
lehuygiang28 committed Jun 28, 2024
1 parent e522ed9 commit 8e1e2f1
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 22 deletions.
7 changes: 4 additions & 3 deletions apps/be/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ REDIS_CONNECT_TIMEOUT=5000
# OPTIONAL
WORKER_MODE=''
WORKER_NAME='tasktr'
BULL_TASK_CONCURRENCY=20
BULL_BACKGROUND_CONCURRENCY=10
BULL_SAVE_LOG_CONCURRENCY=10
TASK_CONCURRENCY=100
MAIL_CONCURRENCY=50
SAVE_LOG_CONCURRENCY=100
CLEAR_LOG_CONCURRENCY=100
BULLMQ_EVENTS_MAXLEN=100

MAX_LOGS_PER_TASK=10
Expand Down
7 changes: 4 additions & 3 deletions apps/be/be.example.env
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ MONGODB_URI='mongodb://user:password@localhost:27017/tasktr?authSource=admin&ret

WORKER_MODE='false'
WORKER_NAME='tasktr-be'
BULL_TASK_CONCURRENCY=1
BULL_BACKGROUND_CONCURRENCY=1
BULL_SAVE_LOG_CONCURRENCY=1
TASK_CONCURRENCY=100
MAIL_CONCURRENCY=50
SAVE_LOG_CONCURRENCY=100
CLEAR_LOG_CONCURRENCY=100
MAX_LOGS_PER_TASK=100

REDIS_HOST='localhost'
Expand Down
2 changes: 1 addition & 1 deletion apps/be/src/app/task-logs/task-log.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type TaskLogsJobName = 'saveTaskLog';

@Injectable()
@Processor(BULLMQ_TASK_LOG_QUEUE, {
concurrency: Number(process.env['BULL_SAVE_LOG_CONCURRENCY']) || 10,
concurrency: Number(process.env['SAVE_LOG_CONCURRENCY']) || 10,
})
export class TaskLogProcessor extends WorkerHost implements OnModuleInit {
constructor(
Expand Down
12 changes: 9 additions & 3 deletions apps/be/src/app/task-logs/task-logs.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { Module, Provider } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { BullModule } from '@nestjs/bullmq';

Expand All @@ -9,14 +9,20 @@ import { TaskLogsRepository } from './task-logs.repository';
import { TaskLogsService } from './task-logs.service';
import { TaskLogProcessor } from './task-log.processor';

const providers: Provider[] = [TaskLogsRepository, TaskLogsService];

if (!(process.env['SAVE_LOG_CONCURRENCY'] && Number(process.env['SAVE_LOG_CONCURRENCY']) <= 0)) {
providers.push(TaskLogProcessor);
}

@Module({
imports: [
MongooseModule.forFeature([{ name: TaskLog.name, schema: TaskLogSchema }]),
BullModule.registerQueue({
name: BULLMQ_TASK_LOG_QUEUE,
}),
],
providers: [TaskLogsRepository, TaskLogsService, TaskLogProcessor],
exports: [TaskLogsService, TaskLogProcessor],
providers: providers,
exports: [TaskLogsService],
})
export class TaskLogsModule {}
19 changes: 16 additions & 3 deletions apps/be/src/app/tasks/tasks.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Module } from '@nestjs/common';
import { Module, Provider } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { BullModule } from '@nestjs/bullmq';
import { HttpModule } from '@nestjs/axios';
import { ConfigModule } from '@nestjs/config';

import {
BULLMQ_TASK_QUEUE,
Expand All @@ -15,10 +16,22 @@ import { TasksController } from './tasks.controller';
import { TasksService } from './tasks.service';
import { TasksRepository } from './tasks.repository';
import { TaskLogsModule } from '../task-logs';
import { ClearTasksProcessor, TaskProcessor } from './processors';
import { TaskProcessor, ClearTasksProcessor } from './processors';
import tasksConfig from './config/tasks-config';

const providers: Provider[] = [TasksRepository, TasksService];

if (!(process.env['TASKS_CONCURRENCY'] && Number(process.env['TASKS_CONCURRENCY']) <= 0)) {
providers.push(TaskProcessor);
}

if (!(process.env['CLEAR_LOG_CONCURRENCY'] && Number(process.env['CLEAR_LOG_CONCURRENCY']) <= 0)) {
providers.push(ClearTasksProcessor);
}

@Module({
imports: [
ConfigModule.forFeature(tasksConfig),
HttpModule.register(axiosConfig),
MongooseModule.forFeature([{ name: Task.name, schema: TaskSchema }]),
TaskLogsModule,
Expand All @@ -33,7 +46,7 @@ import { ClearTasksProcessor, TaskProcessor } from './processors';
}),
],
controllers: [TasksController],
providers: [TasksRepository, TasksService, TaskProcessor, ClearTasksProcessor],
providers: providers,
exports: [TasksService],
})
export class TasksModule {}
15 changes: 9 additions & 6 deletions apps/be/src/app/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Injectable, OnModuleInit, UnprocessableEntityException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { InjectQueue } from '@nestjs/bullmq';
import { Queue, JobsOptions } from 'bullmq';
import { FilterQuery, QueryOptions, Types, UpdateQuery } from 'mongoose';
import { PinoLogger } from 'nestjs-pino';

import { BULLMQ_CLEAR_TASK_QUEUE, BULLMQ_TASK_QUEUE } from '~be/common/bullmq';
import { convertToObjectId, MaybeType, validateCronFrequency } from '~be/common/utils';
import { convertToObjectId, validateCronFrequency } from '~be/common/utils';
import { JwtPayloadType } from '~be/app/auth/strategies';

import { TasksRepository } from './tasks.repository';
Expand All @@ -13,9 +15,8 @@ import { CreateTaskDto, GetTasksResponseDto, TaskDto, UpdateTaskDto } from './dt
import { GetTasksDto } from './dtos/get-tasks.dto';
import { GetLogsByTaskIdDto, GetLogsByTaskIdResponseDto, TaskLogDto } from '../task-logs/dtos';
import { TaskLogsService } from '../task-logs';
import { ConfigService } from '@nestjs/config';
import { ClearTasksJobName } from './processors';
import { PinoLogger } from 'nestjs-pino';
import { AllConfig } from '../config';

@Injectable()
export class TasksService implements OnModuleInit {
Expand All @@ -27,7 +28,7 @@ export class TasksService implements OnModuleInit {
@InjectQueue(BULLMQ_CLEAR_TASK_QUEUE)
readonly clearTaskQueue: Queue<unknown, unknown, ClearTasksJobName>,
private readonly logger: PinoLogger,
private readonly configService: ConfigService,
private readonly configService: ConfigService<AllConfig>,
private readonly taskRepo: TasksRepository,
private readonly taskLogsService: TaskLogsService,
) {}
Expand Down Expand Up @@ -434,8 +435,10 @@ export class TasksService implements OnModuleInit {
* Use in cronjob - background job to scan and delete tasks that have been soft deleted
*/
async scanToHardDeleteTasks(): Promise<void> {
const deletionThreshold =
Number(this.configService.get<MaybeType<number>>('SOFT_DELETE_THRESHOLD_DAYS')) || 30;
const deletionThreshold = this.configService.getOrThrow('tasks.softDeleteThresholdDays', {
infer: true,
});

const thresholdDate = new Date();
thresholdDate.setDate(thresholdDate.getDate() - deletionThreshold);

Expand Down
7 changes: 4 additions & 3 deletions apps/be/worker.example.env
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ MONGODB_URI='mongodb://user:password@localhost:27017/tasktr?authSource=admin&ret

WORKER_MODE='true'
WORKER_NAME='tasktr-worker-1'
BULL_TASK_CONCURRENCY=500
BULL_BACKGROUND_CONCURRENCY=200
BULL_SAVE_LOG_CONCURRENCY=200
TASK_CONCURRENCY=100
MAIL_CONCURRENCY=50
SAVE_LOG_CONCURRENCY=100
CLEAR_LOG_CONCURRENCY=100
MAX_LOGS_PER_TASK=100

REDIS_HOST='localhost'
Expand Down

0 comments on commit 8e1e2f1

Please sign in to comment.