GithubHelp home page GithubHelp logo

Comments (11)

calebeaires avatar calebeaires commented on April 19, 2024 2

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.

from schedule.

twister21 avatar twister21 commented on April 19, 2024

Unfortunately, node-cron doesn't have a Redis integration (see kelektiv/node-cron#175).
I suggest to depend on a more advanced package like https://github.com/actionhero/node-resque#job-schedules.

I tried to use the Bull package as a workaround. You can define the cronejob options via the repeat property and start the queue immediately after Nest's initialization is completed. However, this isn't really the intended use case.

from schedule.

twister21 avatar twister21 commented on April 19, 2024

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.
A job can either be started manually (e.g. on a request) or it can be configured to start automatically. (https://edgeguides.rubyonrails.org/active_job_basics.html)

from schedule.

twister21 avatar twister21 commented on April 19, 2024

Do you start the cron job in Nest's onApplicationBootstrap lifecycle event?

from schedule.

calebeaires avatar calebeaires commented on April 19, 2024

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);
  }
}

from schedule.

twister21 avatar twister21 commented on April 19, 2024

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.

from schedule.

calebeaires avatar calebeaires commented on April 19, 2024

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);

from schedule.

twister21 avatar twister21 commented on April 19, 2024

I'm using the onApplicationBootstrap method, which seems to do the same.
https://docs.nestjs.com/fundamentals/lifecycle-events

However, I would prefer that Nest.js has a solution to register cron jobs with bull automatically.
#312 (comment)

from schedule.

kamilmysliwiec avatar kamilmysliwiec commented on April 19, 2024

The solution proposed in this issue (using @nestjs/bull for such jobs when there are multiple apps/processes/threads running in parallel) is valid. node-schedule used by this package doesn't provide any locks-related functionality ootb (so it would have to be added on top, or another package like e.g., node-resque would have to be used).

I'm using the onApplicationBootstrap method, which seems to do the same.

I would personally suggest using the onApplicationBootstrap hook as well. Either approach should work OK though.

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.

We wanted to provide integrations for both packages as they are heavily used by the framework users.

from schedule.

sirbully avatar sirbully commented on April 19, 2024

How would I set up the cron job in the onApplicationBootstrap hook? Do I still use @nestjs/schedule?

from schedule.

kamilmysliwiec avatar kamilmysliwiec commented on April 19, 2024

I'd not recommend using the @nestjs/schedule in this case.

How would I set up the cron job in the onApplicationBootstrap hook?

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 onApplicationBootstrap hook.

from schedule.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.