Quartz.net scheduler and IStatefulJob - quartz-scheduler

I am wondering if I am understanding this right.
http://quartznet.sourceforge.net/apidoc/
IStatefulJob instances follow slightly
different rules from regular IJob
instances. The key difference is that
their associated JobDataMap is
re-persisted after every execution of
the job, thus preserving state for the
next execution. The other difference
is that stateful jobs are not allowed
to Execute concurrently, which means
new triggers that occur before the
completion of the IJob.Execute method
will be delayed.
Does this mean all triggers will be delayed until another trigger is done? If so how can I make it so only the same triggers will not fire until the previous trigger is done.
Say I have trigger A that fires every min but for some reason it is slow and takes a minute and half to execute. If I just use a plan IJob the next one would fire and I don't want this. I want to halt trigger A from fireing again until it is done.
However at the same time I have trigger B that fires every minute as well. It is going normal speed and finishes every minutes on time. I don't want trigger B to be held up because of trigger A.
From my understanding this is what would happen if I use IStatefulJob.

In short.. This behavior is from job's side. So regardless how many triggers you may have only single instance of given IStatefulJob (job name, job group dictates the instance id) running at a time. So there might be two instance of same job type, but no same-named jobs (name, group) if job implements IStatefulJob.
If trigger misses its fire time because of this, the misfire instructions come into play. A trigger that misses its next fire because the earlier invocation is still running decides what to do based on its misfire instruction (see API and tutorial).
With plain IJob you have no guarantees about how many jobs will be running at the same time if you have multiple triggers for it and/or misfires are happening. IJob is just contract interface for invoking the job. Quartz.NET 2.0 will split IStatefulJob combined behavior to two separate attributes: DisallowConcurrentExecution and PersistJobDataAfterExecution.
So you could combine same job type (IStatefulJobs) with two definitions (different job names) and triggers with applicable misfire instructions.

Related

What is the behavior of ADF schedule triggers when a schedule starts before the previous one ends?

I am trying to contrast and find the exact behavior of ADF schedule triggers and ADF thumbing window triggers when a trigger starts before the previous pipeline triggered by the schedule ends?
For example, let’s say we have a every 5 min schedule but the pipeline takes one hour to finish. What happens to all the every-5-min triggers that happen until the pipeline is finished?
Are the behaviors of thumbing window and schedule triggers the same in this scenario?
When using the ADF schedule trigger (shorter recurrence time than the amount of time pipeline takes to execute), the pipeline starts executing (In progress state) every time it is triggered irrespective of the previous pipeline run status.
Using the trigger with 3-minute recurrence interval produced the following output.
The tumbling window trigger reacts in the same way as a schedule trigger. The pipeline starts execution irrespective of the status of previous run. The trigger used has a 5-minute recurrence interval.
So, in this scenario both types of triggers behave in the same way. But tumbling window triggers have a self-dependency property which is not available with Schedule triggers. If the consecutive pipeline runs depend on each other, the self-dependency property can be used. Other significant differences between these triggers, including the self-dependency property are mentioned in the following Microsoft Q&A link.
https://learn.microsoft.com/en-us/answers/questions/207405/when-to-use-tumbling-window-type-trigger.html

Asynchronous SQL procedure execution set and wait for completion

Say I have a large set of calls to a procedure to run which have varying parameters but are independent so I want to make parallel/async calls. I use the service broker to fire these all off but the problem I have is I want to know neat ways of knowing how to wait for them all to complete (or error).
Is there a way to do this? I believe I could just loop with waits on the result table checking for completion on that but that isn't very "event triggered". Hoping for a nicer way to do this.
I have used the service broker with queue code and processing based off this other answer: Remus' service broker queuing example
Good day Shiv,
There are several ways (like always) that you can use in order to implement this requirement. One of these is using this logic:
(1) Create two queues: one will be the trigger to execute the main SP that you want execute in Asynchronous, and the other will be the trigger to execute whatever you want to execute after all the executions ended.
(2) When you create the message in the first queue you should also create a message in the second queue, which will only tell us which execution did not ended yet (first queue gives the information which execution started since once we START the execution we use the message and remove it from the queue).
(3) Inside the SP that you execute using the main first queue (this part executed in synchronous):
(3.1) execute the queries you need
(3.2) clear the equivalent message from the second queue (meaning that this message will removed only after the queries ended)
(3.3) check if there are messages in the second queue. If there are no messages then all the tasks ended and you can execute your final step
** Theoretically instead of using the second queue, you can store data in a table, but using second queue should probably give better performance then updating table each time an execution ended. Anyhow, you test the option of using a table as well.

Run scheduler to execute jobs at an interval from the completion of the previous job

I need to create schedulers to execute jobs(class files) at specified intervals..For Now, I'm using Quartz Scheduler which triggers the jobs at defined intervals from the time of triggering of it.
For Eg: Consider I'm giving a cron expression to run for every one hour starting at morning 9.My first run will be at 9 and my second run will be at 10 and so on.
If my job is taking 20 minutes to execute then in that case this method is not that much efficient.
What I need to do is to schedule a job for every one hour from the completion time of the previously ran job
For Eg: Consider my job to run every one hour is triggered at 9 and for the first run it took 20 minutes to run, so for the next time the job should trigger only at 10:20 instead of 10 (ie., one hour from the completion of previous ran job)
I need to know whether there are any methods in Quartz Scheduling to achieve this or any other logic I need to do.
If anyone could help me out on this,it would be very helpful for me.
You can easily achieve this by job-chaining your job executions. There are various approaches you can choose from:
(1) Implement a Quartz JobListener and in its jobWasExecuted method, that is invoked by Quartz whenever a job finishes executing, re-fire your job.
(2) Look at the Quartz JobChainingJobListener that you can use to implement simple job chaining scenarios. Please note that the functionality of this listener is very limited as it does not allow you to insert delays between job executions, there is no support for conditions that must be met before target jobs are executed etc. But you can use it as a good starting point to implement (1).
(3) Use QuartzDesk (our commercial product) or any other product that allows you to create job chains while externalizing and managing all job dependencies outside of your application. A job chain can have multiple target jobs that can be executed immediately, with a fixed delay or at arbitrary time in the future produced by a JavaScript expression. It also allows you to implement somewhat more sophisticated works flows, such as firing a target job when multiple source jobs complete their execution etc. I am attaching screenshots showing you what a simple job chain that re-executes Job1 with a 1 minute delay upon Job1's completion (with any job execution status) looks like:

Quartz scheduler: Register multiiple jobs under same trigger

I'm new to the Quartz scheduler. I had few queries and hope someone give a hand here. Thanks a lot!
First of all, let me share with you on my way to organize the jobs and triggers in single Scheduler:
One trigger group, many triggers with unique name
Many job groups, many jobs with unique name inside one group
One job group may associate with one trigger so that all jobs under this group will be fired at the same time
I think this organization is quite normal in scheduler software. However, I only found references to register same job with multiple triggers. Even though, I still thought to register many jobs with the same trigger is achievable logically.
Following is my own logic to achieve the goal:
Job A be created, Trigger A be created, call function scheduleJob(JobA, TriggerA) to register Job A with Scheduler firstly
Job B be created, get Trigger A from Scheduler based on its unique name, call function scheduleJob(JobB, TriggerA) to register Job B with Scheduler later
Therefore, refer to my own logic, I had two queries:
Is it possible to implement getting Trigger A from Scheduler based on its name ?
Is it a correct way to register multiple jobs with same trigger by using scheduleJob() function again and again ?
No, a job can have many triggers relating to it, but a trigger can only relate to one job. Though you can get something of the effect you're after if you use a job/trigger listener and schedule triggers to fire other jobs immediately when the one trigger fires.
You can set up multiple identical triggers, one for each job.

how to make multiple instances execute the same job at the same time not concurrently

I have 4 instances of Quartz Server. All of the instances point to one ADO JobStore. All I want to do is to make each Quartz instance execute the same job at the same time.
I hope it's clear enough.
This isn't supported out of the box. Whenever a trigger fires, it can only be consumed by one instance. You could fire 4 triggers, but it is not guaranteed that the job will not run twice on one instance.
If you want each instance to fire the job once, then you will have to set up 4 separate job stores.
What I do (in Quartz.NET 2.4.1) is that I have multiple identical scheduler instances, which only differ in scheduler instance name (quartz.scheduler.instanceName). They register identical jobs and triggers. Because of different scheduler instance names, the jobs and triggers are duplicated in the job store (scheduler name is part of the primary key in every table of JobStoreTX). This causes logically the same triggers to fire on all scheduler instances at the same time. They are actually separate triggers, though, so each will handle misfires etc separately.