Nestjs Shell Command in queue Consumer

Viewed 735

I want to use shell commands like git clone in a nestjs queue Consumer. But when i execute the command with shelljs it just skips these and ends the job. Any idea why i'm getting my test logs but not the ls executed? I tried it in sync and async but both show the same behaviour.

this is my consumer

import { Process, Processor } from '@nestjs/bull';
import { Job } from 'bull';
import shell from 'shelljs';
@Processor('scan_scm')
export class SCMConsumer {
  @Process()
  async transcode(job: Job<unknown>) {
    console.log('scm-info2');
    console.log(job.data);
    console.log('test');
    // Run external tool synchronously
    const result = await shell.exec('ls -la', { async: true });
    console.log(result.exitCode);
    if (result.exitCode !== 0) {
      console.log(result.stdout);
    }
    console.log(result);

    return result;
  }
}

this is my app module

@Module({
  imports: [
    BullModule.forRoot({
      redis: {
        host: 'localhost',
        port: 6379,
      },
    }),
    BullModule.registerQueue({
      name: 'scan_scm',
    }),
    SCMConsumer,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

this is my service

@Injectable()
export class AppService {
  constructor(@InjectQueue('scan_scm') private jobQueue: Queue) {}
  getHello(): string {
    return 'Hello World!';
  }

  addToQueue(object: any) {
    this.jobQueue.add(object);
  }
}

and this is my controller

import { Controller, Get, Param } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get(':id')
  getHello(@Param('id') id): any {
    this.appService.addToQueue({ test: id });
    return { test: id };
  }
}

1 Answers

So for anyone else that wants to do this here is what solved the issue for me: You can configure a timeout for a job this seems to be pretty low per default. I just set it manually to 10 seconds which may be a bit high but for testing purposes its okay.

so your module should look like this (take a look at the defaultJobOptions):

import { BullModule } from '@nestjs/bull';
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { SCMConsumer } from './scm-info';

@Module({
  imports: [
    BullModule.forRoot({
      redis: {
        host: 'localhost',
        port: 6379,
      },
    }),
    BullModule.registerQueue({
      name: 'scan_scm',
      defaultJobOptions: { timeout: 10000 },
    }),
    SCMConsumer,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
Related