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

Make it possible to disable Scheduled job via cron expression [SPR-16858] #21397

Closed
spring-issuemaster opened this issue May 22, 2018 · 13 comments
Closed
Assignees
Milestone

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented May 22, 2018

Nikolay Bogdanov opened SPR-16858 and commented

To disable a spring job which uses cron expression you need at least 2 properties:

    ...
    @Value("${jobs.name.enable}")
    private boolean jobEnable;

    @Scheduled(cron = "0 0 0 1 * ?")
    public void execute() \{
        if(jobEnable){
            //JOB HERE
        }
    }
    ...
}

But there is no way to configure it to don't start any execution at all, but its relatively valuable feature (you can google it, but here is an example https://stackoverflow.com/questions/13835221/quartz-cron-expression-that-will-never-execute)

So the idea of this issue is to enable support for some "never" value for cron expression which will prevent this job from execution forever, so you can use only 1 parameter to configure your job. Example:

 
@Scheduled(cron = "never")

The exact value to disable a job is negotiable.


Affects: 5.0.6

Referenced from: commits 3a5def0

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented May 22, 2018

Nikolay Bogdanov commented

I can implement it by myself and create a PR, I just need some proof that if it's valuable for the community.

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented May 23, 2018

Stéphane Nicoll commented

I've seen this request in the community and what I'd personally would like is a callback approach where a component can programmatically take the decision whether a given scheduled job should operate or not. This would allow to implement the use case where something added @EnableScheduling and a component is affected by side effect (they have scheduled operations but they didn't want them to run for a particular deployment). 

Restricting based on the target class or its package can be one option and is more powerful than changing the cron value (though of course, one implementation of this callback can react to such value).

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented May 25, 2018

Nikolay Bogdanov commented

@EnableScheduling is reserved for enabling scheduling support in common, but I've got the idea.

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jun 5, 2018

Nikolay Bogdanov commented

PR #1851

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jun 8, 2018

Nikolay Bogdanov commented

@snicoll Any new thoughts about the feature? Any suggestions?

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jun 8, 2018

Stéphane Nicoll commented

I haven't spent a lot of time on it but it's flagged for 5.1 so we'll manage it in due course. I'd like to chat with Juergen about a few options first.

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jul 16, 2018

Nikolay Bogdanov commented

any progress here?

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jul 16, 2018

Stéphane Nicoll commented

Nikolay Bogdanov the issue will be updated once we work on it.

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jul 23, 2018

Nikolay Bogdanov commented

Stéphane Nicoll but it's an open source! I can do it if you don't have time. Just lets agreed on implementation and I can create a branch with changes. 

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jul 23, 2018

Stéphane Nicoll commented

I am not following. This issue tracker is public and I've just mentioned it will be updated once something happens here.

Just lets agreed on implementation and I can create a branch with changes.

I think you're vastly simplify things here. You've tried with one solution that got reviewed and rejected. I've mentioned that I'd like to consider a few options and I hadn't the the time to look at it yet.

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Aug 8, 2018

Stéphane Nicoll commented

Thanks for your patience. Juergen and I finally managed to find the time to have a proper discussion on this one. The outcome of this is that we have several options

  1. Disable all job executions using a flag (to prevent someone adding @EnableScheduling somewhere to enable job scheduling that's supposed to happen conditionally).
  2. Offer a special expression that can be specified as cron value to flag this job shouldn't run
  3. Offer a programmatic API to let uses determine if a particular job should run (can be seen as a generalization of the previous approach).

Based on our discussion 1. can be implemented by a TaskScheduler that doesn't fire any task. As for 2/3, it is important to make sure the Spring Boot scheduledtasks actuator endpoint.

Right now we're leaning to 2 only, discarding the job from the list entirely if it has been disabled that way.

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Aug 8, 2018

Nikolay Bogdanov commented

Nice, so paragraph 2 is very similar to the original description of the ticket, right? 
Juergen Hoeller do you need some help here to resolve it? 

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Aug 9, 2018

Juergen Hoeller commented

I've implemented this with a simple skip check in processScheduled if the cron value is "-" (the new Scheduled.CRON_DISABLED constant). This seems quite aligned with cron's symbolic nature. Any issues with that choice from your side?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.