private async Task RestoreTasksAsync(CancellationToken cancellationToken = default) { foreach (var taskState in await taskRepository.GetActualTasksAsync(cancellationToken)) { var taskMetadata = MetadataManager.FindTaskMetadata(taskState.TaskModel); if (taskMetadata == null) { throw new ArgumentException(); } var taskContainer = new TaskContainer(taskState.TaskId, taskState.TaskModel, taskState.CreatedDate); if (taskState.ExecutorId.HasValue) { if (!executors.TryGetValue(taskState.ExecutorId.Value, out TaskExecutor executor)) { executors.TryAdd(taskState.ExecutorId.Value, executor = new TaskExecutor(taskState.ExecutorId.Value)); } executor.AddTask(taskContainer); Interlocked.Increment(ref countExecutingTasks); } else { commandQueue.Enqueue(taskContainer); } } }
public void FindTaskMetadataByObject() { var task = new TestTask(); var taskMetadata = metadataManager.FindTaskMetadata(task); Assert.NotNull(taskMetadata); Assert.Equal(task.GetType(), taskMetadata.TaskType); }
public async Task <IEnumerable <TaskState> > GetActualTasksAsync(CancellationToken cancellationToken = default) { var tasks = new List <TaskState>(); var cursor = await dbContext.Tasks.FindAsync(it => it.IsFinished == false, cancellationToken : cancellationToken); foreach (var doc in cursor.ToEnumerable(cancellationToken)) { var taskMetadata = taskMetadataManager.FindTaskMetadata(doc.TypeName); if (taskMetadata == null) { throw new Exception(); } var jObj = JObject.Parse(doc.Model.ToString()); var taskState = new TaskState { TaskId = doc.Id, CreatedDate = doc.CreatedDate, TaskModel = jObj.ToObject(taskMetadata.TaskType), EndDate = doc.EndDate }; if (doc.Execution != null) { taskState.ExecutorId = doc.Execution.ExecutorId; taskState.StartedDate = doc.Execution.StartedDate; } tasks.Add(taskState); } return(tasks); }
public TaskExecutor(ITaskAllocator taskAllocator, ITaskHandlerLocator handlerLocator, ITaskMetadataManager metadataManager, IServiceProvider serviceProvider, ILogger <TaskExecutor> logger) { this.taskAllocator = taskAllocator ?? throw new ArgumentNullException(nameof(taskAllocator)); this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); foreach (var taskType in handlerLocator.TaskTypes) { var taskMetadata = metadataManager.FindTaskMetadata(taskType); if (taskMetadata == null) { throw new InvalidOperationException(); } handlerFactories.Add(taskType, new TaskHandlerMetadata(taskMetadata.TaskName, HandlerBaseType.MakeGenericType(taskType))); } }