Reputation: 15
We are using shedlock for clustering on a azure webapp for containers. Now my clode looks something like:
@Scheduled(cron = "0 0 0/3 * * ?")
@SchedulerLock(name = "giveBonus", lockAtMostFor = TWO_HOURS, lockAtLeastFor = TWO_HOURS)
public void giveBonus() {
}
And my shedlock code looks like:
@Bean
public ScheduledLockConfiguration taskScheduler(LockProvider lockProvider) {
return ScheduledLockConfigurationBuilder.withLockProvider(lockProvider).withPoolSize(10)
.withDefaultLockAtMostFor(Duration.ofMinutes(10)).build();
}
@Bean
public LockProvider lockProvider(DataSource dataSource) {
return new JdbcTemplateLockProvider(dataSource);
}
Now, most of the time I see that even if we have one instance of the application, there are multiple entries in the database created with same job name and almost at the same time. Please see the snippet:
What we are not able to figure out is that if this is correct behaviour or we are doing something wrong here.
The version of shedlock used by us is: 1.1.0
Upvotes: 0
Views: 1922
Reputation: 367
I think you need database constraints to do so. You can certainly do that with dlock. Its implementation is database specific e.g. using database indexes to ensure there's one lock at a time.
Service A should be something like this.
@TryLock(name = "giveBonus", owner = "your-owner", lockFor = TWO_HOURS)
public void giveBonus() {
//...
}
See this post for more information.
Upvotes: 0