Skip to content
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

Fix cron disabled flag #1281

Merged
merged 3 commits into from May 17, 2023
Merged

Conversation

d-lukutin
Copy link
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • [ X] Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Doesn't create a job.

Issue Number: N/A

What is the new behavior?

Creates a disabled job that can be enabled later.

Does this PR introduce a breaking change?

  • [ X] Yes
  • No

Other information

...

@kamilmysliwiec
Copy link
Member

What would be the use case for enabling the explicitly disabled CRON job?

@d-lukutin
Copy link
Contributor Author

d-lukutin commented May 17, 2023

What would be the use case for enabling the explicitly disabled CRON job?

export class Job1Service {
    @Cron('* * * * * *', {
        name: Job1Service.name,
        disabled: true
    })
    async job() {
        ...
    }
}

...

export class JobXService {
    @Cron('* * * * * *', {
        name: JobXService.name,
        disabled: true
    })
    async job() {
        ...
    }
}

export class JobsEnabledService {
     constructor(
         private readonly configurationService: ConfigurationService,
         private readonly schedulerRegistry: SchedulerRegistry
     ) {}

    @Cron('*/10 * * * * *', {
        name: JobsEnabledService.name
    })
    async job() {
        if(awaitn this.configurationService.enabled()) {
            this.schedulerRegistry.getCronJobs()
                .forEach((job, jobName) => {
                    if (jobName !== EnableWorkJobService.name && !job.running) job.start();
                });
        }
        else {
            this.schedulerRegistry.getCronJobs()
                .forEach((job, jobName) => {
                    if (jobName !== EnableWorkJobService.name && job.running) job.stop();
                });
        }
    }
}

@kamilmysliwiec
Copy link
Member

LGTM

@kamilmysliwiec kamilmysliwiec merged commit 726cc8c into nestjs:master May 17, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants