You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

184 lines
6.3 KiB

  1. using Learun.Application.Base.SystemModule;
  2. using Learun.Ioc;
  3. using Learun.Util;
  4. using Quartz;
  5. using System;
  6. using System.Threading;
  7. namespace Learun.Application.Extention.TaskScheduling
  8. {
  9. /// <summary>
  10. /// 版 本 Learun-ADMS V7.0.3 力软敏捷开发框架
  11. /// Copyright (c) 2013-2018 上海力软信息技术有限公司
  12. /// 创 建:超级管理员
  13. /// 日 期:2019-01-09 16:07
  14. /// 描 述:任务计划模板信息
  15. /// </summary>
  16. public class SchedulingHelper : IJob
  17. {
  18. private TSProcessIBLL tSProcessIBLL = new TSProcessBLL();
  19. private TSSchemeIBLL tSSchemeIBLL = new TSSchemeBLL();
  20. private TSLogIBLL tSLogIBLL = new TSLogBLL();
  21. private DatabaseLinkIBLL databaseLinkIBLL = new DatabaseLinkBLL();
  22. /// <summary>
  23. /// 任务执行方法
  24. /// </summary>
  25. /// <param name="keyValue">任务进程主键</param>
  26. /// <returns></returns>
  27. private bool _Execute(string keyValue) {
  28. bool isOk = true;
  29. string msg = "执行成功";
  30. TSProcessEntity tSProcessEntity = null;
  31. TSSchemeEntity tSSchemeEntity = null;
  32. // 获取一个任务进程
  33. try
  34. {
  35. tSProcessEntity = tSProcessIBLL.GetProcessEntity(keyValue);
  36. }
  37. catch (Exception ex)
  38. {
  39. isOk = false;
  40. msg = "获取任务进程异常:" + ex.Message;
  41. }
  42. if (tSProcessEntity != null && tSProcessEntity.F_State != 1 && tSProcessEntity.F_State != 2) {
  43. return true;
  44. }
  45. // 获取对应的任务模板
  46. if (isOk)
  47. {
  48. try
  49. {
  50. tSSchemeEntity = tSSchemeIBLL.GetSchemeEntity(tSProcessEntity.F_SchemeId);
  51. }
  52. catch (Exception ex)
  53. {
  54. isOk = false;
  55. msg = "获取任务模板异常:" + ex.Message;
  56. }
  57. }
  58. bool flag = false;
  59. // 执行任务
  60. if (isOk)
  61. {
  62. try
  63. {
  64. TSSchemeModel tSSchemeModel = tSSchemeEntity.F_Scheme.ToObject<TSSchemeModel>();
  65. switch (tSSchemeModel.methodType)
  66. {
  67. case 1:// sql
  68. databaseLinkIBLL.ExecuteBySql(tSSchemeModel.dbId, tSSchemeModel.strSql);
  69. break;
  70. case 2:// 存储过程
  71. databaseLinkIBLL.ExecuteByProc(tSSchemeModel.dbId, tSSchemeModel.procName);
  72. break;
  73. case 3:// 接口
  74. if (tSSchemeModel.urlType == "1")
  75. {
  76. HttpMethods.Get(tSSchemeModel.url);
  77. }
  78. else
  79. {
  80. HttpMethods.Post(tSSchemeModel.url);
  81. }
  82. break;
  83. case 4:// 依赖注入
  84. if (!string.IsNullOrEmpty(tSSchemeModel.iocName) && UnityIocHelper.TsInstance.IsResolve<ITsMethod>(tSSchemeModel.iocName))
  85. {
  86. ITsMethod iTsMethod = UnityIocHelper.TsInstance.GetService<ITsMethod>(tSSchemeModel.iocName);
  87. iTsMethod.Execute();
  88. }
  89. break;
  90. }
  91. if (tSSchemeModel.executeType == 1) {
  92. flag = true;
  93. }
  94. }
  95. catch (Exception ex)
  96. {
  97. isOk = false;
  98. msg = "执行方法出错:" + ex.Message;
  99. }
  100. }
  101. try
  102. {
  103. // 新增一条任务日志
  104. TSLogEntity logEntity = new TSLogEntity()
  105. {
  106. F_ExecuteResult = isOk ? 1 : 2,
  107. F_Des = msg,
  108. F_ProcessId = keyValue
  109. };
  110. logEntity.Create();
  111. tSLogIBLL.SaveEntity("", logEntity);
  112. if (tSProcessEntity.F_State == 1) {
  113. tSProcessEntity.F_State = 2;
  114. if (flag) {
  115. tSProcessEntity.F_State = 4;
  116. }
  117. tSProcessIBLL.SaveEntity(tSProcessEntity.F_Id, tSProcessEntity);
  118. }
  119. }
  120. catch (Exception)
  121. {
  122. }
  123. return isOk;
  124. }
  125. /// <summary>
  126. /// 任务执行方法
  127. /// </summary>
  128. /// <param name="context"></param>
  129. public void Execute(IJobExecutionContext context)
  130. {
  131. try
  132. {
  133. JobDataMap dataMap = context.JobDetail.JobDataMap;
  134. string keyValue = dataMap.GetString("keyValue");
  135. TSProcessEntity tSProcessEntity = null;
  136. TSSchemeEntity tSSchemeEntity = null;
  137. if (!_Execute(keyValue)) { // 如果异常,需要重新执行一次
  138. tSProcessEntity = tSProcessIBLL.GetProcessEntity(keyValue);
  139. if (tSProcessEntity != null) {
  140. tSSchemeEntity = tSSchemeIBLL.GetSchemeEntity(tSProcessEntity.F_SchemeId);
  141. if (tSSchemeEntity != null)
  142. {
  143. TSSchemeModel tSSchemeModel = tSSchemeEntity.F_Scheme.ToObject<TSSchemeModel>();
  144. if (tSSchemeModel.isRestart == 1) {
  145. for (int i = 0; i < tSSchemeModel.restartNum; i++)
  146. {
  147. Thread.Sleep(60 * 1000 * tSSchemeModel.restartMinute); // 停顿1000毫秒
  148. if (_Execute(keyValue))
  149. {
  150. break;
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. catch (Exception)
  159. {
  160. }
  161. }
  162. }
  163. }