CodingCrC
CodingCrC

Reputation: 1

My queue is not working in nestjs using BullQueue

I have a project setup using nextjs and nestjs for backend. Now i have created a file upload mechanism. I am injecting my processor to the controller and then inside the consumer i call the service and start the job. But i am not seeing any logs from within the processor/consumer. Can someone tell me what is the issue here. This is my controller:

import { InjectQueue } from '@nestjs/bullmq';
import { Queue } from 'bullmq';
import { CustomLogger } from 'src/global/utils/CustomLogger';
import { QueueJobEnum } from 'src/global/enums/queue.job.enum';

@Controller('files')
@ApiTags('files')
export class FileManagementController {
  constructor(
    private readonly fileService: FileManagementService,
    private readonly fetchUserHelperService: FetchUserHelperService,
    private readonly logger: CustomLogger,
    @InjectQueue(QueueJobEnum.FILE_UPLOAD) private fileUploadQueue: Queue,
  ) {}

  @Post('file-management-upload')
  @ApiOperation({ summary: 'Upload multiple files' })
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        files: {
          type: 'array',
          items: {
            type: 'string',
            format: 'binary',
          },
        },
      },
    },
  })
  @UseInterceptors(FilesInterceptor('files'))
  async uploadFiles(
    @UploadedFiles(
      new ParseFilePipe({
        validators: [
          new MaxFileSizeValidator({ maxSize: 100 * 1024 * 1024 }),
          new FileTypeValidator({
            fileType:
              /^(application\/vnd\.ms-excel|application\/vnd\.openxmlformats-officedocument\.spreadsheetml\.sheet|text\/csv|text\/plain)$/,
          }),
        ],
      }),
    )
    files: Express.Multer.File[],
  ) {
    const user = await this.fetchUserHelperService.fetchUserDetails();

    console.log(
      `Queuing ${files.length} file(s) for upload for user: ${user.email}`,
    );

    await this.fileUploadQueue.add(QueueJobEnum.FILE_UPLOAD, { files, user });

    console.log('All files have been successfully queued for upload.');

    return new ResponseDto(
      { message: 'File upload initiated' },
      HttpStatus.CREATED,
      'Files uploaded successfully',
    );
  }

This is my processor:

import { Processor, Process } from '@nestjs/bull';
import { Job } from 'bullmq';
import { FileManagementService } from '../file.management.service';
import { QueueJobEnum } from 'src/global/enums/queue.job.enum';

@Processor(QueueJobEnum.FILE_UPLOAD)
export class FileUploadProcessor {
  constructor(private readonly fileManagementService: FileManagementService) {}

  @Process()
  async handleFileUpload(job: Job) {
    const { files, user } = job.data;

    console.log('file in consumer is: ', files);

    console.log(`Processing ${files.length} file(s) for user: ${user.email}`);

    const uploadedFiles = await Promise.all(
      files.map(async (file: Express.Multer.File) => {
        console.log(`Uploading file: ${file.originalname}`);

        const savedFile = await this.fileManagementService.uploadFileForUser(
          file,
          user,
        );

        console.log(
          `File uploaded successfully: ${savedFile.fileName}, URL: ${savedFile.url}`,
        );

        return savedFile;
      }),
    );

    console.log(
      `All files uploaded successfully for user: ${user.email}. Total: ${uploadedFiles.length}`,
    );

    return { status: 'success', uploadedFiles };
  }
}

Now can someone explain why isnt it working at all. I ahve also registered these inside my file controller:

BullModule.registerQueue({
      name: QueueJobEnum.FILE_UPLOAD,
    }),

also in my app module:

BullModule.forRoot({
      connection: {
        host: 'localhost',
        port: 6379,
      },
    });

Now my redis is runnign on port 6379 through docker.

I am trying to handle multiple file uploads by offloading these through queues in nestjs. But the job is not being starting.

Upvotes: 0

Views: 41

Answers (0)

Related Questions