TaskMng.cs 3.7 KB

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