public DateTime ScheduleJob(IScheduledJob job, Trigger trigger) { if (job == null) throw new SchedulerException("Job cannot be null", SchedulerException.ErrorClientError); if (trigger == null) throw new SchedulerException("Trigger cannot be null", SchedulerException.ErrorClientError); if (trigger.JobName == null) trigger.JobName = job.Name; NullableDateTime ft = trigger.ComputeFirstFireTimeUtc(); if (!ft.HasValue) throw new SchedulerException("Based on configured schedule, the given trigger will never fire.", SchedulerException.ErrorClientError); StoreJobAndTrigger(job, trigger); NotifySchedulerThread(); return ft.Value; }
public string AddScheduledJob(int interval, int repeatCount, IScheduledJob job) { // Create trigger that fires indefinitely every <interval> milliseconds Trigger trigger = new Trigger("Trigger_" + job.Name, null, DateTime.UtcNow, null, repeatCount, interval); ScheduleJob(job, trigger); return job.Name; }
public string AddScheduledOnceJob(DateTime date, IScheduledJob job) { Trigger trigger = new Trigger("Trigger_" + job.Name, null, date); ScheduleJob(job, trigger); return job.Name; }
public TriggerFiredBundle TriggerFired(Trigger trigger) { lock (_triggerLock) { TriggerWrapper tw = _triggersDictionary[trigger.Name] as TriggerWrapper; // was the trigger deleted since being acquired? if (tw == null || tw.Trigger == null) return null; // was the trigger completed since being acquired? if (tw.State == InternalTriggerState.Complete) return null; // was the trigger paused since being acquired? if (tw.State == InternalTriggerState.Paused) return null; // was the trigger blocked since being acquired? if (tw.State == InternalTriggerState.Blocked) return null; // was the trigger paused and blocked since being acquired? if (tw.State == InternalTriggerState.PausedAndBlocked) return null; NullableDateTime prevFireTime = trigger.GetPreviousFireTimeUtc(); // in case trigger was replaced between acquiring and firering _timeTriggers.Remove(tw); trigger.Triggered(); //tw.state = TriggerWrapper.StateExecuting; tw.State = InternalTriggerState.Waiting; IScheduledJob job = RetrieveJob(trigger.JobName); TriggerFiredBundle bndle = new TriggerFiredBundle(job, trigger, false, DateTime.UtcNow, trigger.GetPreviousFireTimeUtc(), prevFireTime, trigger.GetNextFireTimeUtc()); NullableDateTime d = tw.Trigger.GetNextFireTimeUtc(); if (d.HasValue) { lock (_triggerLock) { _timeTriggers.Add(tw); } } return bndle; } }
public void TriggeredJobComplete(Trigger trigger, IScheduledJob job, SchedulerInstruction triggerInstCode) { lock (_triggerLock) { JobWrapper jw = _jobsDictionary[job.Name] as JobWrapper; TriggerWrapper tw = _triggersDictionary[trigger.Name] as TriggerWrapper; // even if it was deleted, there may be cleanup to do _blockedJobs.Remove(job.Name); // check for trigger deleted during execution... if (tw != null) { if (triggerInstCode == SchedulerInstruction.DeleteTrigger) { //log.Debug("Deleting trigger"); NullableDateTime d = trigger.GetNextFireTimeUtc(); if (!d.HasValue) { // double check for possible reschedule within job // execution, which would cancel the need to delete... d = tw.Trigger.GetNextFireTimeUtc(); if (!d.HasValue) { RemoveTrigger(trigger.Name); } else { log.Debug("Deleting cancelled - trigger still active"); } } else { RemoveTrigger(trigger.Name); } } else if (triggerInstCode == SchedulerInstruction.SetTriggerComplete) { tw.State = InternalTriggerState.Complete; _timeTriggers.Remove(tw); } else if (triggerInstCode == SchedulerInstruction.SetTriggerError) { log.Info(string.Format(CultureInfo.InvariantCulture, "Trigger {0} set to ERROR state.", trigger.Name)); tw.State = InternalTriggerState.Error; } else if (triggerInstCode == SchedulerInstruction.SetAllJobTriggersError) { log.Info(string.Format(CultureInfo.InvariantCulture, "All triggers of Job {0} set to ERROR state.", trigger.Name)); SetAllTriggersOfJobToState(trigger.Name, InternalTriggerState.Error); } else if (triggerInstCode == SchedulerInstruction.SetAllJobTriggersComplete) { SetAllTriggersOfJobToState(trigger.Name, InternalTriggerState.Complete); } } } }
public void ReleaseAcquiredTrigger(Trigger trigger) { lock (_triggerLock) { TriggerWrapper tw = _triggersDictionary[trigger.Name] as TriggerWrapper; if (tw != null && tw.State == InternalTriggerState.Acquired) { tw.State = InternalTriggerState.Waiting; _timeTriggers.Add(tw); } } }
public void StoreTrigger(Trigger trigger, bool replaceExisting) { TriggerWrapper tw = new TriggerWrapper(trigger); lock(_triggerLock) { if (_triggersDictionary.Contains(tw.Name)) { if (!replaceExisting) { throw new NotSupportedException("Object already exists " + trigger.Name); } // don't delete orphaned job, this trigger has the job anyways RemoveTrigger(trigger.Name, false); } if (RetrieveJob(trigger.JobName) == null) { throw new ApplicationException("The job (" + trigger.JobName + ") referenced by the trigger does not exist."); } // add to triggers array _triggers.Add(tw); _triggersDictionary[tw.Name] = tw; lock (_pausedTriggers) { if (_pausedTriggers.Contains(trigger.Name) || _pausedJobs.Contains(trigger.JobName)) { tw.State = InternalTriggerState.Paused; if (_blockedJobs.Contains(trigger.JobName)) { tw.State = InternalTriggerState.PausedAndBlocked; } } else if (_blockedJobs.Contains(trigger.JobName)) { tw.State = InternalTriggerState.Blocked; } else { _timeTriggers.Add(tw); } } } }
public void StoreJobAndTrigger(IScheduledJob job, Trigger trigger) { StoreJob(job, false); StoreTrigger(trigger, false); }
internal TriggerWrapper(Trigger trigger) { _trigger = trigger; }
/// <summary> /// Initializes a new instance of the <see cref="TriggerFiredBundle"/> class. /// </summary> /// <param name="job">The job.</param> /// <param name="trigger">The trigger.</param> /// <param name="jobIsRecovering">if set to <c>true</c> [job is recovering].</param> /// <param name="fireTimeUtc">The fire time.</param> /// <param name="scheduledFireTimeUtc">The scheduled fire time.</param> /// <param name="prevFireTimeUtc">The previous fire time.</param> /// <param name="nextFireTimeUtc">The next fire time.</param> public TriggerFiredBundle(IScheduledJob job, Trigger trigger, bool jobIsRecovering, NullableDateTime fireTimeUtc, NullableDateTime scheduledFireTimeUtc, NullableDateTime prevFireTimeUtc, NullableDateTime nextFireTimeUtc) { _job = job; _trigger = trigger; _jobIsRecovering = jobIsRecovering; _fireTimeUtc = DateTimeUtils.AssumeUniversalTime(fireTimeUtc); _scheduledFireTimeUtc = DateTimeUtils.AssumeUniversalTime(scheduledFireTimeUtc); _prevFireTimeUtc = DateTimeUtils.AssumeUniversalTime(prevFireTimeUtc); _nextFireTimeUtc = DateTimeUtils.AssumeUniversalTime(nextFireTimeUtc); }