EAPServiceWrapper.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. using System;
  2. using System.ComponentModel;
  3. using System.Xml;
  4. using System.Data;
  5. using System.Reflection;
  6. using System.Configuration;
  7. using Autofac;
  8. using Bowin.Common.Log;
  9. using EMIS.CommonLogic.SystemServices;
  10. using EMIS.Entities;
  11. namespace EMIS.Services
  12. {
  13. #region 所有的服务工作接口 IServiceJob
  14. /// <summary>
  15. /// IServiceJob : 所有的服务工作接口
  16. /// </summary>
  17. public interface IServiceJob
  18. {
  19. string JobID { get; set; }
  20. string JobName { get; set; }
  21. Guid SchedulerID { get; set; }
  22. int MaxThreadSize { get; set; }
  23. bool IsDebug { get; set; }
  24. string CertFile { get; set; }
  25. void ExecuteJob();
  26. DateTime AllTimeBegin { get; set; }
  27. DateTime AllTimeEnd { get; set; }
  28. int AllUseSeconds { get; set; }
  29. string Desc { get; set; }
  30. }
  31. #endregion
  32. #region 所有Job的基类
  33. /// <summary>
  34. /// 所有Job的基类。
  35. /// </summary>
  36. public abstract class EAPServiceJob : IServiceJob
  37. {
  38. #region 实现IServiceJob接口
  39. private string _jobid = "";
  40. private string _jobname = "";
  41. private Guid _schedulerid = Guid.Empty;
  42. private int _MaxThreadSize = 1;
  43. private bool _isdubug = false;
  44. private string _certfile = "";
  45. DateTime _AllTimeBegin;
  46. DateTime _AllTimeEnd;
  47. int _AllUseSeconds;
  48. DateTime _OneScheduleTimeBegin;
  49. DateTime _OneScheduleTimeEnd;
  50. int _OneScheduleUseSeconds;
  51. bool _Successful;
  52. string _Desc;
  53. /// <summary>
  54. /// 任务ID
  55. /// </summary>
  56. public string JobID
  57. {
  58. get { return _jobid; }
  59. set { _jobid = value; }
  60. }
  61. /// <summary>
  62. /// 任务名称
  63. /// </summary>
  64. public string JobName
  65. {
  66. get { return _jobname; }
  67. set { _jobname = value; }
  68. }
  69. /// <summary>
  70. /// 执行ID
  71. /// </summary>
  72. public Guid SchedulerID
  73. {
  74. get { return _schedulerid; }
  75. set { _schedulerid = value; }
  76. }
  77. /// <summary>
  78. /// 子服务同时最大线程数
  79. /// </summary>
  80. public int MaxThreadSize
  81. {
  82. get { return _MaxThreadSize; }
  83. set { _MaxThreadSize = value; }
  84. }
  85. /// <summary>
  86. /// 是否调试状态
  87. /// </summary>
  88. public bool IsDebug
  89. {
  90. get { return _isdubug; }
  91. set { _isdubug = value; }
  92. }
  93. /// <summary>
  94. /// 证书文件
  95. /// </summary>
  96. public string CertFile
  97. {
  98. get { return this._certfile; }
  99. set { this._certfile = value; }
  100. }
  101. /// <summary>
  102. /// 总体执行开始时间
  103. /// </summary>
  104. public DateTime AllTimeBegin
  105. {
  106. get { return _AllTimeBegin; }
  107. set { _AllTimeBegin = value; }
  108. }
  109. /// <summary>
  110. /// 总体执行结束时间
  111. /// </summary>
  112. public DateTime AllTimeEnd
  113. {
  114. get { return _AllTimeEnd; }
  115. set { _AllTimeEnd = value; }
  116. }
  117. /// <summary>
  118. /// 总体服务执行耗时秒数
  119. /// </summary>
  120. public int AllUseSeconds
  121. {
  122. get { return _AllUseSeconds; }
  123. set { _AllUseSeconds = value; }
  124. }
  125. /// <summary>
  126. /// 单个服务执行开始时间
  127. /// </summary>
  128. public DateTime OneScheduleTimeBegin
  129. {
  130. get { return _OneScheduleTimeBegin; }
  131. set { _OneScheduleTimeBegin = value; }
  132. }
  133. /// <summary>
  134. /// 单个服务执行结束时间
  135. /// </summary>
  136. public DateTime OneScheduleTimeEnd
  137. {
  138. get { return _OneScheduleTimeEnd; }
  139. set { _OneScheduleTimeEnd = value; }
  140. }
  141. /// <summary>
  142. /// 单个服务执行耗时秒数
  143. /// </summary>
  144. public int OneScheduleUseSeconds
  145. {
  146. get { return _OneScheduleUseSeconds; }
  147. set { _OneScheduleUseSeconds = value; }
  148. }
  149. /// <summary>
  150. /// 是否执行成功
  151. /// </summary>
  152. public bool Successful
  153. {
  154. get { return _Successful; }
  155. set { _Successful = value; }
  156. }
  157. /// <summary>
  158. /// 执行结果备注信息
  159. /// </summary>
  160. public string Desc
  161. {
  162. get { return _Desc; }
  163. set { _Desc = value; }
  164. }
  165. #endregion
  166. #region 运行JOB
  167. public void RunJobOnce()
  168. {
  169. this.MyJob();
  170. }
  171. #endregion
  172. public virtual void ExecuteJob()
  173. {
  174. //起止配对;容易找起止。
  175. Guid tagID = Guid.NewGuid();
  176. try
  177. {
  178. this.Init();
  179. Desc = "服务【" + this._jobname + "】开始执行:{" + tagID.ToString() + "}";
  180. LogHelper.WriteLog(LogType.ServiceLog, Desc);
  181. //先置最后执行时间;防止多线程一次集中连续执行。
  182. using (var scop = Program.AutofacContainer.BeginLifetimeScope())
  183. {
  184. var ScheduleServices = scop.Resolve<IScheduleServices>();
  185. ScheduleServices.UpdateLastRun(SchedulerID);
  186. }
  187. this.MyJob();
  188. OneScheduleTimeEnd = System.DateTime.Now;
  189. TimeSpan ts = new TimeSpan();
  190. ts = OneScheduleTimeEnd - OneScheduleTimeBegin;
  191. OneScheduleUseSeconds = Convert.ToInt32(ts.TotalSeconds);
  192. Desc = "服务【" + this._jobname + "】结束执行;耗时:" + OneScheduleUseSeconds.ToString() + "秒。{" + tagID.ToString() + "}";
  193. LogHelper.WriteLog(LogType.ServiceLog, Desc);
  194. Successful = true;
  195. UpdateSchedulingLog(SchedulerID, JobID, OneScheduleTimeBegin, OneScheduleTimeEnd, OneScheduleUseSeconds,
  196. Successful, "执行成功");
  197. }
  198. catch (Exception err)
  199. {
  200. OneScheduleTimeEnd = System.DateTime.Now;
  201. Desc = "服务【" + this._jobname + "】执行异常:" + err.Message;
  202. TimeSpan ts = new TimeSpan();
  203. ts = OneScheduleTimeEnd - OneScheduleTimeBegin;
  204. OneScheduleUseSeconds = Convert.ToInt32(ts.TotalSeconds); //转换时间间隔为 秒
  205. LogHelper.WriteLog(LogType.ServiceLog, "服务【" + this._jobname + "】结束;耗时:" + OneScheduleUseSeconds.ToString() + "秒;{" + tagID.ToString() + "} \r\n 执行结果:\r\n " + Desc);
  206. Successful = true;
  207. UpdateSchedulingLog(SchedulerID, JobID, OneScheduleTimeBegin, OneScheduleTimeEnd, OneScheduleUseSeconds,
  208. Successful, "执行失败:" + err.Message);
  209. }
  210. finally
  211. {
  212. }
  213. }
  214. protected virtual bool Init()
  215. {
  216. Successful = true;
  217. AllTimeBegin = System.DateTime.Now;
  218. AllTimeEnd = System.DateTime.Now;
  219. AllUseSeconds = 0;
  220. OneScheduleTimeBegin = System.DateTime.Now;
  221. OneScheduleTimeEnd = System.DateTime.Now;
  222. OneScheduleUseSeconds = 0;
  223. Desc = "服务【" + this._jobname + "】开始执行:";
  224. return true;
  225. }
  226. protected abstract bool MyJob();
  227. #region 修改具体服务调度表中的执行信息,记录服务调度历史记录。
  228. /// <summary>
  229. /// 修改具体服务调度表中的执行信息,记录服务调度历史记录。
  230. /// </summary>
  231. public void UpdateSchedulingLog(Guid schedulerID, string jobID, DateTime scheduleTimeBegin, DateTime scheduleTimeEnd, int UseSeconds,
  232. bool successful, string desc)
  233. {
  234. try
  235. {
  236. using (var scop = Program.AutofacContainer.BeginLifetimeScope())
  237. {
  238. var ScheduleServices = scop.Resolve<IScheduleServices>();
  239. ScheduleServices.UpdateLastRunAndLoopNum(SchedulerID);
  240. Sys_ScheduleLog log = new Sys_ScheduleLog();
  241. log.scid = schedulerID;
  242. log.timeBegin = scheduleTimeBegin;
  243. log.timeEnd = scheduleTimeEnd;
  244. log.successful = successful;
  245. log.useSeconds = UseSeconds;
  246. log.Desc = desc;
  247. ScheduleServices.InsertLog(log);
  248. }
  249. }
  250. catch (Exception ce)
  251. {
  252. LogHelper.WriteLog(LogType.ServiceLog, "操作数据库异常:" + ce.Message);
  253. }
  254. }
  255. #endregion
  256. }
  257. #endregion
  258. #region ServiceWrapper的接口
  259. namespace MyEAPServiceWrapper
  260. {
  261. /// <summary>
  262. /// ServiceWrapper的接口。
  263. /// 开启多线程,并且控制每个子服务当前最多只有一个线程在执行;多个子服务可以同时执行;防止不同的子服务彼此相互堵塞。
  264. /// </summary>
  265. public interface IServiceWrapper
  266. {
  267. bool IsDebug { get; set; }
  268. bool Execute();
  269. }
  270. }
  271. #endregion
  272. #region EAPServiceWrapper
  273. namespace MyEAPServiceWrapper
  274. {
  275. /// <summary>
  276. /// ServiceWrapper的具体子服务执行。
  277. /// 开启多线程,并且控制每个子服务当前最多只有一个线程在执行;多个子服务可以同时执行;防止不同的子服务彼此相互堵塞。
  278. /// </summary>
  279. using System;
  280. using System.Collections.Generic;
  281. using System.Linq;
  282. using System.Text;
  283. using System.Linq.Expressions;
  284. using System.Data.Entity;
  285. using System.Data.SqlClient;
  286. using System.Data;
  287. using System.Collections;
  288. public class ServiceWrapper : IServiceWrapper
  289. {
  290. #region 实现IServiceWrapper接口属性
  291. private bool _isdubug = false;
  292. DateTime _AllTimeBegin;
  293. DateTime _AllTimeEnd;
  294. int _AllUseSeconds;
  295. DateTime _OneScheduleTimeBegin;
  296. DateTime _OneScheduleTimeEnd;
  297. int _OneScheduleUseSeconds;
  298. bool _Successful;
  299. string _Desc;
  300. Guid SchedulerID = Guid.Empty;
  301. int MaxThreadSize = 1;
  302. string JobID = "";
  303. string JobName = "";
  304. string AssemName = "";
  305. string ClassName = "";
  306. /// <summary>
  307. /// 是否调试状态
  308. /// </summary>
  309. public bool IsDebug
  310. {
  311. get { return _isdubug; }
  312. set { _isdubug = value; }
  313. }
  314. /// <summary>
  315. /// 总体执行开始时间
  316. /// </summary>
  317. public DateTime AllTimeBegin
  318. {
  319. get { return _AllTimeBegin; }
  320. set { _AllTimeBegin = value; }
  321. }
  322. /// <summary>
  323. /// 总体执行结束时间
  324. /// </summary>
  325. public DateTime AllTimeEnd
  326. {
  327. get { return _AllTimeEnd; }
  328. set { _AllTimeEnd = value; }
  329. }
  330. /// <summary>
  331. /// 总体服务执行耗时秒数
  332. /// </summary>
  333. public int AllUseSeconds
  334. {
  335. get { return _AllUseSeconds; }
  336. set { _AllUseSeconds = value; }
  337. }
  338. /// <summary>
  339. /// 单个服务执行开始时间
  340. /// </summary>
  341. public DateTime OneScheduleTimeBegin
  342. {
  343. get { return _OneScheduleTimeBegin; }
  344. set { _OneScheduleTimeBegin = value; }
  345. }
  346. /// <summary>
  347. /// 单个服务执行结束时间
  348. /// </summary>
  349. public DateTime OneScheduleTimeEnd
  350. {
  351. get { return _OneScheduleTimeEnd; }
  352. set { _OneScheduleTimeEnd = value; }
  353. }
  354. /// <summary>
  355. /// 单个服务执行耗时秒数
  356. /// </summary>
  357. public int OneScheduleUseSeconds
  358. {
  359. get { return _OneScheduleUseSeconds; }
  360. set { _OneScheduleUseSeconds = value; }
  361. }
  362. /// <summary>
  363. /// 是否执行成功
  364. /// </summary>
  365. public bool Successful
  366. {
  367. get { return _Successful; }
  368. set { _Successful = value; }
  369. }
  370. /// <summary>
  371. /// 执行结果备注信息
  372. /// </summary>
  373. public string Desc
  374. {
  375. get { return _Desc; }
  376. set { _Desc = value; }
  377. }
  378. #endregion
  379. #region 子服务线程控制
  380. public class ThreadInfo
  381. {
  382. public int ManagedThreadId { get; set; }
  383. public string Name { get; set; }
  384. public bool IsAlive { get; set; }
  385. public DateTime StartTime { get; set; }
  386. }
  387. /// <summary>
  388. /// 存放当前正在执行的子服务线程信息
  389. /// </summary>
  390. static System.Collections.Generic.List<ThreadInfo> NowExecThreadsInfo = new System.Collections.Generic.List<ThreadInfo>();
  391. #endregion
  392. #region 整体控制执行服务
  393. /// <summary>
  394. /// 开启多线程,并且控制每个子服务当前最多只有一个线程在执行;多个子服务可以同时执行;防止不同的子服务彼此相互堵塞。
  395. /// </summary>
  396. /// <returns></returns>
  397. public bool Execute()
  398. {
  399. //AllTimeEnd = System.DateTime.Now;
  400. //Desc = "总体服务执行成功!";
  401. //TimeSpan ts = new TimeSpan();
  402. //ts = AllTimeEnd - AllTimeBegin;
  403. //AllUseSeconds = Convert.ToInt32(ts.TotalSeconds); //转换时间间隔为 秒
  404. //Log.WriteLog("总体服务结束;耗时:" + AllUseSeconds.ToString() + "秒; \r\n 执行结果:\r\n " + Desc);
  405. bool bResult = true;
  406. //string serverName = ConfigurationManager.AppSettings["ServerName"];
  407. var scheduleJobList = QureyScheduleJobs();
  408. foreach (var scheduleJob in scheduleJobList)
  409. {
  410. string sStartDate = scheduleJob.datebegin.ToString().Trim(); ;//20040707
  411. sStartDate = sStartDate.Substring(0, 4) + "-" + sStartDate.Substring(4, 2) + "-" + sStartDate.Substring(6, 2); //转换成2004-07-07
  412. DateTime tmpStartDate = System.Convert.ToDateTime(sStartDate);
  413. sStartDate = scheduleJob.dateend.ToString().Trim();
  414. sStartDate = sStartDate.Substring(0, 4) + "-" + sStartDate.Substring(4, 2) + "-" + sStartDate.Substring(6, 2); //转换成2004-07-07
  415. DateTime tmpEndDate = System.Convert.ToDateTime(sStartDate);
  416. int scinterval = scheduleJob.scinterval;
  417. /* D:天 H:小时 M:分钟 S:秒 */
  418. switch (scheduleJob.scmode.ToUpper())
  419. {
  420. case "D":
  421. scinterval = scheduleJob.scinterval * (60 * 60 * 24);
  422. //对于周期跨天的,要防止累计偏差而出现跳天现象(空出2分钟的柔韧空间值)。
  423. scinterval = scinterval - 120;
  424. break;
  425. case "H":
  426. scinterval = scheduleJob.scinterval * (60 * 60);
  427. break;
  428. case "M":
  429. scinterval = scheduleJob.scinterval * (60);
  430. break;
  431. //默认秒
  432. default:
  433. break;
  434. }
  435. this.SchedulerID = scheduleJob.scid;
  436. this.MaxThreadSize = 1;
  437. this.JobID = scheduleJob.Sys_ScheduleJob.jobid;
  438. this.JobName = scheduleJob.Sys_ScheduleJob.jobname;
  439. this.AssemName = scheduleJob.Sys_ScheduleJob.assemname;
  440. this.ClassName = scheduleJob.Sys_ScheduleJob.classname;
  441. //检查该项服务是否到了下次运行时间,如是则运行。
  442. if (this.CheckJobAllowRun(tmpStartDate, tmpEndDate, scinterval, scheduleJob.lastrun,
  443. scheduleJob.timefrom, scheduleJob.timeto))
  444. {
  445. //this.JobRun(tmpSchedulerID, tmpJobID,tmpServerName, tmpJobName, tmpAssemName, tmpClassName);
  446. System.Threading.Thread thread = new System.Threading.Thread(JobRun);
  447. thread.Start();
  448. //防止循环后再次获取数据库进行判断,而当前的验证还没有加入线程属性导致可能多1个子线程服务在运行;需要停顿一下。
  449. System.Threading.Thread.Sleep(3000);
  450. }
  451. }
  452. return bResult;
  453. }
  454. #endregion
  455. #region 检查是允许运行
  456. /// <summary>
  457. /// 检查任务是否允许运行
  458. /// </summary>
  459. /// <param name="StartDate"></param>
  460. /// <param name="EndDate"></param>
  461. /// <param name="SecondTiple"></param>
  462. /// <param name="LastUpdate"></param>
  463. /// <returns></returns>
  464. private bool CheckJobAllowRun(DateTime StartDate, DateTime EndDate, int SecondTiple, DateTime? LastUpdate, string StartTime, string EndTime)
  465. {
  466. DateTime nowday = new System.DateTime(System.DateTime.Now.Year, System.DateTime.Now.Month, System.DateTime.Now.Day);
  467. StartDate = new DateTime(StartDate.Year, StartDate.Month, StartDate.Day);
  468. EndDate = new DateTime(EndDate.Year, EndDate.Month, EndDate.Day);
  469. if ((nowday >= StartDate) && (nowday <= EndDate))
  470. {
  471. if (InRunTimeRange(StartTime, EndTime))
  472. {
  473. if ((ReturnSecondFromDate(System.DateTime.Now) - ReturnSecondFromDate(LastUpdate)) >= SecondTiple)
  474. return true;
  475. else
  476. return false;
  477. }
  478. else
  479. return false;
  480. }
  481. else
  482. {
  483. return false;
  484. }
  485. }
  486. /// <summary>
  487. /// 把日期转化为秒
  488. /// </summary>
  489. /// <param name="datetime1"></param>
  490. /// <returns></returns>
  491. private long ReturnSecondFromDate(DateTime? datetime1)
  492. {
  493. if (!datetime1.HasValue) return 0;
  494. return (long)((datetime1.Value.Year * 365 + datetime1.Value.DayOfYear) * 24 * 60 + datetime1.Value.Hour * 60 + datetime1.Value.Minute) * 60 + datetime1.Value.Second;
  495. }
  496. private bool InRunTimeRange(string StartTime, string EndTime)
  497. {
  498. int mHour = System.DateTime.Now.Hour;
  499. int mMinute = System.DateTime.Now.Minute;
  500. int mSecond = System.DateTime.Now.Second;
  501. string[] splStartTime = StartTime.Split(':');
  502. int mStartHour = Convert.ToInt32(splStartTime[0]);
  503. int mStartMinute = Convert.ToInt32(splStartTime[1]);
  504. int mStartSecond = Convert.ToInt32(splStartTime[2]);
  505. string[] splEndTime = EndTime.Split(':');
  506. int mEndHour = Convert.ToInt32(splEndTime[0]);
  507. int mEndMinute = Convert.ToInt32(splEndTime[1]);
  508. int mEndSecond = Convert.ToInt32(splEndTime[2]);
  509. int mCurrent = mHour * 60 * 60 + mMinute * 60 + mSecond;
  510. int mStart = mStartHour * 60 * 60 + mStartMinute * 60 + mStartSecond;
  511. int mEnd = mEndHour * 60 * 60 + mEndMinute * 60 + mEndSecond;
  512. if (mCurrent >= mStart && mCurrent <= mEnd)
  513. return true;
  514. return false;
  515. }
  516. #endregion
  517. #region 运行具体某个Job
  518. /// <summary>
  519. /// 运行服务工作,不返回错误以免将其他的工作也停止
  520. /// private void JobRun(string schedulerID, string jobID,string serverName, string jobName, string assemName, string className)
  521. /// </summary>
  522. /// <param name="SqlConn"></param>
  523. /// <param name="JobName"></param>
  524. /// <param name="ClassName"></param>
  525. private void JobRun()
  526. {
  527. Guid schedulerID = SchedulerID;
  528. int maxThreadSize = MaxThreadSize;
  529. string jobID = JobID;
  530. string jobName = JobName;
  531. string assemName = AssemName;
  532. string className = ClassName;
  533. bool thisTimeNeedDo = true;
  534. ThreadInfo threadInfo = new ThreadInfo();
  535. try
  536. {
  537. //判断检查该项服务对应的线程是否在运行。
  538. System.Threading.Thread currentSubServiceThread = System.Threading.Thread.CurrentThread;
  539. currentSubServiceThread.Name = className;
  540. //启动时间在6个钟内的线程才有效
  541. var currentSubServices = (from a in NowExecThreadsInfo
  542. where a.Name == className
  543. && a.StartTime > DateTime.Now.AddHours(-6)
  544. select new ThreadInfo
  545. {
  546. ManagedThreadId = a.ManagedThreadId,
  547. Name = a.Name,
  548. IsAlive = a.IsAlive,
  549. StartTime = a.StartTime
  550. }
  551. ).ToList();
  552. int currentSubServiceNum = currentSubServices.Count;
  553. LogHelper.WriteLog(LogType.ServiceLog, "当前【" + className + "】有个(" + currentSubServiceNum.ToString() + ")线程在执行。最大线程数:" + maxThreadSize.ToString() + ";时间:" + DateTime.Now.ToString());
  554. if (currentSubServiceNum >= maxThreadSize)
  555. {
  556. thisTimeNeedDo = false;
  557. LogHelper.WriteLog(LogType.ServiceLog, "当前【" + className + "】正已经有个(" + currentSubServiceNum.ToString() + ")线程在执行,本次跳过激发执行。" + DateTime.Now.ToString());
  558. }
  559. if (thisTimeNeedDo)
  560. {
  561. //加线程子服务信息
  562. threadInfo.ManagedThreadId = currentSubServiceThread.ManagedThreadId;
  563. threadInfo.Name = currentSubServiceThread.Name;
  564. threadInfo.IsAlive = currentSubServiceThread.IsAlive;
  565. threadInfo.StartTime = DateTime.Now;
  566. NowExecThreadsInfo.Add(threadInfo);
  567. string fullClassName = "";
  568. Assembly assem = Assembly.Load(assemName);
  569. if (className.IndexOf(".") > 0)
  570. {
  571. fullClassName = className;
  572. }
  573. else
  574. {
  575. fullClassName = assemName + "." + className;
  576. }
  577. //Log.WriteLog("TestHere01 " + fullClassName);
  578. Type classType = assem.GetType(fullClassName);
  579. IServiceJob fss = (IServiceJob)System.Activator.CreateInstance(classType);
  580. fss.SchedulerID = schedulerID;
  581. fss.JobID = jobID;
  582. fss.JobName = jobName;
  583. fss.IsDebug = this._isdubug;
  584. fss.CertFile = ConfigurationManager.AppSettings["CertFile"];
  585. fss.ExecuteJob(); //运行Job
  586. }
  587. }
  588. catch (Exception ce)
  589. {
  590. LogHelper.WriteLog(LogType.ServiceLog, "服务【" + jobName + "】执行异常:" + ce.Message);
  591. }
  592. finally
  593. {
  594. if (thisTimeNeedDo)
  595. {
  596. //处理完毕后清除线程子服务信息
  597. NowExecThreadsInfo.Remove(threadInfo);
  598. }
  599. }
  600. }
  601. #endregion
  602. #region 获取总体服务信息;然后处理那些需要执行的各个具体服务。
  603. /// <summary>
  604. /// 获取总体服务信息;然后处理那些需要执行的各个具体服务。
  605. /// </summary>
  606. public List<Sys_Scheduling> QureyScheduleJobs()
  607. {
  608. try
  609. {
  610. using (var scop = Program.AutofacContainer.BeginLifetimeScope())
  611. {
  612. var ScheduleServices = scop.Resolve<IScheduleServices>();
  613. return ScheduleServices.GetJobList();
  614. }
  615. }
  616. catch (Exception ce)
  617. {
  618. LogHelper.WriteLog(LogType.ServiceLog, "操作数据库异常:" + ce.Message);
  619. }
  620. finally
  621. {
  622. }
  623. return new List<Sys_Scheduling>();
  624. }
  625. #endregion
  626. }
  627. #endregion
  628. }
  629. }