public void StartTask(ITask task, ITaskJobSettings settings) { if (task == null) { throw new ArgumentNullException(nameof(task)); } SampleTask demoTask = (SampleTask)task; int totalDurationInSeconds = demoTask.Details.Sum(d => d.DurationInSeconds); int pastDurationInSeconds = 0; foreach (SampleTaskDetail duration in demoTask.Details) { if (this.isCanceled) { break; } Thread.Sleep(TimeSpan.FromSeconds(duration.DurationInSeconds)); pastDurationInSeconds += duration.DurationInSeconds; var reportProgressEventHandler = this.ReportProgress; if (reportProgressEventHandler != null) { double percent = 100.0 * pastDurationInSeconds / totalDurationInSeconds; reportProgressEventHandler(this, new TaskWorkerProgressEventArgs(percent)); } } }
public void SetTaskJobSettings(Type taskType, ITaskJobSettings settings) { this.RecordMethodCall(taskType, settings); if (settings == null) { throw new ArgumentNullException(nameof(settings)); } }
/// <summary> /// Sets task job settings for task type. /// </summary> /// <typeparam name="TTask">The type of the task.</typeparam> /// <param name="repository">The repository to extend.</param> /// <param name="settings">The settings to set for the specified task type.</param> /// <exception cref="ArgumentNullException">Parameter <paramref name="repository"/> or <paramref name="settings" /> is null.</exception> public static void Set <TTask>(this ITaskJobSettingsRepository repository, ITaskJobSettings settings) where TTask : ITask { if (repository == null) { throw new ArgumentNullException(nameof(repository)); } repository.Set(typeof(TTask), settings); }
public void Get1() { FakeTaskJobSettings settings1 = new FakeTaskJobSettings(); this.Repository.Set(typeof(FakeTask), settings1); ITaskJobSettings settings2 = this.Repository.Get<FakeTask>(); Assert.AreEqual(settings1, settings2); this.Repository.AssertMethodCallOnceWithArguments(r => r.Get(typeof(FakeTask))); }
public void Get1() { FakeTaskJobSettings settings1 = new FakeTaskJobSettings(); this.Facade.PredefineResult(settings1, r => r.GetTaskJobSettings(typeof(FakeTask))); ITaskJobSettings settings2 = this.Facade.GetTaskJobSettings <FakeTask>(); Assert.AreEqual(settings1, settings2); this.Facade.AssertMethodCallOnceWithArguments(r => r.GetTaskJobSettings(typeof(FakeTask))); }
/// <inheritdoc /> public void Set(Type taskType, ITaskJobSettings settings) { Trace.WriteLine("ENTER: Setting task job settings '{0}' for task '{1}' ...".FormatInvariant(settings, taskType)); if (taskType == null) { throw new ArgumentNullException(nameof(taskType)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } if (!typeof(ITask).IsAssignableFrom(taskType)) { throw new ArgumentException("Type '{0}' does not implement '{1}'.".FormatInvariant(taskType, typeof(ITask), nameof(taskType))); } byte[] content = this.serializer.Serialize(settings); if (this.serializer.CanDetermineEntityTypeFromContent) { this.provider.SetHashValue(RedisTaskJobSettingsRepository.RedisTaskJobSettingsHashKey, taskType.Name, content); } else { using (IRedisPipeline pipeline = this.provider.CreatePipeline()) { pipeline.SetHashValue(RedisTaskJobSettingsRepository.RedisTaskJobSettingsHashKey, taskType.Name, content); pipeline.SetHashValue(RedisTaskJobSettingsRepository.RedisTaskJobSettingsHashKey, taskType.Name + "$Type", RedisConverter.ToString(settings.GetType(), false)); pipeline.Flush(); } } Trace.WriteLine("EXIT: Task job settings '{0}' for task '{1}' set.".FormatInvariant(settings, taskType)); }
public void StartTask(ITask task, ITaskJobSettings settings) { this.RecordMethodCall(task, settings); this.ExecutePredefinedMethod(task, settings); }
/// <summary> /// Sets task job settings for task type. /// </summary> /// <param name="facade">The <see cref="ITaskProcessorFacade"/> instance to extend.</param> /// <param name="settings">The settings to set for the specified task type.</param> /// <typeparam name="TTask">The type of the task.</typeparam> /// <exception cref="ArgumentNullException">Parameter <paramref name="facade"/> or <paramref name="settings" /> is null.</exception> public static void SetTaskJobSettings <TTask>(this ITaskProcessorFacade facade, ITaskJobSettings settings) where TTask : ITask { if (facade == null) { throw new ArgumentNullException(nameof(facade)); } facade.SetTaskJobSettings(typeof(TTask), settings); }
/// <inheritdoc /> public void SetTaskJobSettings(Type taskType, ITaskJobSettings settings) { this.repository.TaskJobSettings.Set(taskType, settings); }
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(); } } }