-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multi core running app duplicate schedules saved with the same name #312
Comments
Unfortunately, node-cron doesn't have a Redis integration (see kelektiv/node-cron#175). I tried to use the Bull package as a workaround. You can define the cronejob options via the |
Shouldn't Nest.js have a single package than can handle jobs and recurring jobs? Currently, there is @nestjs/bull for one-time jobs and @nestjs/schedule for cron jobs, although bull can handle both.(https://github.com/OptimalBits/bull/blob/master/REFERENCE.md#queueadd) For example, @nestjs/bull could create an abstraction for cron jobs, so that a new bull job (configured with the cron option) is automatically scheduled each time the Nest.js app is started. It would be great to haves a concept like ActiveJob (Ruby on Rails) for Nest.js. |
I come up to using bull. Using the process as anonymous I found out that I could replicate what I need from schedule and cron , this is how my proccess looks like: import { Process, Processor } from '@nestjs/bull';
import { Logger } from '@nestjs/common';
import { Job } from 'bull';
@Processor('audio')
export class AudioProcessor {
private readonly logger = new Logger(AudioProcessor.name);
@Process('*')
async transcode(job: Job<unknown>) {
this.logger.debug(job.data);
}
} the @process('*') decorator with the * make possible call distinct tasks. The rest is just logic. |
Do you start the cron job in Nest's |
in my case I sue redis since I need to make tasks uniq and since is tis connected the task is already registered and started every time the app is launched. Here is the full simple example you can extend with your logic The module import { BullModule } from '@nestjs/bull';
import { Module } from '@nestjs/common';
import { AudioController } from './audio.controller';
import { AudioProcessor } from './audio.processor';
@Module({
imports: [
BullModule.registerQueue({
name: 'audio',
}),
],
controllers: [AudioController],
providers: [AudioProcessor],
})
export class AudioModule {} the controller, if you need it to start some taks import { InjectQueue } from '@nestjs/bull';
import { Controller, Get, Post } from '@nestjs/common';
import { Queue } from 'bull';
@Controller('audio')
export class AudioController {
constructor(@InjectQueue('audio') private readonly audioQueue: Queue) {
}
@Get('transcode')
async transcode() {
// this.audioQueue.empty().then(function() {
// console.log('done removing jobs');
// });
//
await this.audioQueue.add('teste', {
file: 'teste-name',
}, {
repeat: {
cron: '* * * * * *',
},
});
return { status: 'ok' };
}
}
the generic process bull creator import { Process, Processor } from '@nestjs/bull';
import { Logger } from '@nestjs/common';
import { Job } from 'bull';
@Processor('audio')
export class AudioProcessor {
private readonly logger = new Logger(AudioProcessor.name);
@Process('*')
async transcode(job: Job<unknown>) {
this.logger.debug(job.data);
}
} |
I need to have a clean-up cron job to delete inactive user accounts. So, it isn't bound to a specific request, but should be executed every 24 hours. |
To make some service be called every time you turn on the app, just use something like this // from main.ts
await app
.select(CronModule)
.get(CronService, { strict: true })
.someMethodHere(someParamIfYouNeed); |
I'm using the However, I would prefer that Nest.js has a solution to register cron jobs with bull automatically. |
The solution proposed in this issue (using
I would personally suggest using the
We wanted to provide integrations for both packages as they are heavily used by the framework users. |
How would I set up the cron job in the |
I'd not recommend using the
Depends on what you want to accomplish. If you want to use Bull's repeatable jobs, just add logic for registering such a job in the |
I'm submitting a...
Current behavior
Using PM2 with the option -i the app runs a multi multi thread, but when schedule is created at differents moments at a node running app under multi core, some schedule is created twice and more.
Expected behavior
Use redis, like @nestjs/websockets do, to save/create schedules
The text was updated successfully, but these errors were encountered: