2

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.

newLearner
  • 637
  • 1
  • 12
  • 20
  • What DB are you using for locking? What dependencies do you bring for Shedlock to work? Do you have `LockProvider` bean available in the spring context? – diginoise Jul 20 '21 at 14:09
  • I am using Postgres SQL. The scheduler was running fine. I ran the project locally by pointing it to test environment and since then it's stuck. Yes bean is available. ` net.javacrumbs.shedlock shedlock-spring ${shedlock.version} net.javacrumbs.shedlock shedlock-provider-jdbc-template ${shedlock.version} ` – newLearner Jul 20 '21 at 14:12
  • 1
    I think the issue happened when I stopped the project from running locally and lock was still not released. But I am just guessing. – newLearner Jul 20 '21 at 14:15
  • 2
    The lock is released when `lock_until` is in the past. Here it seems that the lock has been held only for ~2s. – Lukas Jul 21 '21 at 11:04
  • This can happen if your local is pointing to test db and there is already a lock by the application running in test env. So your local instance won't get the lock. – kulsin Apr 01 '22 at 02:08

1 Answers1

0
  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.

kulsin
  • 398
  • 4
  • 18