I'm trying to make a timerjob on a sharepoint server, that also needs built-in retry functionality. Basically, the timerjob should run each night at 2 am, but if it fails, it should retry 3 timers with 15 minutes between each retry. If all goes well, it should go back to running only at 2 am.
What I've done is to create a featurereciever that installs the JobDefinition and sets up a SPDailySchedule that makes the timerjob run at 2 am - this works fine and dandy. However, when I try to implement the retry functionality, I stumple across something I cannot explain.
The timerjob calls a method, and if this method raises an exception, I create a new SPHourlySchedule, set the BeginMinute and EndMinute to DateTime.Now.AddMinutes(5).Minute, sets the timerjobs schedule to this new schedule and update the timerjob. I created a test solution to test it out, and I can replicate it with minimal code, with the Execute method looking like this:
public override void Execute(Guid targetInstanceId) { SPUtility.SendEmail(web, false, false, "add", "Mail sent at "+DateTime.Now.ToShortTimeString(), string.Empty); SPHourlySchedule schedule = new SPHourlySchedule(); var dt = DateTime.Now.AddMinutes(5); schedule.BeginMinute = dt.Minute; schedule.EndMinute = dt.Minute; this.Schedule = schedule; this.Update(); }
This just sends an email every time the timer job is executed, and sets the next execution to be 5 minutes later.
However, once I do this, the timerjob keeps going on, and on, and on, and on, sending between 2 and 6 mails every minute. If I look at the properties for the job in Central Administration, I can see that the timer job runs at 10:00, catches the exception and sets the next execution to take place at 10.05, and then 10-30 seconds later, the timerjob executes again.
Why on earth is the timerjob being executed over and over, even outside the determined begin/end values for the schedule?
if(!kill(self)) { self.strength++; }