private async Task ExecuteInternal(TaskExecutionOptions options) { // Cancel the current execution, if any if (CurrentCancellationTokenSource != null) { throw new InvalidOperationException("Cannot execute a Task that is already running"); } var progress = new Progress<double>(); CurrentCancellationTokenSource = new CancellationTokenSource(); Logger.Info("Executing {0}", Name); ((TaskManager)TaskManager).OnTaskExecuting(this); progress.ProgressChanged += progress_ProgressChanged; TaskCompletionStatus status; CurrentExecutionStartTime = DateTime.UtcNow; Exception failureException = null; try { if (options != null && options.MaxRuntimeMs.HasValue) { CurrentCancellationTokenSource.CancelAfter(options.MaxRuntimeMs.Value); } var localTask = ScheduledTask.Execute(CurrentCancellationTokenSource.Token, progress); await localTask.ConfigureAwait(false); status = TaskCompletionStatus.Completed; } catch (OperationCanceledException) { status = TaskCompletionStatus.Cancelled; } catch (Exception ex) { Logger.ErrorException("Error", ex); failureException = ex; status = TaskCompletionStatus.Failed; } var startTime = CurrentExecutionStartTime; var endTime = DateTime.UtcNow; progress.ProgressChanged -= progress_ProgressChanged; CurrentCancellationTokenSource.Dispose(); CurrentCancellationTokenSource = null; CurrentProgress = null; OnTaskCompleted(startTime, endTime, status, failureException); }
/// <summary> /// Converts a TaskTriggerInfo into a concrete BaseTaskTrigger /// </summary> /// <param name="info">The info.</param> /// <returns>BaseTaskTrigger.</returns> /// <exception cref="System.ArgumentNullException"></exception> /// <exception cref="System.ArgumentException">Invalid trigger type: + info.Type</exception> public static ITaskTrigger GetTrigger(TaskTriggerInfo info) { var options = new TaskExecutionOptions { MaxRuntimeMs = info.MaxRuntimeMs }; if (info.Type.Equals(typeof(DailyTrigger).Name, StringComparison.OrdinalIgnoreCase)) { if (!info.TimeOfDayTicks.HasValue) { throw new ArgumentNullException(); } return new DailyTrigger { TimeOfDay = TimeSpan.FromTicks(info.TimeOfDayTicks.Value), TaskOptions = options }; } if (info.Type.Equals(typeof(WeeklyTrigger).Name, StringComparison.OrdinalIgnoreCase)) { if (!info.TimeOfDayTicks.HasValue) { throw new ArgumentNullException(); } if (!info.DayOfWeek.HasValue) { throw new ArgumentNullException(); } return new WeeklyTrigger { TimeOfDay = TimeSpan.FromTicks(info.TimeOfDayTicks.Value), DayOfWeek = info.DayOfWeek.Value, TaskOptions = options }; } if (info.Type.Equals(typeof(IntervalTrigger).Name, StringComparison.OrdinalIgnoreCase)) { if (!info.IntervalTicks.HasValue) { throw new ArgumentNullException(); } return new IntervalTrigger { Interval = TimeSpan.FromTicks(info.IntervalTicks.Value), TaskOptions = options }; } if (info.Type.Equals(typeof(SystemEventTrigger).Name, StringComparison.OrdinalIgnoreCase)) { if (!info.SystemEvent.HasValue) { throw new ArgumentNullException(); } return new SystemEventTrigger { SystemEvent = info.SystemEvent.Value, TaskOptions = options }; } if (info.Type.Equals(typeof(StartupTrigger).Name, StringComparison.OrdinalIgnoreCase)) { return new StartupTrigger(); } throw new ArgumentException("Unrecognized trigger type: " + info.Type); }
/// <summary> /// Executes the task /// </summary> /// <param name="options">Task options.</param> /// <returns>Task.</returns> /// <exception cref="System.InvalidOperationException">Cannot execute a Task that is already running</exception> public async Task Execute(TaskExecutionOptions options) { var task = ExecuteInternal(options); _currentTask = task; try { await task.ConfigureAwait(false); } finally { _currentTask = null; } }
/// <summary> /// Converts a TaskTriggerInfo into a concrete BaseTaskTrigger /// </summary> /// <param name="info">The info.</param> /// <returns>BaseTaskTrigger.</returns> /// <exception cref="System.ArgumentNullException"></exception> /// <exception cref="System.ArgumentException">Invalid trigger type: + info.Type</exception> public static ITaskTrigger GetTrigger(TaskTriggerInfo info) { var options = new TaskExecutionOptions { MaxRuntimeMs = info.MaxRuntimeMs }; if (info.Type.Equals(typeof(DailyTrigger).Name, StringComparison.OrdinalIgnoreCase)) { if (!info.TimeOfDayTicks.HasValue) { throw new ArgumentNullException(); } return(new DailyTrigger { TimeOfDay = TimeSpan.FromTicks(info.TimeOfDayTicks.Value), TaskOptions = options }); } if (info.Type.Equals(typeof(WeeklyTrigger).Name, StringComparison.OrdinalIgnoreCase)) { if (!info.TimeOfDayTicks.HasValue) { throw new ArgumentNullException(); } if (!info.DayOfWeek.HasValue) { throw new ArgumentNullException(); } return(new WeeklyTrigger { TimeOfDay = TimeSpan.FromTicks(info.TimeOfDayTicks.Value), DayOfWeek = info.DayOfWeek.Value, TaskOptions = options }); } if (info.Type.Equals(typeof(IntervalTrigger).Name, StringComparison.OrdinalIgnoreCase)) { if (!info.IntervalTicks.HasValue) { throw new ArgumentNullException(); } return(new IntervalTrigger { Interval = TimeSpan.FromTicks(info.IntervalTicks.Value), TaskOptions = options }); } if (info.Type.Equals(typeof(SystemEventTrigger).Name, StringComparison.OrdinalIgnoreCase)) { if (!info.SystemEvent.HasValue) { throw new ArgumentNullException(); } return(new SystemEventTrigger { SystemEvent = info.SystemEvent.Value, TaskOptions = options }); } if (info.Type.Equals(typeof(StartupTrigger).Name, StringComparison.OrdinalIgnoreCase)) { return(new StartupTrigger()); } throw new ArgumentException("Unrecognized trigger type: " + info.Type); }