TaskHandler.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using HttpDownload;
  2. using NettyClient;
  3. using NettyClient.proto;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Threading;
  8. using TaskDatabase;
  9. using TaskDatabase.Model;
  10. namespace Client
  11. {
  12. class TaskHandler
  13. {
  14. private SimpleMessageHandler simpleHandler;
  15. private TaskService taskService;
  16. public IList<TaskModel> taskModels;
  17. private TaskDownloadManager taskDownloadManager;
  18. private string defaultFileDir;
  19. IRevitCommandExcutor revitCommandExcutor;
  20. public void RunTasks(object obj) {
  21. while (true) {
  22. try
  23. {
  24. for (int i = taskModels.Count - 1; i > -1; --i) {
  25. TaskModel task = taskModels[i];
  26. TaskModel downloadException = null, commandException = null;
  27. bool isAllDownloaded = true;
  28. // 看当前任务依赖的文件下载状况
  29. foreach (DownloadTaskModel downloadTaskModel in task.DownloadTaskModelList) {
  30. DownloadTask download = taskDownloadManager.getDownloadTaskByDownloadTaskId(downloadTaskModel.Tid);
  31. if (download == null) // 如果还未加入下载队列
  32. {
  33. // 开始下载
  34. if (downloadTaskModel.Local_dir == null) {
  35. downloadTaskModel.Local_dir = defaultFileDir + downloadTaskModel.Tid + ".rvt";
  36. }
  37. download = taskDownloadManager.enqueueTask(task.Id, downloadTaskModel.Tid, downloadTaskModel.Task_url,
  38. downloadTaskModel.Local_dir, downloadTaskModel.Task_md5, downloadTaskModel.Downloaded_bytes);
  39. taskDownloadManager.runTask(download);
  40. isAllDownloaded &= false;
  41. }
  42. else {
  43. // 如果该下载发生异常
  44. if (download.Status == DownloadStatus.ExceptionStopped) {
  45. isAllDownloaded &= false;
  46. downloadException = task;
  47. break;
  48. }
  49. // 同步当前状态到数据库
  50. if (download.Status == DownloadStatus.Completed)
  51. {
  52. downloadTaskModel.Finish_time = GetTimeStampSeconds();
  53. isAllDownloaded &= true;
  54. }
  55. else {
  56. isAllDownloaded &= false;
  57. }
  58. syncSingleDownTask(download, downloadTaskModel);
  59. }
  60. } // foreach end
  61. // 如果该下载任务下载失败
  62. if (downloadException != null)
  63. {
  64. task.Task_status = -1;
  65. taskService.UpdateTask(task);
  66. taskModels.RemoveAt(i);
  67. simpleHandler.WriteMessage(MessageUtil.generateMessage(Command.DownloadError.ToString(), task.Id, ""));
  68. continue;
  69. }
  70. // 如果该任务的所有下载文件均已完成, 开始执行Revit命令
  71. if (isAllDownloaded) {
  72. try
  73. {
  74. HashSet<string> fileSet = new HashSet<string>();
  75. foreach(var downloadModel in task.DownloadTaskModelList) {
  76. fileSet.Add(downloadModel.Local_dir);
  77. }
  78. try
  79. {
  80. string resultJson = revitCommandExcutor.ExecuteCmd(task.Task_cmd, task.Task_param, fileSet);
  81. simpleHandler.WriteMessage(MessageUtil.generateMessage(Command.TaskSuccess.ToString(), task.Id, resultJson));
  82. task.Task_status = 1;
  83. task.Task_result_json = resultJson;
  84. taskService.UpdateTask(task);
  85. taskModels.RemoveAt(i);
  86. }
  87. catch(Exception ex) {
  88. Console.WriteLine(ex.StackTrace);
  89. commandException = task;
  90. }
  91. }
  92. catch(Exception ex) {
  93. Console.WriteLine(ex.StackTrace);
  94. }
  95. }
  96. if (commandException != null)
  97. {
  98. task.Task_status = -2;
  99. taskService.UpdateTask(task);
  100. taskModels.RemoveAt(i);
  101. simpleHandler.WriteMessage(MessageUtil.generateMessage(Command.CommandError.ToString(), task.Id, ""));
  102. continue;
  103. }
  104. }
  105. taskService.removeErrorTask();
  106. }
  107. catch (Exception ex){
  108. Console.WriteLine(ex.StackTrace);
  109. }
  110. Thread.Sleep(1000);
  111. }
  112. }
  113. private void syncSingleDownTask(DownloadTask downTask, DownloadTaskModel downloadModel)
  114. {
  115. bool changed = false;
  116. if (downloadModel.Downloaded_bytes != downTask.BytesWritten)
  117. {
  118. downloadModel.Downloaded_bytes = downTask.BytesWritten;
  119. changed = true;
  120. }
  121. if (downloadModel.File_bytes != downTask.totalFileSize)
  122. {
  123. downloadModel.File_bytes = downTask.totalFileSize;
  124. changed = true;
  125. }
  126. if(changed)
  127. taskService.UpdateDownload(downloadModel);
  128. }
  129. public void setSimpleMessageHandler(SimpleMessageHandler simpleHandler)
  130. {
  131. this.simpleHandler = simpleHandler;
  132. }
  133. private TaskHandler(SimpleMessageHandler simpleHandler, TaskService taskService)
  134. {
  135. this.simpleHandler = simpleHandler;
  136. this.taskService = taskService;
  137. }
  138. public TaskHandler(SimpleMessageHandler simpleHandler, TaskService taskService, TaskDownloadManager taskDownloadManager, string defaultFileDir, IRevitCommandExcutor revitCommandExcutor) : this(simpleHandler, taskService)
  139. {
  140. this.taskDownloadManager = taskDownloadManager;
  141. this.defaultFileDir = defaultFileDir;
  142. this.revitCommandExcutor = revitCommandExcutor;
  143. }
  144. internal void SyncAllTask()
  145. {
  146. IList<TaskModel> tasks = taskService.GetTasksByStatus(0); // 任务状态 0 未执行完的任务, 1 执行完成 -1 下载出错 -2 revit命令执行异常
  147. taskModels = tasks;
  148. }
  149. internal bool addOneTask(int taskId, string msgContent) {
  150. try
  151. {
  152. TaskModel taskModel = JsonConvert.DeserializeObject<TaskModel>(msgContent);
  153. taskService.AddTask(taskModel);
  154. TaskModel task = taskService.GetTasksByTaskId(taskId)[0];
  155. taskModels.Add(task);
  156. foreach (DownloadTaskModel download in taskModel.DownloadTaskModelList) {
  157. download.Local_dir = defaultFileDir + download.Tid + ".rvt";
  158. taskService.UpdateDownload(download);
  159. }
  160. return true;
  161. }
  162. catch(Exception ex) {
  163. Console.WriteLine(ex.Message+ex.StackTrace);
  164. return false;
  165. }
  166. }
  167. internal bool isContainTask(int taskId)
  168. {
  169. try
  170. {
  171. return taskService.isContainsTask(taskId);
  172. }
  173. catch (Exception ex){
  174. Console.WriteLine(ex.StackTrace);
  175. return true;
  176. }
  177. }
  178. public static long GetTimeStampSeconds() {
  179. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  180. return (long)ts.TotalSeconds;
  181. }
  182. }
  183. }