TaskHandler.cs 8.7 KB

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