lukas-krecan / shedlock Goto Github PK
View Code? Open in Web Editor NEWDistributed lock for your scheduled tasks
License: Apache License 2.0
Distributed lock for your scheduled tasks
License: Apache License 2.0
Hi Lukas, thanks for implementing this lock! I just tried using it(version:0.18.2) in my spring boot app with MS SQL server DB. I want to understand how the lock should behave in the event if the app is redeployed or server node goes down. In such cases, row in the table still exists though the task was already complete. When the app comes back up, it tries to INSERT row one more time and it fails till deleting the row manually.
What is the expected behavior? Have I missed any thing in the implementation?
@SchedulerLock(name = "someUniqueTaskName", lockAtMostFor = 60000)
Hello there,
People who use SpringBoot and Redis have some problem with your Redis-provider. SpringBoot gives us some nice and easy configuration with RedisConnectionFactory. This CF uses the same RedisPool inside the implementation but that is private so no way to get it. Instead it gives us RedisConnection.
I created some nice implementation for your LockProvider interface. I can give it back to the community but I need some information about proper contributions here for that.
Thanks in advance.
Hi,
Do you have any plan to support reactive MongoClient?
Hope add global definition for spring-boot application. For example, if I add annotation at spring boot launch class, will guard all task execute once in nodes:
@SpringBootAppliction
@EnableScheduling
@SingleUse
Thanks for any reply
It looks like this doesn't take into consideration the next firing time. I believe schedulers like quartz knows when the next fire time should be and doesn't allow the job to be ran until that time comes. So I imagine it could be possible that a task could fire a little late and run the same task if the first one finishes quickly and releases the lock. Is this the case?
Would be good to maintain run history of jobs also some way to identify which node is running the job.
Hi:
It's something wrong with my project, a @Scheduled
task method never execute because redis dead locked here
If setNX
command execute success and something wrong with pExpire
, the key will be exist forever, the task will not execute any more.
Hi,
Do you have any plan to support for DynamoDB?
Hi, I got an exception as follows:
java.lang.NoSuchMethodError:org.springframework.core.annotation.AnnotatedElementUtils.getMergedAnnotation
Spring version: 4.1.6.RELEASE.
ShedLock version: 1.0.0, 1.1.0(in fact all version after 0.11.0 will throw this exception cause spring 4.1.6 really no such method)
LockProvider: jdbc / jedis / zookeeper
Could anyone resolve this?
Thanks.
Hi
I set up shedlock with spring on my batches and my environment is: Database SQL Server , Jboss EAP 6.4 and java8. I turned on logs of shedlock and I catch some errors below:
[2017-04-19 13:05:00,013 - DEBUG - net.javacrumbs.shedlock.core.DefaultLockingTaskExecutor] Locked scheduledAlertaConciliacoes.
[2017-04-19 13:05:00,018 - DEBUG - net.javacrumbs.shedlock.core.DefaultLockingTaskExecutor] Unlocked scheduledAlertaConciliacoes.
[2017-04-19 13:15:00,064 - DEBUG - net.javacrumbs.shedlock.provider.jdbc.JdbcStorageAccessor] Exception thrown when inserting record
com.microsoft.sqlserver.jdbc.SQLServerException: Violação da restrição PRIMARY KEY 'PK_shedlock'. Não é possível inserir a chave duplicada no objeto 'dbo.Spring_Batch_Locker'. O valor de chave duplicada é (scheduledAlertaConciliacoes).
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:232)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1672)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:460)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:405)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7535)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2438)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:208)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:183)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:348)
at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:98)
at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:98)
at net.javacrumbs.shedlock.provider.jdbc.JdbcStorageAccessor.insertRecord(JdbcStorageAccessor.java:52)
at net.javacrumbs.shedlock.support.StorageBasedLockProvider.doLock(StorageBasedLockProvider.java:70)
at net.javacrumbs.shedlock.support.StorageBasedLockProvider.lock(StorageBasedLockProvider.java:54)
at net.javacrumbs.shedlock.core.DefaultLockingTaskExecutor.executeWithLock(DefaultLockingTaskExecutor.java:38)
at net.javacrumbs.shedlock.core.DefaultLockManager.executeWithLock(DefaultLockManager.java:51)
at net.javacrumbs.shedlock.core.LockableRunnable.run(LockableRunnable.java:35)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
[2017-04-19 13:15:00,072 - DEBUG - net.javacrumbs.shedlock.core.DefaultLockingTaskExecutor] Locked scheduledAlertaConciliacoes.
[2017-04-19 13:15:00,078 - DEBUG - net.javacrumbs.shedlock.core.DefaultLockingTaskExecutor] Unlocked scheduledAlertaConciliacoes.
The shedlock-provider-jdbc tries to insert on table but PK_shedlock is PK.
I am using last version of shedock 0.9.0 both of them
Hi @lukas-krecan, thanks for the great library. I am looking for the correct implementation of a simple scheduler without Spring by relying only on ShedLock's LockProvider
. To execute the Runnable
at a scheduled rate I am using a java.util.concurrent
Executor
with fixed rate. I am not sure whether creating a new LockConfiguration at each execution will negatively impact, i.e., will another instance of the application running the same executor will be able to create a new lock since the LockConfiguration
is created within the Runnable or not?
Following is my approach:
...
public void run() {
executorService = Executors.newScheduledThreadPool(1);
Runnable command = () -> {
Instant lockAtMostUntil = Instant.now().plusSeconds(14);
LockConfiguration lockConfiguration = new LockConfiguration("my_lock", lockAtMostUntil);
Optional<SimpleLock> lock = lockProvider.lock(lockConfiguration);
if (lock.isPresent()) {
doUpdate();
lock.get().unlock();
}
};
executorService.scheduleAtFixedRate(command, 15_000, 15_000, TimeUnit.MILLISECONDS);
}
Ps.: Although that is not an issue, I had not found a user list to post such question.
Thanks for the ShedLock feature, its really great. I would like to see if we can have a job disabling feature where in the next run can be disabled for a particular job. These help during application maintenance.
Hello,
this is my sample application where I'm trying to configure ShedLock to use Spring's TaskScheduler
. But unfortunately with this setup I can't get it to work. Please can you suggest another configuration or tell me what am I doing wrong?
Thanks
Heyo. I'm looking at ShedLock for locking tasks in a Spring environment. We use a setup where each tenant has their own schema and it's managed through Spring. We use SQL Server 2017 as the backing database.
Digging through ShedLock I wasn't able to find anything that would let me specify what schema to use. It would be great if we could set the schemaName in the @scheduled annotation, or at the very least specify it in the LockProvider.
Seems like I could customly implement StorageAccessor for the second use case of specifying a schema, but it doesn't seem like the first use-case is supported.
Can you advise me, what the best approach would be?
Nice tool. I've just been setting it up and noticed my @SchedulerLock
wasn't taking effect until I moved it to the interface, but my @Scheduled
annotation is on the implementation method. I can probably live with this, but it does make the code less readable. I'm not sure how Spring manages this, but perhaps this tool could do the same?
The relevant code is in net.javacrumbs.shedlock.spring.SpringLockConfigurationExtractor.java
:
SchedulerLock annotation = AnnotationUtils.findAnnotation(method, SchedulerLock.class);
I am looking for a way to use maybe an environment variable instead of the hostname for it.
Is that possible?
Have tested with Oracle, here is Oracle SQL script in case someone needs it:
CREATE TABLE SHEDLOCK (
name VARCHAR2(64 CHAR),
lock_until TIMESTAMP,
locked_at TIMESTAMP,
locked_by VARCHAR2(255 CHAR),
PRIMARY KEY (name)
);
Thanks for you work!
When I create a ScheduledLockConfiguration
and try to set withDefaultLockAtLeastFor
, I cannot build the configuration anymore without casting. The following example does not compile:
@Bean
public ScheduledLockConfiguration taskScheduler(LockProvider lockProvider) {
return ScheduledLockConfigurationBuilder
.withLockProvider(lockProvider)
.withPoolSize(10)
.withDefaultLockAtMostFor(Duration.ofMinutes(15))
.withDefaultLockAtLeastFor(Duration.ofSeconds(5))
.build();
}
The workaround is casting the ConfigurationBuilder
after calling withDefaultLockAtLeastFor
:
@Bean
public ScheduledLockConfiguration taskScheduler(LockProvider lockProvider) {
ScheduledLockConfigurationBuilder.ConfiguredScheduledLockConfigurationBuilder builder =
(ScheduledLockConfigurationBuilder.ConfiguredScheduledLockConfigurationBuilder) ScheduledLockConfigurationBuilder
.withLockProvider(lockProvider)
.withPoolSize(10)
.withDefaultLockAtMostFor(Duration.ofMinutes(15))
.withDefaultLockAtLeastFor(Duration.ofSeconds(5));
return builder.build();
}
In my opinion, the subinterfaces should be removed from the ScheduledLockConfigurationBuilder
interface.
After some tests scaling my application I start receiving this error
2018-02-15T11:15:00.00-0200 [APP/PROC/WEB/0] OUT java.lang.NoSuchMethodError: org.springframework.data.redis.core.types.Expiration.from(Ljava/time/Duration;)Lorg/springframework/data/redis/core/types/Expiration; 2018-02-15T11:15:00.00-0200 [APP/PROC/WEB/0] OUT at net.javacrumbs.shedlock.provider.redis.spring.RedisLockProvider.getExpiration(RedisLockProvider.java:72) ~[shedlock-provider-redis-spring-0.18.0.jar!/:na] 2018-02-15T11:15:00.00-0200 [APP/PROC/WEB/0] OUT at net.javacrumbs.shedlock.provider.redis.spring.RedisLockProvider.lock(RedisLockProvider.java:60) ~[shedlock-provider-redis-spring-0.18.0.jar!/:na] 2018-02-15T11:15:00.00-0200 [APP/PROC/WEB/0] OUT at net.javacrumbs.shedlock.core.DefaultLockManager.executeWithLock(DefaultLockManager.java:51) ~[shedlock-core-0.18.0.jar!/:na] 2018-02-15T11:15:00.00-0200 [APP/PROC/WEB/0] OUT at net.javacrumbs.shedlock.core.DefaultLockingTaskExecutor.executeWithLock(DefaultLockingTaskExecutor.java:38) ~[shedlock-core-0.18.0.jar!/:na] 2018-02-15T11:15:00.00-0200 [APP/PROC/WEB/0] OUT at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]
It fails with an exception when inserting lock due to existing entry by other host (which is not reflected in its in-memory set of locks it knows about), then fails to retry with update.
This prevents acquiring locks where another host left a stale lock entry - even if expired the other host wont be able to acquire the lock.
Using shedlock I don't find a way to set it in the same way one can with the default TaskScheduler.
When using this library with Tomcat, upon shutdown I receive the following Warning:
WARNING [localhost-startStop-4] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [xyz] appears to have started a thread named [ThreadPoolTaskScheduler-7] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1088)
java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)
I believe this is related to the following stack overflow post: http://stackoverflow.com/questions/6603051/how-can-i-shutdown-spring-task-executor-scheduler-pools-before-all-other-beans-i
You are instantiating a new ThreadPoolTaskScheduler which has a shutdown() method:
Unfortunately the class you return only implements the TaskScheduler Interface which lacks the shutdown() method. Would it be possible to have this class extend ThreadPoolTaskScheduler instead of implementing TaskScheduler? That way we can get a hook to an autowired bean of this type and can call shutdown on this object as demonstrated in the stack overflow posting.
Regards,
grep-ir
Hello,
when application starts we have following error on PostgreSQL logs:
ERROR: duplicate key value violates unique constraint "shedlock_pkey"
DETAIL: Key (name)=(SomeScheduler) already exists.
STATEMENT: INSERT INTO shedlock(name, lock_until, locked_at, locked_by) VALUES($1, $2, $3, $4)
Is there a possibility to avoid or fix above error?
We are trying to use ShedLock in some integration tests. These tests need the locks cleared after each run, however when I use @DirtiesContext
and set it to clear after tests, locks seem to be sticking around.
I need a way to manually clear all locks after each context.
Hi it would be nice to implement also LockingTaskExecutor for async tasks since default one DefaultLockingTaskExecutor do not work in correct way.
Currently, all the parameters of @SchedulerLock
annotation support placeholders, with exception of name
property. Name specified in annotation is not evaluated.
I would like to specify it like this:
@SchedulerLock(name = "${lock.name}")
so that it would be evaluated to a value defined in properties/yml file.
Hi.
I've been following the project for a long time, Very good.
Are you going to do that? like this
spring-boot-klock-starter
@Lock(keys = "#user.id") // Spring spel expression
public saveUser(User user) {
// any
}
Hey, I am having a lot of warnings in my logs referencing an error creating bean with name shedlockConfiguration. This only started happening when I migrated my applications to Spring Boot 2.x from 1.5. Any ideas?
17:10:28,752 WARN [com.netflix.discovery.InstanceInfoReplicator] (DiscoveryClient-InstanceInfoReplicator-0) There was a problem with the instance info replicator: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shedlockConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:589)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:372)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1256)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1105)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$1(AbstractBeanFactory.java:353)
at org.springframework.cloud.context.scope.GenericScope$BeanLifecycleWrapper.getBean(GenericScope.java:390)
at org.springframework.cloud.context.scope.GenericScope.get(GenericScope.java:184)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:350)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:672)
at com.netflix.appinfo.ApplicationInfoManager$$EnhancerBySpringCGLIB$$a611a52b.refreshDataCenterInfoIfRequired()
at com.netflix.discovery.DiscoveryClient.refreshInstanceInfo(DiscoveryClient.java:1377)
at com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:117)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shedlockConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:379)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1350)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:580)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:372)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1256)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1105)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck(AbstractAutowireCapableBeanFactory.java:952)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:835)
at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:562)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:427)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:398)
at org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors(BeanFactoryUtils.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.resolvedCachedArgument(AutowiredAnnotationBeanPostProcessor.java:545)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.access$000(AutowiredAnnotationBeanPostProcessor.java:119)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:575)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1350)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:580)
... 27 more
Caused by: java.lang.NullPointerException
I might have found an issue through some slightly non-standard use. I'll try to confirm, but knowing the code, you might know if this is possible.
Firstly, I noticed a lock_until set for much longer (about an hour) than expected - it should only have locked for 15 seconds. I deleted that lock (via SQL delete) and then noticed the job stopped triggering - it kept saying it's already locked. It remained in that state for about 24 hours when I noticed it started up again (not sure if it was deleted while the job was running).
Apologies for the vagueness. I'll try to reproduce and debug the code to be more specific.
It would be cool for the @SchedulerLock
-annotation to be allowed for other annotations to make custom composed annotations possible:
https://sdqali.in/blog/2015/12/06/implementing-custom-annotations-for-spring-mvc/
The @Scheduled
-annotation already allows this: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html
I would like to do something like this:
@Target(METHOD)
@Retention(RUNTIME)
@Documented
@Scheduled
@SchedulerLock
public @interface ScheduledLocked {
@AliasFor(annotation = Scheduled.class, attribute = "cron")
String cron() default "";
@AliasFor(annotation = SchedulerLock.class, attribute = "lockAtMostFor")
long lockAtMostFor() default 20L;
@AliasFor(annotation = SchedulerLock.class, attribute = "name")
String name() default "";
}
// usage:
@ScheduledLocked(cron="* * * * * *", name="batchrun")
public void runBatch() {
// ...
}
Hi
i would like to use the library programatically (not with annotations) but still leverage spring scheduler.
i could not find this in the examples (except for using it without spring)
so i looked at the code and could not find how to do it, specially giving a custom config extractor.
any tip would help
Shlomi
if I have more than 1 scheduler in my service and want to configure differently for parameter loctAtMost and lockAtLeast. How to go about it?
I haven't verified this, but have noticed when running two jobs at the same time on Postgres I'm getting the following error:
Caused by: org.postgresql.util.PSQLException: ERROR: out of shared memory Hint: You might need to increase max_locks_per_transaction.
The code is shown below
@Scheduled(cron = "*/30 * * * * *") //every 30 secs
@SchedulerLock(name = "update1", lockAtLeastFor = 15 * 1000)
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void update1() {
log.debug("Running update1: " + new Date());
myDAO.update1();
log.debug("Finished Running update1: " + new Date());
}
@Scheduled(cron = "*/30 * * * * *") //every 30 secs
@SchedulerLock(name = "update2", lockAtLeastFor = 15 * 1000)
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void update2() {
I'll try to confirm tomorrow, but thought I'd raise this in case you get a chance to test. We're using version 0.11.0
.
I am able to change the schema name and table name by specifying in the JdbcTemplateLockProvider
constructor.
Is there a way to rename the columns too?
thanks.
Hi,
we need support for cassandra.
How expensive it is?
Can we implement it self?
Thanks
Hello, @lukas-krecan I'm trying to set up this library to use on a side project but the documentation in the README
file doesn't seem to be correct.
It says:
if you already have an instance of ScheduledExecutorService
@Bean
public TaskScheduler taskScheduler(ScheduledExecutorService executorService, LockProvider lockProvider) {
return ScheduledLockConfigurationBuilder
.withLockProvider(lockProvider)
.withExecutorService(executorService)
.withDefaultLockAtMostFor(Duration.ofMinutes(10))
.build();
}
However, the code above returns incompatible types.
The insertRecord method in JdbcTemplateStorageAccessor catches only the DuplicateKeyException to ascertain whether the lock already exists. With Vertica DB this poses a problem as it throws a org.springframework.dao.DataIntegrityViolationException instead.
Note that this is the parent class of DuplicateKeyException. As such the catch block is skipped entirely and the exception is propagated to spring framework rendering ShedLock unusable.
I would like to propose that the DataIntegrityViolationException be caught instead so that this can work with Vertica.
IMHO maybe the base Exception class can be caught? Not sure if you have any other constraints around this so would leave that to you.
If you like, I can submit a pull request for this as well.
Exception encountered as below:
org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [INSERT INTO SHEDLOCK(name, lock_until, locked_at, locked_by) VALUES(?, ?, ?, ?)]; [Vertica]VJDBC ERROR: Duplicate key values: 'name=batchStatusEmailTask' -- violates constraint 'SHEDLOCK.C_PRIMARY'; nested exception is java.sql.SQLIntegrityConstraintViolationException: [Vertica]VJDBC ERROR: Duplicate key values: 'name=batchStatusEmailTask' -- violates constraint 'SHEDLOCK.C_PRIMARY'
What value should be set for the poolSize below?
@Bean
public ScheduledLockConfiguration taskScheduler(LockProvider lockProvider) {
return ScheduledLockConfigurationBuilder
.withLockProvider(lockProvider)
.withPoolSize(2)
.withDefaultLockAtMostFor(Duration.ofMinutes(2))
.withDefaultLockAtLeastFor(Duration.ofMinutes(2))
.build();
}
Is there a way of configuring ShedLock with Spring integration?
@Bean
@SchedulerLock( name = "fileProcessor")
@InboundChannelAdapter( channel = "fileInputChannel", poller = @Poller( fixedDelay = "${processor.poller.fixed-delay:5000}" ) )
public MessageSource<File> fileReadingMessageSource( ProcessorProperties processorProperties ) {
...
}
Is shedlock compatible with tomcat?
I´ve been trying to run on tomcat but I got some errors below:
I used
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-spring</artifactId>
<version>0.5.0</version>
</dependency>
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-provider-jdbc-template</artifactId>
<version>0.5.0</version>
</dependency>
GRAVE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/BS-CONCILIA-PORTAL]]
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:915)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/BS-CONCILIA-PORTAL]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
... 6 more
Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.webresources.StandardRoot@7795bcd1]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4929)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5059)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
... 6 more
Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [org.apache.catalina.webresources.JarResourceSet@3eab89d9]
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:106)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:136)
at org.apache.catalina.webresources.StandardRoot.startInternal(StandardRoot.java:699)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
... 9 more
Caused by: java.lang.IllegalArgumentException: java.util.zip.ZipException: invalid LOC header (bad signature)
at org.apache.catalina.webresources.JarResourceSet.initInternal(JarResourceSet.java:139)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
... 12 more
Caused by: java.util.zip.ZipException: invalid LOC header (bad signature)
at java.util.zip.ZipFile.read(Native Method)
at java.util.zip.ZipFile.access$1400(ZipFile.java:60)
at java.util.zip.ZipFile$ZipFileInputStream.read(ZipFile.java:717)
at java.util.zip.ZipFile$ZipFileInflaterInputStream.fill(ZipFile.java:419)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
at sun.misc.IOUtils.readFully(IOUtils.java:65)
at java.util.jar.JarFile.getBytes(JarFile.java:425)
at java.util.jar.JarFile.getManifestFromReference(JarFile.java:193)
at java.util.jar.JarFile.getManifest(JarFile.java:180)
at org.apache.catalina.webresources.JarResourceSet.initInternal(JarResourceSet.java:137)
... 13 more
Hi!
We are trying to use hazelcast provider connecting to a remote cluster (not an embedded hazelcast instance), but we have an error:
2017-09-06 07:22:12.705 ERROR 8 --- [WATCHDOG_THREAD] o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task.
java.lang.UnsupportedOperationException: Client has no local member!
at com.hazelcast.client.proxy.ClientClusterProxy.getLocalMember(ClientClusterProxy.java:61)
at net.javacrumbs.shedlock.provider.hazelcast.HazelcastLockProvider.getLocalMemberUuid(HazelcastLockProvider.java:144)
at net.javacrumbs.shedlock.provider.hazelcast.HazelcastLockProvider.addNewLock(HazelcastLockProvider.java:127)
at net.javacrumbs.shedlock.provider.hazelcast.HazelcastLockProvider.tryLock(HazelcastLockProvider.java:94)
at net.javacrumbs.shedlock.provider.hazelcast.HazelcastLockProvider.lock(HazelcastLockProvider.java:79)
at net.javacrumbs.shedlock.core.DefaultLockingTaskExecutor.executeWithLock(DefaultLockingTaskExecutor.java:38)
at net.javacrumbs.shedlock.core.DefaultLockManager.executeWithLock(DefaultLockManager.java:51)
at net.javacrumbs.shedlock.core.LockableRunnable.run(LockableRunnable.java:35)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
When we try against an embedded hazelcast, it works like a charm.
Thanks!
In mongoProvider, if node1 running a task gets a lock at 9:00, lockuntil's value is 9:30, but the task is blocked until 9:40.
At 9:30, another node2 running the shcedule task can get the lock by the following code and set lockuntil is 10:00:
@Override public boolean updateRecord(LockConfiguration lockConfiguration) { Date now = now(); Bson update = combine( set(LOCK_UNTIL, Date.from(lockConfiguration.getLockAtMostUntil())), set(LOCKED_AT, now), set(LOCKED_BY, hostname) ); Document result = getCollection().findOneAndUpdate( and(eq(ID, lockConfiguration.getName()), lte(LOCK_UNTIL, now)), update ); return result != null; }
When the times comes to 9:40, node1 unlocks lock and update LOCK_UNTIL to now(9:40)
@Override public void unlock(LockConfiguration lockConfiguration) { // Set lockUtil to now or lockAtLeastUntil whichever is later getCollection().findOneAndUpdate( eq(ID, lockConfiguration.getName()), combine(set(LOCK_UNTIL, Date.from(lockConfiguration.getUnlockTime()))) ); }
public Instant getUnlockTime() { Instant now = Instant.now(); return lockAtLeastUntil.isAfter(now) ? lockAtLeastUntil : now; }
the node1 changes node2's lock info through unlock ? should add "lockedBy" check in unlock operation?
Is is possible to increase the lock_until
value while the task is still running?
I could set the lockAtMostFor
to some large value. This (mostly) ensures that the scheduled task is able to finish before the lock is released again. But I'm worried about what will happen if the node running this task is killed. Then other nodes in the cluster would have to wait until the large lockAtMostFor
value times out.
Hi. Big thx for this project. Any plans to upgrade?
I've an application with two instances deployed having two scheduled tasks (with no delay) and I'm using shedlock-provider-mongo, when I start the applications the tasks are executed by both instances initially but it works fine after first execution, is this expected behavior? Or do i need to put more configuration to block tasks to be executed by both instances when the application start.
Hello,
I have to check the shedlock-state in an managedOperation without the shedlock annotation. Is there any option to handle shedlock without the annotation?
Greetings
I tried to configured my Spring env, as documented in the "Configure the task scheduler" paragraph (the ScheduledLockConfiguration bean provider), but this way, I noticed my Tomcat was unable to stop gracefully, due to the error
"The web application [xyz] appears to have started a thread named [ThreadPoolTaskScheduler-X] but has failed to stop it. This is very likely to create a memory leak."
I worked around it by using a different configuration:
@Bean
public TaskScheduler taskScheduler(LockProvider lockProvider) throws Exception {
ScheduledLockConfiguration schedulerConfig = (ScheduledLockConfigurationBuilder
.withLockProvider(lockProvider)
.withPoolSize(10)
.withDefaultLockAtMostFor(Duration.ofMinutes(10))
.build());
SpringLockableTaskSchedulerFactoryBean configBean = (SpringLockableTaskSchedulerFactoryBean) schedulerConfig;
return configBean.getObject();
}
This way, it all looks working as expected.
Will this work in a container? If running in a clustered environment where multiple nodes have the same hostname, will this produce an issue?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.