/// <inheritdoc /> public virtual ITaskWorker CreateTaskWorker(ITask task) { if (task == null) { throw new ArgumentNullException(nameof(task)); } ITaskWorkerConfiguration config = this.configuration[task.GetType()]; if (config == null) { throw new NotSupportedException <Type>(task.GetType()); } if (this.locator.CanResolve(config.WorkerType)) { return(this.locator.ResolveSingle <ITaskWorker>(config.WorkerType)); } else { return(config.WorkerType.CreateInstance <ITaskWorker>()); } }
public void StartTask(Guid taskId) { ITask task = this.repository.Tasks.GetById(taskId); if (task == null) { throw new ArgumentException("Task '{0}' was not found in storage.".FormatInvariant(taskId), "taskId"); } ITaskWorker taskWorker = this.taskFactory.CreateTaskWorker(task); ITaskMessageBusSender taskMessageBus = this.messageBus.Tasks.GetSender(task.GetType()); taskWorker.ReportProgress += (sender, e) => { this.repository.TaskRuntimeInfo.Progress(taskId, e.Percentage); taskMessageBus.NotifyTaskProgress(taskId, e.Percentage); }; this.messageBus.Tasks.Receiver.SubscribeForChannels(MessageBusChannel.TaskCancelRequest); bool isCanceled = false; this.messageBus.Tasks.Receiver.TaskCancelRequested += (_, e1) => { if (!isCanceled && (e1.TaskId == taskId)) { isCanceled = true; taskWorker.CancelTask(); } }; ITaskJobSettings settings = null; ITaskWorkerConfiguration config = this.configuration[task.GetType()]; if ((config != null) && config.HasTaskJobSettings) { settings = this.repository.TaskJobSettings.Get(task.GetType()); } ITaskRuntimeInfo taskInfo = this.repository.TaskRuntimeInfo.GetById(taskId); if (taskInfo == null) { throw new ArgumentException("Task '{0}' not found in storage.".FormatInvariant(taskId), "taskId"); } switch (taskInfo.Status) { case TaskStatus.Pending: case TaskStatus.InProgress: break; case TaskStatus.Canceled: throw new OperationCanceledException(); default: throw new ArgumentException("Task '{0}' status is '{1}'.".FormatInvariant(taskId, taskInfo.Status), "taskId"); } try { try { taskWorker.StartTask(task, settings); } catch (Exception ex) { this.repository.TaskRuntimeInfo.Fail(taskId, this.dateTimeProvider.UtcNow, ex); throw; } } finally { this.messageBus.Tasks.Receiver.UnsubscribeFromAllChannels(); if (taskWorker is IDisposable) { ((IDisposable)taskWorker).Dispose(); } } }