예제 #1
0
        public bool RemoveTrigger(string triggerName, bool deleteOrphanedJob)
        {
            bool found;

            lock (_triggerLock) {
                // remove from triggers
                object tempObject;
                tempObject = _triggersDictionary[triggerName];
                _triggersDictionary.Remove(triggerName);
                found = (tempObject == null) ? false : true;
                if (found)
                {
                    TriggerWrapper tw = null;
                    // remove from triggers array
                    for (int i = 0; i < _triggers.Count; ++i)
                    {
                        tw = _triggers[i] as TriggerWrapper;
                        if (triggerName.Equals(tw.Name))
                        {
                            _triggers.RemoveAt(i);
                            break;
                        }
                    }
                    _timeTriggers.Remove(tw);

                    JobWrapper jw       = _jobsDictionary[tw.Trigger.JobName] as JobWrapper;
                    Trigger[]  triggers = GetTriggersForJob(tw.Trigger.JobName);
                    if ((triggers == null || triggers.Length == 0) && deleteOrphanedJob)
                    {
                        RemoveJob(tw.Trigger.JobName);
                    }
                }
            }
            return(found);
        }
예제 #2
0
        protected bool ApplyMisfire(TriggerWrapper tw)
        {
            DateTime misfireTime = DateTime.UtcNow;

            if (MisfireThreshold > 0)
            {
                misfireTime = misfireTime.AddMilliseconds(-1 * MisfireThreshold);
            }

            NullableDateTime tnft = tw.Trigger.GetNextFireTimeUtc();

            if (!tnft.HasValue || tnft.Value > misfireTime)
            {
                return(false);
            }

            tw.Trigger.UpdateAfterMisfire();

            if (!tw.Trigger.GetNextFireTimeUtc().HasValue)
            {
                tw.State = InternalTriggerState.Complete;
                lock (_triggerLock) {
                    _timeTriggers.Remove(tw);
                }
            }
            else if (tnft.Equals(tw.Trigger.GetNextFireTimeUtc()))
            {
                return(false);
            }
            return(true);
        }
예제 #3
0
 public Trigger RetrieveTrigger(string triggerName)
 {
     lock (_triggersDictionary) {
         TriggerWrapper tw = _triggersDictionary[triggerName] as TriggerWrapper;
         return((tw != null) ? tw.Trigger : null);
     }
 }
예제 #4
0
        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);
                    }
                }
            }
        }
예제 #5
0
 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);
         }
     }
 }
예제 #6
0
 /// <summary>
 /// Determines whether the specified <see cref="T:System.Object"></see> is equal to the current <see cref="T:System.Object"></see>.
 /// </summary>
 /// <param name="obj">The <see cref="T:System.Object"></see> to compare with the current <see cref="T:System.Object"></see>.</param>
 /// <returns>
 /// true if the specified <see cref="T:System.Object"></see> is equal to the current <see cref="T:System.Object"></see>; otherwise, false.
 /// </returns>
 public override bool Equals(object obj)
 {
     if (obj is TriggerWrapper)
     {
         TriggerWrapper tw = (TriggerWrapper)obj;
         if (tw.Name.Equals(this.Name))
         {
             return(true);
         }
     }
     return(false);
 }
예제 #7
0
        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);
            }
        }
예제 #8
0
        private ArrayList GetTriggerWrappersForJob(string jobName)
        {
            ArrayList trigList = new ArrayList();

            lock (_triggerLock) {
                for (int i = 0; i < _triggers.Count; i++)
                {
                    TriggerWrapper tw = _triggers[i] as TriggerWrapper;
                    if (tw.JobName.Equals(jobName))
                    {
                        trigList.Add(tw);
                    }
                }
            }
            return(trigList);
        }
예제 #9
0
        /// <summary>
        /// Get all of the Triggers that are associated to the given Job.
        /// <p>
        /// If there are no matches, a zero-length array should be returned.
        /// </p>
        /// </summary>
        public virtual Trigger[] GetTriggersForJob(string jobName)
        {
            ArrayList result = new ArrayList();

            lock (_triggerLock) {
                for (int i = 0; i < _triggers.Count; i++)
                {
                    TriggerWrapper tw = _triggers[i] as TriggerWrapper;
                    if (tw.JobName.Equals(jobName))
                    {
                        result.Add(tw.Trigger);
                    }
                }
            }
            return((Trigger[])result.ToArray(typeof(Trigger)));
        }
예제 #10
0
        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);
                    }
                }
            }
        }
예제 #11
0
        public Trigger AcquireNextTrigger(DateTime noLaterThan)
        {
            TriggerWrapper tw = null;

            lock (_triggerLock) {
                while (tw == null)
                {
                    if (_timeTriggers.Count > 0)
                    {
                        tw = _timeTriggers[0] as TriggerWrapper;
                    }
                    if (tw == null)
                    {
                        return(null);
                    }
                    if (!tw.Trigger.GetNextFireTimeUtc().HasValue)
                    {
                        _timeTriggers.Remove(tw);
                        tw = null;
                        continue;
                    }
                    _timeTriggers.Remove(tw);
                    if (ApplyMisfire(tw))
                    {
                        if (tw.Trigger.GetNextFireTimeUtc().HasValue)
                        {
                            _timeTriggers.Add(tw);
                        }
                        tw = null;
                        continue;
                    }
                    if (tw.Trigger.GetNextFireTimeUtc().Value > noLaterThan)
                    {
                        _timeTriggers.Add(tw);
                        return(null);
                    }
                    tw.State = InternalTriggerState.Acquired;
                    //tw.Trigger.FireInstanceId = FiredTriggerRecordId;
                    return(tw.Trigger);
                }
            }
            return(null);
        }
예제 #12
0
        public virtual int Compare(object obj1, object obj2)
        {
            TriggerWrapper trig1 = (TriggerWrapper)obj1;
            TriggerWrapper trig2 = (TriggerWrapper)obj2;

            int comp = trig1.Trigger.CompareTo(trig2.Trigger);

            if (comp != 0)
            {
                return(comp);
            }

            comp = trig2.Trigger.Priority - trig1.Trigger.Priority;
            if (comp != 0)
            {
                return(comp);
            }

            return(trig1.Trigger.Name.CompareTo(trig2.Trigger.Name));
        }
예제 #13
0
		protected bool ApplyMisfire(TriggerWrapper tw) {
			DateTime misfireTime = DateTime.UtcNow;
			if (MisfireThreshold > 0) {
				misfireTime = misfireTime.AddMilliseconds(-1 * MisfireThreshold);
			}

			NullableDateTime tnft = tw.Trigger.GetNextFireTimeUtc();
			if (!tnft.HasValue || tnft.Value > misfireTime)
				return false;

			tw.Trigger.UpdateAfterMisfire();

			if (!tw.Trigger.GetNextFireTimeUtc().HasValue) {
				tw.State = InternalTriggerState.Complete;
				lock (_triggerLock) {
					_timeTriggers.Remove(tw);
				}
			} else if (tnft.Equals(tw.Trigger.GetNextFireTimeUtc())) {
				return false;
			}
			return true;
		}
예제 #14
0
		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);
					}
				}
			}
		}