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.