private void InitTaskQueue() { Task task1 = Task.Factory.StartNew(() => { Completed_TaskQueue = new ConcurrentDictionary <string, T8TaskEntity>(); string taskQueuePath = Common.GetTaskQueueFileFullpath(TaskQueueType.Completed); List <T8TaskEntity> taskList = SerializableHelper <List <T8TaskEntity> > .BinaryDeserialize(taskQueuePath); if (taskList != null && taskList.Count > 0) { foreach (T8TaskEntity t8Task in taskList) { Completed_TaskQueue.TryAdd(t8Task.GenerateTaskQueueKey, t8Task); } } }); Task task2 = Task.Factory.StartNew(() => { Processing_TaskQueue = new ConcurrentDictionary <string, T8TaskEntity>(); string taskQueuePath = Common.GetTaskQueueFileFullpath(TaskQueueType.Processing); List <T8TaskEntity> taskList = SerializableHelper <List <T8TaskEntity> > .BinaryDeserialize(taskQueuePath); if (taskList != null && taskList.Count > 0) { foreach (T8TaskEntity t8Task in taskList) { Completed_TaskQueue.TryAdd(t8Task.GenerateTaskQueueKey, t8Task); } } }); Task task3 = Task.Factory.StartNew(() => { Error_TaskQueue = new ConcurrentDictionary <string, T8TaskEntity>(); string taskQueuePath = Common.GetTaskQueueFileFullpath(TaskQueueType.Error); List <T8TaskEntity> taskList = SerializableHelper <List <T8TaskEntity> > .BinaryDeserialize(taskQueuePath); if (taskList != null && taskList.Count > 0) { foreach (T8TaskEntity t8Task in taskList) { Completed_TaskQueue.TryAdd(t8Task.GenerateTaskQueueKey, t8Task); } } }); Task task4 = Task.Factory.StartNew(() => { UserManual_TaskQueue = new ConcurrentDictionary <string, T8TaskEntity>(); string taskQueuePath = Common.GetTaskQueueFileFullpath(TaskQueueType.UserManual); List <T8TaskEntity> taskList = SerializableHelper <List <T8TaskEntity> > .BinaryDeserialize(taskQueuePath); if (taskList != null && taskList.Count > 0) { foreach (T8TaskEntity t8Task in taskList) { Completed_TaskQueue.TryAdd(t8Task.GenerateTaskQueueKey, t8Task); } } }); Task.WaitAll(new[] { task1, task2, task3, task4 }); }
/// <summary> /// 构建T8任务 /// </summary> private void BuildServiceTask(T8ConfigItemEntity t8ConfigItem, CancellationToken ct) { try { ct.ThrowIfCancellationRequested(); ACreateTask createTaskService = AutoFacContainer.ResolveNamed <ACreateTask>(typeof(ServiceCreateTask).Name); T8TaskEntity t8Task = createTaskService.CreateTask(); if (t8Task != null) { //获取文件名 AFileName aFileNameService = AutoFacContainer.ResolveNamed <AFileName>(typeof(GeneralFileName).Name); t8Task.T8FileEntity.GeneralFileInfo = new T8FileInfoEntity { FileName = aFileNameService.FileName(t8Task.T8FileEntity), FilePath = aFileNameService.FileFullName(t8Task.T8FileEntity) }; T8TaskEntity taskEntityQueueItem; if (Completed_TaskQueue.TryGetValue(t8Task.GenerateTaskQueueKey, out taskEntityQueueItem)) { return; } if (Processing_TaskQueue.TryGetValue(t8Task.GenerateTaskQueueKey, out taskEntityQueueItem)) { return; } if (Error_TaskQueue.TryGetValue(t8Task.GenerateTaskQueueKey, out taskEntityQueueItem)) { return; } //添加到执行中队列 if (!Common.AddTaskToQueue(Processing_TaskQueue, t8Task, TaskQueueType.Processing)) { LogUtil.WriteLog($"任务队列[{t8Task.TaskTitle}]添加失败"); Common.SetTaskErrorStatus(t8Task, "添加Processing_TaskQueue失败"); Common.AddTaskToQueue(Error_TaskQueue, t8Task, TaskQueueType.Error); return; } //构造数据文件产品并执行 DbFileProductDirector director = new DbFileProductDirector(); ADbFileProductBuilder productBuilder = new DbFileProductBuilder(); director.ConstructProduct(productBuilder); DbFileProduct product = productBuilder.GetDbFileProduct(); product.Execute(t8Task, cts.Token); //任务结束 1从Processing_TaskQueue队列移走 2任务完成,移入Completed_TaskQueue队列 3任务失败,移入 if (Common.RemoveTaskFromQueue(Processing_TaskQueue, t8Task, TaskQueueType.Processing)) { if (t8Task.T8TaskStatus == T8TaskStatus.Complete) { Common.AddTaskToQueue(Completed_TaskQueue, t8Task, TaskQueueType.Completed); LogUtil.WriteLog($"T8任务[{t8Task.TaskTitle}]执行完成,转移到Completed_TaskQueue队列"); } else { Common.AddTaskToQueue(Error_TaskQueue, t8Task, TaskQueueType.Error); LogUtil.WriteLog($"T8任务[{t8Task.TaskTitle}]执行失败,转移到Error_TaskQueue队列,等待下次重试"); } } } else { LogUtil.WriteLog("创建任务实体失败"); } } catch (Exception ex) { LogUtil.WriteLog(ex); } }