user3611168
user3611168

Reputation: 385

Spring boot schedule task with shedlock not running by its delay

I am using spring boot with shedlock to ensure my schedule task only run in an instance at the same time.

Here is my config

@Configuration
@EnableScheduling
@EnableSchedulerLock(mode = 
EnableSchedulerLock.InterceptMode.PROXY_METHOD, 
defaultLockAtMostFor = "PT15M", 
defaultLockAtLeastFor = "PT2M")
public class SchedulerConfig implements SchedulingConfigurer {
@Bean
public LockProvider lockProvider(DataSource dataSource) {
    return new JdbcTemplateLockProvider(dataSource);
}
}

Here my task

@Scheduled(fixedDelayString = "2000")
@SchedulerLock(name = "ms.demo.scheduleTaskJob1")
public void scheduleTaskJob1() {
    logger.info("Fixed delay task called every 2 seconds after latest finished Execution Time - {}",dateTimeFormatter.format(LocalDateTime.now()));
}

Here is shedlock table NAME LOCK_UNTIL LOCKED_AT LOCKED_BY

Problem here is, when my task run the first time at 03-JUL-19 06.37.55.858685000 PM

Shedlock will add 1 row to DB like this:

ms.demo.scheduleTaskJob1    03-JUL-19 06.52.37.178573000 PM 03-JUL-19 06.37.55.858685000 PM MB0001

Because defaultLockAtMostFor is 15 minutes

When the task finished, it will update this record to unlock, and the row right now is:

ms.demo.scheduleTaskJob1    03-JUL-19 06.39.37.178573000 PM 03-JUL-19 06.37.55.858685000 PM MB0001

Because defaultLockAtLeastFor is 2 minutes.

when to the next run of the task (2019-07-03T18:37:58.434650900Z), it will updateRecord to get lock by sql command

update shedlock set LOCK_UNTIL = '', lock_at = '' , locked_by = 'MB0001' where name = 'MB0001' and LOCK_UNTIL <= '2019-07-03T18:37:58.434650900Z';

Cannot update, so the task will not running.

Here I have to wait about 2 minutes until the current time is over LOCK_UNTIL (03-JUL-19 06.39.37.178573000 PM) while my task is configured with 2s delay So my task not run every 2s as expected, instead of that it will running every 2 minutes.

Upvotes: 3

Views: 16845

Answers (3)

theNextBigThing
theNextBigThing

Reputation: 131

Make Sure to use @EnableSchedulerLock after spring 2.x.

Upvotes: 0

Skill For Kill
Skill For Kill

Reputation: 71

You can try @SchedulerLock(name = "ms.demo.scheduleTaskJob1", lockAtLeastFor = "PT2S") to use non default options or change defaultLockAtLeastFor, in @EnableSchedulerLock annotation

Upvotes: 0

Maksim Myshkin
Maksim Myshkin

Reputation: 48

It is expected behaviour. lockAtLeastFor is parameter that sets minimal interval between runs. It means that you can't run jobs more frequently then specified value. It helps to fix time synchronization problems between nodes.

In you your particular case when job is complete lock is keeping hold until specified 2 minutes interval is expired (defaultLockAtLeastFor = "PT2M"). While this time Spring is trying to run job again (as job interval is smaller then lock interval) and Shedlock see that job is locked and preventing new run. Source: https://github.com/lukas-krecan/ShedLock ("Annotate your scheduled tasks" section).

To fix this you need to change parameter defaultLockAtLeastFor to value less or equal 2 seconds as you specified this value in fixedDelayString = "2000" ("PT2S")

Upvotes: 1

Related Questions