예제 #1
0
 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;
 }
예제 #2
0
 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;
 }
예제 #3
0
 public string AddScheduledOnceJob(DateTime date, IScheduledJob job)
 {
     Trigger trigger = new Trigger("Trigger_" + job.Name, null, date);
     ScheduleJob(job, trigger);
     return job.Name;
 }
예제 #4
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;
            }
        }
예제 #5
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);
                    }
                }
            }
        }
예제 #6
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);
         }
     }
 }
예제 #7
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);
                    }
                }
            }
        }
예제 #8
0
 public void StoreJobAndTrigger(IScheduledJob job, Trigger trigger)
 {
     StoreJob(job, false);
     StoreTrigger(trigger, false);
 }
예제 #9
0
 internal TriggerWrapper(Trigger trigger)
 {
     _trigger = trigger;
 }
예제 #10
0
 /// <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);
 }