Is there a way to automatically "unschedule" recurring jobs with JobRunr and Spring Boot?

1k Views Asked by At

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?

1

There are 1 best solutions below

0
rdehuyss On

There are a couple of ways to delete Recurring Jobs:

  • there is the dashboard as you mentioned - if you can keep it accessible internally only in your PRD environment, you can delete it via there
  • give your recurring job an id - then you can delete it programmatically
  • JobRunr Pro comes with a migration api that will not only allow you to migrate these jobs (or delete them if you want) but also gives you the option to add a test case so that your CI/CD fails if you have jobs that are scheduled in PRD but not available anymore in your current code.

To give your recurring jobs an id and delete them, see the example below:

@Service
public class MyService {

    @Recurring(id = "my-id", cron = "*/30 * * * * *")
    public void doSomething() {
       LOGGER.info("did something");
    }
}

And to delete them:

jobScheduler.delete("my-id");