Ejemplo n.º 1
0
 private void ApplyTaskStatus(TaskDescriptor descriptor)
 {
     if (descriptor.TaskType == TaskType.Scheduled)
     {
         if (descriptor.TaskState != TaskState.Suspended)
         {
             descriptor.TaskState = TaskState.Finished;
             descriptor.Timer.Dispose();
             RemoveTask(descriptor.TaskId);
         }
     }
     else if (descriptor.TaskType == TaskType.Interval)
     {
         if (descriptor.TaskState != TaskState.Suspended && descriptor.TaskState != TaskState.Paused && descriptor.TaskState != TaskState.Aborted && descriptor.TaskState != TaskState.Finished)
         {
             descriptor.TaskState = TaskState.Pending;
         }
     }
 }
Ejemplo n.º 2
0
 private bool FinishTaskIfRequired(TaskDescriptor descriptor)
 {
     if (descriptor.TaskType == TaskType.Interval &&
         (descriptor.EndDate.HasValue && DateTime.Now > descriptor.EndDate))
     {
         // NOTE: check this case for long frequent tasks.
         descriptor.TaskState = TaskState.Finished;
         descriptor.Timer.Dispose();
         RemoveTask(descriptor.TaskId);
         return true;
     }
     bool result = false;
     if (descriptor.TaskType == TaskType.Interval && 
         (descriptor.ExecuteTimesLimit.HasValue && descriptor.ExecuteTimesLimit <= descriptor.ExecutingTimes))
     {
         descriptor.Timer.Dispose();
         result = true;
     }
     if (descriptor.TaskType == TaskType.Interval && 
         (descriptor.ExecuteTimesLimit.HasValue && descriptor.ExecuteTimesLimit <= descriptor.ExecutedTimes))
     {
         //logger.Info("Finished task " + descriptor.TaskId);
         descriptor.TaskState = TaskState.Finished;
         RemoveTask(descriptor.TaskId);
         result= true;
     }
     return result;
 }
Ejemplo n.º 3
0
        private string AddTimerTask(ITask task, DateTime? startDate, long interval, DateTime? endDate, int? repeatTimes)
        {
            //  AssertUtils.Check(!startDate.HasValue || startDate > DateTime.Now, "Start date should be greater then now.", "startDate");
            if (interval == 0) interval = -1;
            DateTime now = DateTime.Now;
            //if (startDate.HasValue)
            //{
            //    startDate = startDate.Value;

            //    if (startDate <= now)
            //    {
            //        startDate = startDate.Value.Add(now.Date.AddDays(startDate.Value.TimeOfDay <= now.TimeOfDay ? 1 : 0) - startDate.Value.Date);
            //    }
            // }

            // This check required for Scheduled tasks only.
            AssertUtils.Check(interval != -1 || !startDate.HasValue || !endDate.HasValue || startDate <= endDate,
                              "Start date should be less then end date.", "startDate, endDate");

            long startOffset = startDate.HasValue ? (long) startDate.Value.Subtract(now).TotalMilliseconds : 0;
            if (startOffset <= 0 && interval > 0)
            {
                // If start date is in past, we'll add interval * min number to make start offset > 0. Previous logic with adding days is not valid.
                var multiplier = (int) Math.Floor(Math.Abs(startOffset)/(double) interval) + 1;
                startOffset += multiplier*interval;
            }
            else if(startOffset <= 0 && startOffset > -120000 && interval == -1)
            {
                startOffset = 0;
            }

            string taskId = GenerateTaskId();
            if (startOffset > int.MaxValue ) throw new ArgumentOutOfRangeException("startDate", "The start date  {0:dd MMM yyyy} is too far from now.".F(startDate));
            if (startOffset <= -120000 && interval <= 0)
            {
                logger.WarnFormat("The task {0} is obsolete, skipped. Type: {1}. Start date: {2}. Interval: {3} ms.", task.Description,
                              TaskType.Scheduled, now.AddMilliseconds(startOffset), 0);
                return "";
            }
            var taskDescriptor = new TaskDescriptor(taskId)
            {
                Task = task,
                TaskType = interval == -1 ? TaskType.Scheduled : TaskType.Interval,
                Interval = interval,
                EndDate = endDate,
                ExecuteTimesLimit = repeatTimes
            };
            taskDescriptor.Timer = new Timer(TimerCallBack, taskDescriptor, startOffset, interval);
            tasks[taskId] = taskDescriptor;
            logger.InfoFormat("Adding {0} task. Type: {1}. Start date: {2}. Interval: {3} ms.", task.Description,
                              taskDescriptor.TaskType, now.AddMilliseconds(startOffset),
                              taskDescriptor.Interval);
            return taskId;
        }