How to ensure the SharePoint 2010 workflows end in given period

Many question has been asked about this problem.

in SharePoint Best Practices;

* For performance consideration ; you have to plan your timer jobs will always finish on time.
* You need to choose appropriate schedule interval to do it.

But this is not suitable for every condition ; Let me give some information about the issue.

You have planing to develop a custom timer job  for example this job has responsible to get some remote data from a remote System in 1 minute interval .
And what if the remote operation has taken more than 60sec. or what if that the timer job has faced with some problems and not getting finished itsjob in given period. You got the idea 🙂

In some conditions ; you may need to ensure no matter what ,the timer job has to be finished its job in given period . and If doesnt , you may want to terminate the running instance of the timer job and start a new instance of it.

The problem is here ;

If a timer has over its period and continue to running ; After the period has elapsed the new instance wont be started by  default behaviour of the SharePoint.
In SharePoint there is no way to run  two instance of a same timer job definition at same time

And even you have implemented some control logic in that timer job which responsible for the check and terminate if previous instance has running to stop it. It doesnt work because the timer job code wont be run again so it is never step through the SPTimerJob.Execute() method one more time.

And there is no way in OOB configuration to provide this functionality.

Resolution;

Resolution of this problem is easy but tricky. First you need to have 2 timer job.

EnsureTimerJobFinished

1)  Controler Timer job: (Scheduled) Has responsible to run worker timer job in defined interval and responsible to timing. (if the worker timer job has still running it can kill the previous one and start a new instance)

2)  Worker Timer Job. (Once) : Does the execute actual operation.

3)  If the Controler has detected that the previous instance of the worker timer job still running. It can terminate it. (Or you can extend your solution and make your own decision here)

4) After termination , The Controller can run another instance of the worker timer job.

Advertisements