Exemplo n.º 1
0
        private void RunTask(ScheduledTaskTracker tracker)
        {
            if (_isStopping)
            {
                return;
            }

            var scheduledTask = _pluginManagerService.GetScheduledTaskFromConfig(tracker.Config);

            if (scheduledTask == null)
            {
                throw new InvalidOperationException(
                          string.Format("Scheduled Task not found with type name = " + tracker.Config.FullTypeName));
            }

            scheduledTask.Execute();
        }
Exemplo n.º 2
0
        private void QueueTask(ScheduledTaskTracker tracker)
        {
            var schedule = CrontabSchedule.Parse(tracker.Config.CronExpression);

            var startTime   = DateTime.Now.WithoutSeconds();
            var lastRunTime = tracker.LastStart.GetValueOrDefault(startTime.AddMinutes(-1)).WithoutSeconds();

            var nextOccurenceTime = schedule.GetNextOccurrence(lastRunTime);

            if (startTime < nextOccurenceTime)
            {
                return;
            }

            lock (_queuedOrRunningTasksLock)
            {
                // Make sure the task isn't already queued
                if (_queuedOrRunningTasks.Contains(tracker.Config.FullTypeName))
                {
                    return;
                }

                _queuedOrRunningTasks.Add(tracker.Config.FullTypeName);

                Task.Factory
                .StartNew(() =>
                {
                    tracker.LastStart = DateTime.Now;

                    RunTask(tracker);

                    tracker.LastEnd = DateTime.Now;

                    lock (_queuedOrRunningTasksLock)
                    {
                        _queuedOrRunningTasks.Remove(tracker.Config.FullTypeName);
                    }
                });
            }
        }