Ejemplo n.º 1
0
 private void runJob(ScheduledJob job)
 {
     if (_started && !job.PreventReschedule)
     {
         job.RunCount++;
         var task = Task.Run(job.JobTask);
         _tasks.Add(task);
         task.ContinueWith(t => jobEnd(task, job));
     }
 }
Ejemplo n.º 2
0
        public IScheduledJob ScheduleJob(ISchedule schedule, IJob job, DateTime firstRun)
        {
            var scheduledJob = new ScheduledJob()
            {
                Job      = job,
                Schedule = schedule
            };

            scheduledJob.JobTask = new Func <Task>(() => job.Start(scheduledJob.RunTime));

            queueJob(firstRun, scheduledJob);
            return(scheduledJob);
        }
Ejemplo n.º 3
0
        public IScheduledJob ScheduleParameterizedJob <T>(ISchedule schedule, IParameterizedJob <T> job, T parameter, DateTime firstRun)
        {
            var scheduledJob = new ScheduledJob()
            {
                Job      = job,
                Schedule = schedule,
            };

            scheduledJob.JobTask = new Func <Task>(() => job.Start(parameter, scheduledJob.RunTime));

            queueJob(firstRun, scheduledJob);

            return(scheduledJob);
        }
Ejemplo n.º 4
0
        public IScheduledJob ScheduleJob(ISchedule schedule, IJob job, bool runImmediately)
        {
            var scheduledJob = new ScheduledJob()
            {
                Job      = job,
                Schedule = schedule
            };

            scheduledJob.JobTask = new Func <Task>(() => job.Start(scheduledJob.RunTime));
            DateTime firstRun = runImmediately ? DateTime.UtcNow : schedule.NextScheduledTime(scheduledJob);

            queueJob(firstRun, scheduledJob);
            return(scheduledJob);
        }
Ejemplo n.º 5
0
        private void jobEnd(Task jobTask, ScheduledJob job)
        {
            if (jobTask.Exception == null)
            {
                Task.Run(() => _onSuccess?.Invoke(new ScheduledJobEventArgs(job)));
            }
            else
            {
                Task.Run(() => _onJobError?.Invoke(new ScheduledJobEventArgs(job),
                                                   jobTask.Exception is AggregateException? jobTask.Exception.InnerException : jobTask.Exception));
            }
            _tasks.Remove(jobTask);

            setNextExecution(job);
        }
Ejemplo n.º 6
0
        private void setNextExecution(ScheduledJob job)
        {
            if (job.PreventReschedule)
            {
                return;
            }
            DateTime next, now = DateTime.UtcNow;

            try
            {
                next = job.Schedule.NextScheduledTime(job);
            }
            catch (Exception ex)
            {
                _onScheduleError?.Invoke(new ScheduledJobEventArgs(job), ex);
                return;
            }
            if (next == Chroniton.Constants.Never)
            {
                return;
            }
            if (next < now)
            {
                switch (job.Job.ScheduleMissedBehavior)
                {
                case ScheduleMissedBehavior.RunAgain:
                    next = now;
                    break;

                case ScheduleMissedBehavior.SkipExecution:
                    next = job.Schedule.NextScheduledTime(job);
                    if (next < now)
                    {
                        _onScheduleError?.Invoke(new ScheduledJobEventArgs(job),
                                                 new Exception("The schedule twice returned a time before the end of the previous execution. "));
                        return;
                    }
                    break;

                case ScheduleMissedBehavior.ThrowException:
                    _onScheduleError?.Invoke(new ScheduledJobEventArgs(job),
                                             new Exception("The schedule returned a time before the end of the previous execution"));
                    return;
                }
            }
            queueJob(next, job);
        }
Ejemplo n.º 7
0
 private void queueJob(DateTime nextRun, ScheduledJob scheduledJob)
 {
     scheduledJob.RunTime = nextRun;
     _scheduledQueue.Add(scheduledJob);
     _onScheduled?.Invoke(new ScheduledJobEventArgs(scheduledJob));
 }