I'm using JobRunr 6.0.0 (OSS) to execute recurring jobs in a Spring Boot backend (2.7.4). I use the Spring Boot starter jobrunr-spring-boot-2-starter that allows me to schedule a job with the annotation @Recurring. This works very well. My problem is when I want to "unschedule" a recurring job: removing (or commenting) the @Recurring annotation does not unschedule the job.
To be more specific, here's an example of how I use JobRunr to schedule a task that should happen every 30 seconds:
@Service
public class MyService {
@Recurring(cron = "*/30 * * * * *", id = "my-id")
public void doSomething() {
LOGGER.info("did something");
}
}
There's nothing very special in the configuration. The part of the application.properties related to JobRunr looks like this:
org.jobrunr.background-job-server.enabled=true
org.jobrunr.dashboard.enabled=false
org.jobrunr.background-job-server.worker-count=2
org.jobrunr.background-job-server.poll-interval-in-seconds=10
So far, everything is working as expected. The job is indeed executed every 30 seconds. And an entry in the JobRunr table in charge of keeping tracks of the recurring jobs (jobrunr_recurring_jobs) is properly added.
My problem is when I remove the @Recurring annotation or when I completely remove the bit of code that was scheduled recurently (because the code of my backend has to evolve and this particular bit is not useful anymore, or because there was a bit of refactoring, or...) For instance, my code would look like this:
@Service
public class MyService {
// @Recurring(cron = "*/30 * * * * *", id = "my-id")
// public void doSomething() {
// LOGGER.info("did something");
// }
}
In this situation, the code that was previously scheduled has disappeared. But the entry corresponding to the old job is still present in the jobrunr_recurring_jobs table. This results in many errors in the logs because JobRunr is trying to execute a bit of code that does not exist anymore.
I could manually delete the entry in the jobrunr_recurring_jobs table or use the provided dashboard to do so (though, I don't plan to keep the dashboard in the production environment). This is quite error-prone.
Is there a way to configure JobRunr so that it would "clean automatically" the recurring jobs that are not scheduled anymore?
There are a couple of ways to delete Recurring Jobs:
To give your recurring jobs an id and delete them, see the example below:
And to delete them: