Esempio n. 1
0
        protected virtual void RunPendingJobs()
        {
            var now     = SchedulingTimeHelpers.Now();
            var dueJobs = new List <SchedulingItemContext>();
            SchedulingItemContext currentJob = null;

            for (int i = 0; i < SchedulingItems.Count; i++)
            {
                var job = SchedulingItems[i];
                if (job.NextExecution.Value > now)
                {
                    break;
                }
                dueJobs.Add(job);
            }

            try
            {
                for (var i = dueJobs.Count - 1; i >= 0; i--)
                {
                    currentJob = dueJobs[i];
                    currentJob.ExecuteAsync(this);
                    if (currentJob.NextExecution.HasValue)
                    {
                        IsSorted = false;
                    }
                    else
                    {
                        SchedulingItems.RemoveAt(i);
                    }
                }
            }
            catch (Exception e)
            {
                if (!SubmitJobException(currentJob.ManagedJob, e))
                {
                    throw;
                }
            }
        }
Esempio n. 2
0
        public virtual void Submit(SchedulingItem schedulingItem, Action <SchedulingItem> callback)
        {
            if (schedulingItem == null)
            {
                throw new ArgumentNullException("schedulingItem");
            }
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }
            var interval = schedulingItem.Interval;

            if (interval.HasValue && interval.Value.TotalMilliseconds < MinJobInterval)
            {
                string msg = "Interval of {0} ms is too small - a minimum interval of {1} ms is accepted.";
                msg = String.Format(msg, interval.Value.TotalMilliseconds, MinJobInterval);
                throw new InvalidOperationException(msg);
            }
            var context = new SchedulingItemContext(schedulingItem, callback);

            lock (SyncRoot)
            {
                if (NextExecution == null || context.NextExecution <= NextExecution.Value)
                {
                    SchedulingItems.Insert(0, context);
                    if (NextExecution == null || NextExecution.Value.Subtract(SchedulingTimeHelpers.Now()).TotalMilliseconds > MinJobInterval)
                    {
                        Reschedule();
                    }
                }
                else
                {
                    SchedulingItems.Add(context);
                    IsSorted = false;
                }
            }
        }