Unable to write unit test for Spring #Scheduler - spring

I have implemented a scheduler with shedlock in my current spring project as follows:
#Scheduled(cron = "0 0/1 * * * *")
#SchedulerLock(name = "syncData",
lockAtMostFor = "${shedlock.myScheduler.lockAtMostFor}",
lockAtLeastFor = "${shedlock.myScheduler.lockAtLeastFor}")
public void syncSAData() {
//To assert that the lock is held
LockAssert.assertLocked();
...
}
Now I would like to write unit test for this function. Here the problem I am facing is: I am unable to mock first statement: LockAssert.assertLocked().

This should do the trick LockAssert.TestHelper.makeAllAssertsPass(true);
Just add it at the beginning of the test method.
Docs: https://github.com/lukas-krecan/ShedLock#lock-assert

Related

Spring synchronize saving to database with another instances

I have 2 instances of my Spring boot application, I am using spring JPA.
Cron scheduler run method every one hour which
first, check if record is already updated, if it's updated it should skip and don't update, but it's updated anyway on both instances.
How to implement, something like synchronized, allow to read only when something is done?
#Scheduled(cron = "0 0 * * * ?", zone = "Europe/Paris")
public void updateServers() {
applicationServerRepository.findAll().forEach(this::updateServerInfo);
}
Please explain the problem more detailed&exact.
But as I understand, you should:
Or run the cron jobs only in one instance. (Back in 90ies we did it "manually", but spring-boot-profiles offer here great opportunity: decorate "all #scheduled beans" (re-factoring!?) with #Profile("cronFooBar"), and activate/add it only on one instance.)
Or, if you also want cron jobs "load balanced", then you should find a way to synchronize/lock. (probably best in updateServerInfo or updateServers. (more details!? ..I am sure both instances will find "some object to lock on", at least the database (table, row, ..)))
As proposed by Erkan/found on internet, with "little setup" (and discarding quartz!), you can have a handy annotation for this, like:
#SchedulerLock(name = "...",
lockAtLeastForString = "...", lockAtMostForString = "...")
But I suppose, it is also possible with spring-data-jpa (only & quartz) resources, like:
Add (pessimistic) locking:
interface ApplicationServerRepository ... {
#Lock(LockModeType.PESSIMISTIC_READ)
#Query("select as from ApplicationService ...")
findAllForCron();
...
}
Catch it:
#Scheduled(cron = "0 0 * * * ?", zone = "Europe/Paris")
public void updateServers() {
try {
applicationServerRepository
.findAllForCron()
.forEach(this::updateServerInfo);
​}
catch​ (javax.persistence.PessimisticLockException plex) {
logger.info("i get some coffee");
return;
}
}

Jenkins Job DSL trigger is deprecated

I am using the Job DSL Jenkins plugin, and I have got a problem regarding the trigger. It is deprecated and when I update the code, then the deprecation warning is still shown.
Here the code before:
protected def job
void nightly(String schedule='H 0 * * *') {
job.triggers {
cron(schedule)
}
}
Then the update according to: https://github.com/jenkinsci/job-dsl-plugin/wiki/Migration
void nightly(String schedule='H 0 * * *') {
properties {
pipelineTriggers {
job.triggers {
cron(schedule)
}
}
}
}
There is still a warning: Warning: (JobBuilder.groovy, line 100) triggers is deprecated
What am I doing wrong? Is the properties keyword wrong or should it be job.properties?
thanks in advance
job basically represents project block of a job XML configuration file and its methods are converted to nested XML elements.
Your initial code
void nightly(String schedule = 'H 0 * * *') {
job.triggers {
cron(schedule)
}
}
renders this part:
<triggers>
<hudson.triggers.TimerTrigger>
<spec>H 4 * * *</spec>
</hudson.triggers.TimerTrigger>
</triggers>
Your updated code does effectively the same thing, because you are calling triggers method of job exactly like before the update. Another problem is that cron method specification for pipelineTriggers is different, so the correct code is:
void nightly(String schedule = 'H 0 * * *') {
job.properties {
pipelineTriggers {
triggers {
cron {
spec(schedule)
}
}
}
}
}
You can view available Jenkins DSL methods at https://your.jenkins.domain/plugin/job-dsl/api-viewer/index.html

What to use instead of ScheduledLockConfiguration Bean , in shedlock-spring 3.0?

I have a ScheduledLockConfiguration bean configuration.
#Bean
public ScheduledLockConfiguration taskScheduler(LockProvider lockProvider) {
return ScheduledLockConfigurationBuilder
.withLockProvider(lockProvider)
.withPoolSize(5)
.withDefaultLockAtMostFor(Duration.ofMinutes(5))
.build();
}
I just upgraded to shedlock-spring 3.0, and I don't know what to use instead of this Bean?
We can configure like below.
#Component
class TaskScheduler {
#Scheduled(cron = "0 0 10 * * ?")
#SchedulerLock(name = "TaskScheduler_scheduledTask", lockAtLeastForString = "PT5M", lockAtMostForString = "PT14M")
public void scheduledTask() {
// ...
}
}
#Scheduled will support corn format.
#SchedulerLock, the name parameter has to be unique and ClassName_methodName is typically enough to achieve that. We don't want more than one run of this method happening at the same time, and ShedLock uses the unique name to achieve that.
First, we've added lockAtLeastForString so that we can put some distance between method invocations. Using “PT5M” means that this method will hold the lock for 5 minutes, at a minimum. In other words, that means that this method can be run by ShedLock no more often than every five minutes.
Next, we added lockAtMostForString to specify how long the lock should be kept in case the executing node dies. Using “PT14M” means that it will be locked for no longer than 14 minutes.
In normal situations, ShedLock releases the lock directly after the task finishes. Now, really, we didn't have to do that because there is a default provided in #EnableSchedulerLock, but we've chosen to override that here.

spring #scheduled cron with variable

in our system we use a settings class to point to the properties file, depends on which eve it loads different property file. To access specific property, we call 'Settings.getString('property_name_here')'.
In my code, i loaded the #scheduled cron expression to a variable and try to passed to the #scheduled annotation, but it won't work,
here is my code:
in properties file:
cron.second=0
cron.min=1
cron.hour=14
in constructor i have:
this.cronExpression = new StringBuilder()
.append(settings.getString("cron.second"))
.append(" ")
.append(settings.getString("cron.min"))
.append(" ")
.append(settings.getString("cron.hour"))
.append(" ")
.append("*").append(" ").append("*").append(" ").append("*")
.toString();
which create a String of "0 1 14 * * *", it's a valid con expression
in the scheduled task i have:
#Scheduled(cron = "${this.cronExpression}")
public void scheduleTask() throws Exception {
....
}
when I ran the code it complaint:
Caused by: java.lang.IllegalStateException: Encountered invalid #Scheduled method 'scheduleTask': Cron expression must consist of 6 fields (found 1 in "${this.cronExpression}")
then I changed this.cronExpression to a list of String:
this.cronExpression = Lists.newArrayList();
this.cronExpression.add(settings.getString("cron.second"));
this.cronExpression.add(settings.getString("cron.min"));
this.cronExpression.add(settings.getString("cron.hour"));
this.cronExpression.add("*");
this.cronExpression.add("*");
this.cronExpression.add("*");
but still got the same error, so what exactly is the cron expression supposed to be?
What I did and it worked for me:
In properties file:
my.cron.expression=0 3 * * * ?
In code:
#Scheduled(cron = "${my.cron.expression}")
public void scheduleTask(){
...
}

spring Quartz scheduler with cron expression

I want to schedule a job class that checks if a boolean var changed to true or , which is initially not set to any value, using cron expression every night at sometime(say 1'o clock).The scheduler should quit the job if var is set to true or false, otherwise continue running the job at schedule for the max of 15 days & then set it to true automatically. I think IoC container pattern is suitable to do this. Please provide a brief picture of the whole code to implement this.
Spring has built-in scheduling capabilities. While the full implementation is in your court, here is an example of a scheduled method, in this case for 1AM every day:
private Boolean scheduleToggle = null;
#Scheduled(cron = "0 0 01 * * ?")
public void myScheduledJob() {
if(scheduleToggle != null) {
return;
} else {
// run the job
scheduleToggle = true;
return;
}
}
For a full explanation and configuration details, see: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/scheduling.html

Resources