TaskMng.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Collections.Concurrent;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using Newtonsoft.Json;
  6. using Sers.Core.Module.App;
  7. using Vit.Core.Util.ConfigurationManager;
  8. using Vit.Core.Util.Threading;
  9. namespace App.Robot.Station.Logical
  10. {
  11. public class TaskMng
  12. {
  13. public static void Init()
  14. {
  15. }
  16. /// <summary>
  17. /// 主线程开启的常驻线程,用以启动api触发的任务。
  18. /// 若在api中直接调用,则会导致 ApiClient中 RpcData错乱的问题
  19. /// (RpcData通过AsyncCache保存api调用关系,故若api中直接开启线程调用api,可能会出现api中的 RpcData错乱)。
  20. /// </summary>
  21. public static readonly TaskQueue MainTask = new TaskQueue() { threadName = "Robot-MainTaskToStartTask" };
  22. public static readonly TaskMng Instance;
  23. static TaskMng()
  24. {
  25. //TaskController.MainTask
  26. SersApplication.onStart += () => MainTask.Start();
  27. SersApplication.onStop += () => MainTask.Stop();
  28. Instance = JsonFile.GetFromFile<TaskMng>(new[] { "Data", "App.Robot.json" }) ;
  29. if (null == Instance)
  30. {
  31. Instance = new TaskMng();
  32. }
  33. MainTask.AddTask(() =>
  34. {
  35. Thread.Sleep(2000);
  36. Instance.tasks.Values.Where(m => m.config.autoStart).ToList().ForEach(task => task.worker.Start());
  37. });
  38. }
  39. public void TaskMngSaveToCache()
  40. {
  41. JsonFile.SetToFile(this, new[] { "Data", "App.Robot.json" });
  42. }
  43. [JsonProperty]
  44. ConcurrentDictionary<int, TaskItem> tasks = new ConcurrentDictionary<int, TaskItem>();
  45. public bool Add(TaskConfig config)
  46. {
  47. lock (this)
  48. {
  49. var id = 1;
  50. if (tasks.Count != 0)
  51. {
  52. id = tasks.Keys.Max() + 1;
  53. }
  54. TaskItem taskItem = new TaskItem { config = config, id = id };
  55. if (!tasks.TryAdd(id, taskItem))
  56. {
  57. return false;
  58. }
  59. if (config.autoStart)
  60. {
  61. MainTask.AddTask(() =>
  62. {
  63. taskItem.worker.Start();
  64. });
  65. }
  66. TaskMngSaveToCache();
  67. return true;
  68. }
  69. }
  70. public bool Start(int id)
  71. {
  72. if (tasks.TryGetValue(id, out var taskItem))
  73. {
  74. MainTask.AddTask(() =>
  75. {
  76. taskItem.worker.Start();
  77. });
  78. return true;
  79. }
  80. return false;
  81. }
  82. public bool Stop(int id)
  83. {
  84. if (tasks.TryGetValue(id, out var task))
  85. {
  86. task.worker.Stop();
  87. return true;
  88. }
  89. return false;
  90. }
  91. public bool Remove(int id)
  92. {
  93. if (tasks.TryGetValue(id, out var task))
  94. {
  95. task.worker.Stop();
  96. tasks.TryRemove(id, out _);
  97. TaskMngSaveToCache();
  98. return true;
  99. }
  100. return false;
  101. }
  102. public List<TaskItem> GetAll()
  103. {
  104. return tasks.Values.ToList();
  105. }
  106. }
  107. }