/// <inheritdoc /> public virtual void Enqueue(IQueueableTask queueableTask) { Check.NotNull(queueableTask, nameof(queueableTask)); _queuedTasks.Enqueue(queueableTask); _semaphore.Release(); }
public TaskView(IQueueableTask task) { if (task == null) throw new ArgumentNullException(nameof(task)); Task = task; task.StatusChanged += Task_StatusChanged; _nameLabel = new Label { Font = Font.SystemFont.WithWeight(FontWeight.Bold).WithSize(15) }; _descriptionlabel = new Label { Font = Font.SystemFont.WithStyle(FontStyle.Italic) }; _spinner = new Spinner {Visible = false}; var hBox = new HBox(); hBox.PackStart(_spinner); hBox.PackStart(_descriptionlabel); PackStart(_nameLabel); PackStart(hBox); HeightRequest = 64; MinHeight = 64; UpdateLabels(); }
public TaskView(IQueueableTask task) { if (task == null) { throw new ArgumentNullException(nameof(task)); } Task = task; task.StatusChanged += Task_StatusChanged; _nameLabel = new Label { Font = Font.SystemFont.WithWeight(FontWeight.Bold).WithSize(15) }; _descriptionlabel = new Label { Font = Font.SystemFont.WithStyle(FontStyle.Italic) }; _spinner = new Spinner { Visible = false }; var hBox = new HBox(); hBox.PackStart(_spinner); hBox.PackStart(_descriptionlabel); PackStart(_nameLabel); PackStart(hBox); HeightRequest = 64; MinHeight = 64; UpdateLabels(); }
public void InsertAfter <TTask>(IQueueableTask afterTask) where TTask : IQueueableTask { var expression = _values.Aggregate <KeyValuePair <string, object>, ExplicitArgsExpression>(null, (current, keyValue) => current == null ? ObjectFactory.Container.With(keyValue.Key).EqualTo(keyValue.Value) : current.With(keyValue.Key).EqualTo(keyValue.Value)); _queueableTaskManager.InsertAfter(expression.GetInstance <TTask>(), afterTask); }
private void CurrentTaskFinished() { // Remove the event listener and unset the "current task" value. var task = _currentTask; _currentTask = null; // Raise the TaskFinished event. OnTaskFinished(new QueueableTaskEventArgs(task)); }
/// <summary> /// Adds the specified task to the queue. /// </summary> /// <param name="task">The task.</param> public void Add(IQueueableTask task) { if (task == null) { throw new ArgumentNullException(nameof(task)); } _log.WriteLine($"Adding {task} to the task queue."); _queuedTasks.Add(task); OnTaskAdded(new QueueableTaskEventArgs(task)); RunNext(); }
/// <summary> /// Inserts the specified task after the specified task. /// </summary> /// <param name="task">The task.</param> /// <param name="afterTask">The after.</param> public void InsertAfter(IQueueableTask task, IQueueableTask afterTask) { if (task == null) { throw new ArgumentNullException(nameof(task)); } if (afterTask == null) { throw new ArgumentNullException(nameof(afterTask)); } var index = _queuedTasks.IndexOf(afterTask); if (index < 0) { index = 0; } _queuedTasks.Insert(index, task); OnTaskAdded(new QueueableTaskEventArgs(task)); }
public int IndexOf(IQueueableTask task) { var index = _runningAndFinishedTasks.IndexOf(task); if (index >= 0) { return(index); } index = _queuedTasks.IndexOf(task); if (index < 0) { return(-1); } index += _runningAndFinishedTasks.Count; return(index); }
private async Task ExecuteQueuedTaskAsync(IQueueableTask queuedTask, IServiceProvider serviceProvider, CancellationToken cancellationToken) { var taskName = TypeNameHelper.GetTypeDisplayName(queuedTask); // Try to execute the task Exception exception = null; using (var ctx = new QueuedTaskExecutionContext(serviceProvider, cancellationToken)) { _logger.LogDebug($"Executing task \"{taskName}\""); var sw = Stopwatch.StartNew(); try { await queuedTask.RunAsync(ctx); } catch (Exception e) { if (!(e is OperationCanceledException)) { exception = e; } } sw.Stop(); _logger.LogDebug($"Executed task \"{taskName}\" in {sw.Elapsed.TotalMilliseconds}ms"); } // Report to the exception handler // Report to the exception handler TaskExecutionHelper.ReportUnhandledTaskException( exception, queuedTask, _taskExceptionHandler, serviceProvider, _logger ); }
public void InsertAfter <TTask>(IQueueableTask afterTask) where TTask : IQueueableTask { _queueableTaskManager.InsertAfter(_expression.GetInstance <TTask>(), afterTask); }
/// <summary> /// Initializes a new instance of the <see cref="QueueableTaskEventArgs" /> class. /// </summary> /// <param name="task">The task.</param> public QueueableTaskEventArgs(IQueueableTask task) { Task = task; }
private async void RunNext() { // Do not start new tasks if there are none queued. if (!_queuedTasks.Any()) { return; } // Do not start new tasks if there's already a running task. if (_currentTask != null) { return; } // Store the first task in the queue in _currentTask and add it to the running tasks list. _currentTask = _queuedTasks.First(); _queuedTasks.RemoveAt(0); _runningAndFinishedTasks.Add(_currentTask); // Ensure the dequeued task is awaiting being run. if (_currentTask.Status != TaskStatus.Queued && _currentTask.Status != TaskStatus.Break) { _log.WriteLine($"Skipping execution of task {_currentTask}. Task is not queued or in break mode."); RunNext(); return; } // Create a cancellation token source for this task. _cancellationTokenSource = new CancellationTokenSource(); try { _log.WriteLine($"Started task {_currentTask}."); await _currentTask.Run(_cancellationTokenSource.Token); _log.WriteLine( $"Task {_currentTask} ended in state {_currentTask.Status} at {_currentTask.CompletionPercentage}%."); // If the task requests a break, move it to the end of the queue. if (_currentTask.Status == TaskStatus.Break) { _runningAndFinishedTasks.Remove(_currentTask); Add(_currentTask); } else { if (_currentTask.CompletionPercentage != 100) { _currentTask.Status = TaskStatus.FinishedWithErrors; _currentTask.CompletionPercentage = 100; _currentTask.StatusDescription = "Task finished in an unkown error."; } } } catch (Exception e) { // Find the deepest inner exception to extract the error message from. while (e is AggregateException) { e = ((AggregateException)e).InnerException; } // Set the task status to "canceled" and change the status description to the error message. _currentTask.Status = TaskStatus.Canceled; _currentTask.CompletionPercentage = 100; _currentTask.StatusDescription = $"Failed: {e.Message}"; _log.WriteLine($"Task {_currentTask} threw an exception!", LogLevel.Fatal); _log.WriteException(e); } finally { // After the task has either successfully run or failed, a next task can be started. _log.WriteLine($"Task {_currentTask} process finished."); CurrentTaskFinished(); RunNext(); } }
public void InsertAfter <TTask>(IQueueableTask afterTask) where TTask : IQueueableTask { InsertAfter(ObjectFactory.GetInstance <TTask>(), afterTask); }