How to release the lock for scheduler with Shedlock in Spring boot?

Viewed 1924

I created a scheduler which runs after every 30 seconds. @Scheduled(fixedRateString = "${scheduler.time:30000}")

The Shedlock for this has been configured as @EnableSchedulerLock(defaultLockAtMostFor = "PT30S").

I have an entry in the Shedlock table as:

lock_until : 2021-07-20 14:53:26.446

locked_at : 2021-07-20 14:53:24.585

And scheduler is not running at all.

I have few questions:

  1. Shouldn't the lock have been released after 30 seconds ?

  2. Is the time for running the scheduler and the Sherlock is same as 30 seconds, which causes the issue ?

  3. Now that I am stuck, what is the way to release the lock ? Should I just delete the entry from the shedlock table ?

Thanks.

1 Answers
  1. As per my understanding lock is getting extended before it's released.

  2. No. When you are running your application locally pointing to test environment, you won't seize the lock because it's already locked by application running in test environment. And chances are less that your local instance will seize the lock, but if this happens than scheduled job won't be executed in test environment.

  3. I won't recommend deleting the shedlock entry from db. Instead you should annotate scheduled task method with @SchedulerLock and provide a lock name. So when you run application locally just to change the lock name temporarily, so that it doesn't clashes with the lock name in test environment.

    @Scheduled(fixedRateString = "${scheduler.time:30000}")
    @SchedulerLock(name = "scheduledTaskLockName", lockAtLeastFor = "PT10S", lockAtMostFor = "PT30S")
    public void scheduledTask() {
        // scheduled task execution
    }

Source code is available here on GitHub.

Related