JobDispatcher.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. using Aitex.Core.Common;
  2. using Aitex.Core.Util;
  3. using Aitex.Sorter.Common;
  4. using MECF.Framework.Common.DBCore;
  5. using MECF.Framework.Common.Jobs;
  6. using MECF.Framework.Common.Routine;
  7. using MECF.Framework.Common.SubstrateTrackings;
  8. using CyberX8_Core;
  9. using CyberX8_RT.Modules;
  10. using CyberX8_RT.Schedulers;
  11. using SecsGem.Core.ItemModel;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Diagnostics;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using Aitex.Core.RT.Log;
  19. using MECF.Framework.Common.RecipeCenter;
  20. using MECF.Framework.Common.WaferHolder;
  21. namespace CyberX8_RT.Dispatch
  22. {
  23. public class JobDispatcher : ICycle
  24. {
  25. #region 内部变量
  26. /// <summary>
  27. /// 状态
  28. /// </summary>
  29. private RState _cycleState = RState.Init;
  30. /// <summary>
  31. /// Cycle计时器
  32. /// </summary>
  33. private Stopwatch _cycleWatch = new Stopwatch();
  34. #endregion
  35. /// <summary>
  36. /// 是否存在Job
  37. /// </summary>
  38. public bool HasJobRunning => JobProcesser.Instance.ControlJobInfos.Count > 0;
  39. /// <summary>
  40. /// 状态
  41. /// </summary>
  42. public RState CycleState => _cycleState;
  43. /// <summary>
  44. /// 构造函数
  45. /// </summary>
  46. public JobDispatcher()
  47. {
  48. SchedulerManager.Instance.Initialize();
  49. CycleManager.Instance.Initialize();
  50. }
  51. public void Abort()
  52. {
  53. }
  54. #region Job
  55. /// <summary>
  56. /// 创建任务
  57. /// </summary>
  58. /// <param name="param"></param>
  59. /// <param name="reason"></param>
  60. /// <returns></returns>
  61. public bool CreateJob(Dictionary<string, object> param, out string reason)
  62. {
  63. return JobProcesser.Instance.CreateJob(param, out reason);
  64. }
  65. /// <summary>
  66. /// 暂停所有任务
  67. /// </summary>
  68. /// <returns></returns>
  69. public bool PauseAllJobs()
  70. {
  71. foreach(var job in JobProcesser.Instance.ControlJobInfos)
  72. {
  73. bool result = JobProcesser.Instance.PauseJob(job.Name,out string reason);
  74. if(!result)
  75. {
  76. return false;
  77. }
  78. }
  79. _cycleState = RState.Paused;
  80. WaferHolderTaskManager.Instance.PauseAllTask();
  81. WaferTaskManager.Instance.PauseAllTask();
  82. return true;
  83. }
  84. /// <summary>
  85. /// 暂停Job
  86. /// </summary>
  87. /// <param name="jobName"></param>
  88. /// <param name="reason"></param>
  89. /// <returns></returns>
  90. public bool PauseJob(string jobName, out string reason)
  91. {
  92. bool result = JobProcesser.Instance.PauseJob(jobName, out reason);
  93. if(result)
  94. {
  95. if (!JobProcesser.Instance.ControlJobInfos.Exists(job => job.State == EnumControlJobState.Executing))
  96. _cycleState = RState.Paused;
  97. }
  98. return result;
  99. }
  100. /// <summary>
  101. /// 恢复所有任务
  102. /// </summary>
  103. /// <returns></returns>
  104. public bool ResumeAllJobs()
  105. {
  106. foreach (var job in JobProcesser.Instance.ControlJobInfos)
  107. {
  108. bool result = JobProcesser.Instance.ResumeJob(job.Name, out string reason);
  109. if (!result)
  110. {
  111. return false;
  112. }
  113. }
  114. WaferHolderTaskManager.Instance.ResumeAllTask();
  115. WaferTaskManager.Instance.ResumeAllTask();
  116. _cycleState = RState.Running;
  117. return true;
  118. }
  119. /// <summary>
  120. /// 恢复Job
  121. /// </summary>
  122. /// <param name="jobName"></param>
  123. /// <param name="reason"></param>
  124. /// <returns></returns>
  125. public bool ResumeJob(string jobName, out string reason)
  126. {
  127. bool result=JobProcesser.Instance.ResumeJob(jobName, out reason);
  128. if(result)
  129. {
  130. _cycleState = RState.Running;
  131. }
  132. return result;
  133. }
  134. /// <summary>
  135. /// 启动任务
  136. /// </summary>
  137. /// <param name="jobName"></param>
  138. /// <param name="reason"></param>
  139. /// <returns></returns>
  140. /// <exception cref="NotImplementedException"></exception>
  141. public bool StartJob(string jobName, out string reason)
  142. {
  143. bool result = JobProcesser.Instance.StartJob(jobName, out reason);
  144. if(result)
  145. {
  146. ControlJobInfo cj = JobProcesser.Instance.ControlJobInfos.Find(x => x.Name == jobName);
  147. if (!_cycleWatch.IsRunning)
  148. {
  149. _cycleWatch.Restart();
  150. }
  151. List<ProcessJobInfo> pjs = JobProcesser.Instance.ProcessJobInfos;
  152. foreach (ProcessJobInfo item in pjs)
  153. {
  154. foreach (var pjSlotWafer in item.SlotWafers)
  155. {
  156. WaferInfo wafer = WaferManager.Instance.GetWafer(pjSlotWafer.Item1, pjSlotWafer.Item2);
  157. wafer.ProcessJob = item;
  158. wafer.NextSequenceStep = 0;
  159. WaferDataRecorder.SetPjInfo(wafer.InnerId.ToString(), item.InnerId.ToString());
  160. }
  161. }
  162. _cycleState = RState.Running;
  163. }
  164. return result;
  165. }
  166. /// <summary>
  167. /// 停止任务
  168. /// </summary>
  169. /// <param name="jobName"></param>
  170. /// <param name="reason"></param>
  171. /// <returns></returns>
  172. public bool StopJob(string jobName, out string reason)
  173. {
  174. return JobProcesser.Instance.StopJob(jobName, out reason);
  175. }
  176. /// <summary>
  177. /// System Abort
  178. /// </summary>
  179. /// <returns></returns>
  180. public bool SystemAbort()
  181. {
  182. JobProcesser.Instance.SystemAbort();
  183. return true;
  184. }
  185. /// <summary>
  186. /// 删除所有job
  187. /// </summary>
  188. /// <returns></returns>
  189. public bool RemoveAllJob()
  190. {
  191. JobProcesser.Instance.RemoveAllJob();
  192. return true;
  193. }
  194. #endregion
  195. /// <summary>
  196. /// 检验是否所有任务完成
  197. /// </summary>
  198. /// <returns></returns>
  199. public bool CheckAllJobDone()
  200. {
  201. foreach (var cj in JobProcesser.Instance.ControlJobInfos)
  202. {
  203. if (cj.State != EnumControlJobState.Completed)
  204. return false;
  205. }
  206. bool waferTask = WaferTaskManager.Instance.CheckAllTaskComplete();
  207. if (!waferTask)
  208. {
  209. return false;
  210. }
  211. bool waferHolderTask = WaferHolderTaskManager.Instance.CheckAllTaskComplete();
  212. if (!waferHolderTask)
  213. {
  214. return false;
  215. }
  216. return true;
  217. }
  218. /// <summary>
  219. /// 检验是否存在已经完成的任务
  220. /// </summary>
  221. /// <param name="sJobName"></param>
  222. /// <returns></returns>
  223. public bool CheckJobJustDone(out string sJobName)
  224. {
  225. foreach (var cj in JobProcesser.Instance.ControlJobInfos)
  226. {
  227. if (cj.State == EnumControlJobState.Completed && !cj.BeenPosted)
  228. {
  229. sJobName = $"{cj.Module};{cj.JobWaferSize};{cj.LotName};{cj.LotWafers.Count};{cj.StartTime:T};{cj.EndTime:T}";
  230. cj.BeenPosted = true;
  231. return true;
  232. }
  233. }
  234. sJobName = "NULL";
  235. return false;
  236. }
  237. public RState CheckManualReturnWafer()
  238. {
  239. return RState.End;
  240. }
  241. public void Clear()
  242. {
  243. }
  244. public bool ManualReturnWafer(object[] objs)
  245. {
  246. return true;
  247. }
  248. /// <summary>
  249. /// 监控
  250. /// </summary>
  251. /// <returns></returns>
  252. public RState Monitor()
  253. {
  254. UpdateProcessJobStatus();
  255. UpdateControlJobStatus();
  256. CreateWaferHolderTask();
  257. RunWaferHolderTask();
  258. RunWaferTask();
  259. return _cycleState;
  260. }
  261. public RState ReturnAllWafers()
  262. {
  263. return RState.Init;
  264. }
  265. /// <summary>
  266. /// 启动
  267. /// </summary>
  268. /// <param name="objs"></param>
  269. /// <returns></returns>
  270. public RState Start(params object[] objs)
  271. {
  272. _cycleWatch.Stop();
  273. SchedulerManager.Instance.ResetSchedulerModule();
  274. CycleManager.Instance.InitializeAllLoadPortData();
  275. return RState.Running;
  276. }
  277. /// <summary>
  278. /// 更新ProcessJob状态
  279. /// </summary>
  280. private void UpdateProcessJobStatus()
  281. {
  282. foreach (var pj in JobProcesser.Instance.ProcessJobInfos)
  283. {
  284. if (CheckAllWaferReturned(pj, pj.State != EnumProcessJobState.Stopping))
  285. {
  286. pj.SetState(EnumProcessJobState.ProcessingComplete);
  287. JobDataRecorder.EndPJ(pj.InnerId.ToString(), 0, 0);
  288. JobProcesser.Instance.RemoveRecipeProcessJob(pj);
  289. }
  290. }
  291. List<ProcessJobInfo> list = JobProcesser.Instance.GetRecipeStateProcessJobs(EnumProcessJobState.Queued);
  292. foreach(var item in list)
  293. {
  294. if(SchedulerSequenceRecipeManager.Instance.ExistAvaibleProcessCell(item.SequenceRecipe, false))
  295. {
  296. ActiveProcessJob(item);
  297. }
  298. else
  299. {
  300. UpdateProcessJobCanceled(item);
  301. }
  302. }
  303. }
  304. /// <summary>
  305. /// ProcessJob状态修改为canceled
  306. /// </summary>
  307. /// <param name="pj"></param>
  308. private void UpdateProcessJobCanceled (ProcessJobInfo pj)
  309. {
  310. foreach (var slot in pj.SlotWafers)
  311. {
  312. var wafer = WaferManager.Instance.GetWafer(slot.Item1, slot.Item2);
  313. if (!wafer.IsEmpty&&wafer.ProcessState==EnumWaferProcessStatus.Idle)
  314. {
  315. wafer.ProcessState = EnumWaferProcessStatus.Canceled;
  316. LOG.WriteLog(eEvent.EV_SCHEDULER, "System", $"wafer {slot.Item1} slot {slot.Item2} state changed to canceled");
  317. }
  318. }
  319. pj.SetState(EnumProcessJobState.Canceled);
  320. JobDataRecorder.EndPJ(pj.InnerId.ToString(), 0, 0);
  321. LOG.WriteLog(eEvent.EV_SCHEDULER, "System", $"Processjob sequence {pj.SequenceRecipe.Ppid} not avaible cell,state changed to canceled");
  322. }
  323. /// <summary>
  324. /// 检验ProcessJob是否所有Wafer已经回到了LoadPort
  325. /// </summary>
  326. /// <param name="pj"></param>
  327. /// <param name="checkAllProcessed"></param>
  328. /// <returns></returns>
  329. private bool CheckAllWaferReturned(ProcessJobInfo pj, bool checkAllProcessed)
  330. {
  331. bool allWaferReturn = true;
  332. foreach (var slot in pj.SlotWafers)
  333. {
  334. var wafer = WaferManager.Instance.GetWafer(slot.Item1, slot.Item2);
  335. if (wafer.IsEmpty || (wafer.ProcessState != EnumWaferProcessStatus.Completed && checkAllProcessed))
  336. {
  337. allWaferReturn = false;
  338. break;
  339. }
  340. }
  341. return allWaferReturn;
  342. }
  343. /// <summary>
  344. /// 更新ControlJob状态
  345. /// </summary>
  346. private void UpdateControlJobStatus()
  347. {
  348. if(JobProcesser.Instance.ControlJobInfos==null||JobProcesser.Instance.ControlJobInfos.Count==0)
  349. {
  350. return;
  351. }
  352. List<ControlJobInfo> runningJobs=JobProcesser.Instance.getRunningControlJobs();
  353. foreach (ControlJobInfo controlJobInfo in runningJobs)
  354. {
  355. if (IsAllProcessJobComplete(controlJobInfo))
  356. {
  357. CycleManager.Instance.UpdateCycleCount(controlJobInfo.Module);
  358. if (CycleManager.Instance.GetLoadportCycleCount(controlJobInfo.Module) >= controlJobInfo.CycleNumber)
  359. {
  360. JobProcesser.Instance.CompleteControlJob(controlJobInfo);
  361. FaJobManager.Instance.Complete(controlJobInfo.Name);
  362. LOG.WriteLog(eEvent.EV_SCHEDULER, "System", $"{controlJobInfo.Name} complete");
  363. }
  364. else
  365. {
  366. JobProcesser.Instance.RestartControlJob(controlJobInfo);
  367. }
  368. }
  369. }
  370. if(CheckAllJobDone())
  371. {
  372. _cycleState = RState.End;
  373. }
  374. }
  375. /// <summary>
  376. /// 是否所有ProcessJob已经完成
  377. /// </summary>
  378. /// <param name="cj"></param>
  379. /// <returns></returns>
  380. private bool IsAllProcessJobComplete(ControlJobInfo cj)
  381. {
  382. foreach (var pj in JobProcesser.Instance.ProcessJobInfos)
  383. {
  384. if (pj.ControlJobName == cj.Name)
  385. {
  386. if (pj.State == EnumProcessJobState.Canceled)
  387. {
  388. if (!CheckAllWaferReturned(pj, false))
  389. {
  390. return false;
  391. }
  392. }
  393. else if (pj.State != EnumProcessJobState.ProcessingComplete)
  394. {
  395. return false;
  396. }
  397. }
  398. for (int i = 0; i < pj.SlotWafers.Count; i++)
  399. {
  400. var item = pj.SlotWafers[i];
  401. if (WaferManager.Instance.CheckHasWafer(item.Item1, item.Item2))
  402. {
  403. WaferInfo waferInfo = WaferManager.Instance.GetWafer(item.Item1, item.Item2);
  404. if(WaferTaskManager.Instance.Contains(waferInfo.WaferID))
  405. {
  406. return false;
  407. }
  408. }
  409. }
  410. }
  411. return true;
  412. }
  413. /// <summary>
  414. /// 启动ProcessJob
  415. /// </summary>
  416. /// <param name="pj"></param>
  417. /// <returns></returns>
  418. private bool ActiveProcessJob(ProcessJobInfo pj)
  419. {
  420. if (pj.State == EnumProcessJobState.Queued)
  421. {
  422. ControlJobInfo cj = JobProcesser.Instance.ControlJobInfos.Find(O => O.Name == pj.ControlJobName);
  423. if (cj == null)
  424. {
  425. return false;
  426. }
  427. CarrierInfo carrier = CarrierManager.Instance.GetCarrier(cj.Module);
  428. JobDataRecorder.StartPJ(pj.InnerId.ToString(), carrier.InnerId.ToString(), cj.InnerId.ToString(), pj.Name, cj.Module, cj.Module, pj.SlotWafers.Count);
  429. pj.SetState(EnumProcessJobState.Processing);
  430. }
  431. return true;
  432. }
  433. /// <summary>
  434. /// 创建WaferHolder任务
  435. /// </summary>
  436. private void CreateWaferHolderTask()
  437. {
  438. if (JobProcesser.Instance.ProcessJobInfos.Count == 0) { return; }
  439. //查看Loader之前是否有Wafer(出现场景LoaderTransporter取WaferHolder出现Id不匹配,Loader前面就已经存在了Wafer)
  440. WaferInfo waferInfo = WaferHolderTaskManager.Instance.GetPreLoaderHasWafer();
  441. List<ProcessJobInfo> tmpProcessJobs = new List<ProcessJobInfo>();
  442. if (waferInfo == null)
  443. {
  444. var runningProcessJobs = JobProcesser.Instance.GetRecipeStateProcessJobs(EnumProcessJobState.Processing);
  445. foreach (var job in runningProcessJobs)
  446. {
  447. if (!SchedulerSequenceRecipeManager.Instance.ExistAvaibleProcessCell(job.SequenceRecipe, false))
  448. {
  449. UpdateProcessJobCanceled(job);
  450. continue;
  451. }
  452. if (!CheckProcessJobHasUncompleteWafer(job))
  453. {
  454. continue;
  455. }
  456. List<string> chemistries = SchedulerSequenceRecipeManager.Instance.GetSequenceChemistry(job.SequenceRecipe);
  457. WaferSize waferSize = (WaferSize)job.SequenceRecipe.SubstrateSize;
  458. WaferHolderInfo waferHolderInfo = SchedulerManager.Instance.GetAvaibleBufferToLoaderWaferHolder(waferSize, job.SequenceRecipe.CrsType, chemistries);
  459. if (waferHolderInfo == null)
  460. {
  461. continue;
  462. }
  463. tmpProcessJobs.Add(job);
  464. break;
  465. }
  466. }
  467. else
  468. {
  469. if (waferInfo.ProcessJob != null)
  470. {
  471. tmpProcessJobs.Add(waferInfo.ProcessJob);
  472. }
  473. }
  474. if (tmpProcessJobs.Count != 0)
  475. {
  476. WaferHolderTaskManager.Instance.CreateWaferHolderTask(tmpProcessJobs);
  477. }
  478. else
  479. {
  480. WaferHolderTaskManager.Instance.CreateDummyWaferHolderTask();
  481. }
  482. }
  483. /// <summary>
  484. /// 检验ProcessJob是否存在未加工的Wafer
  485. /// </summary>
  486. /// <param name="pj"></param>
  487. /// <returns></returns>
  488. private bool CheckProcessJobHasUncompleteWafer(ProcessJobInfo pj)
  489. {
  490. for (int i = 0; i < pj.SlotWafers.Count; i++)
  491. {
  492. var item = pj.SlotWafers[i];
  493. if (WaferManager.Instance.CheckHasWafer(item.Item1, item.Item2))
  494. {
  495. WaferInfo waferInfo = WaferManager.Instance.GetWafer(item.Item1, item.Item2);
  496. if (waferInfo != null && waferInfo.ProcessState == EnumWaferProcessStatus.Idle)
  497. {
  498. if (!WaferTaskManager.Instance.Contains(waferInfo.WaferID))
  499. {
  500. return true;
  501. }
  502. }
  503. }
  504. }
  505. return false;
  506. }
  507. /// <summary>
  508. /// 运行WaferHolder任务
  509. /// </summary>
  510. private void RunWaferHolderTask()
  511. {
  512. WaferHolderTaskDispatcher.Instance.Analyse();
  513. }
  514. /// <summary>
  515. /// 运行Wafer任务
  516. /// </summary>
  517. private void RunWaferTask()
  518. {
  519. List<WaferTask> waferTasks = WaferTaskManager.Instance.LoadWaferTaskList();
  520. foreach(var item in waferTasks)
  521. {
  522. item.Run();
  523. }
  524. }
  525. }
  526. }