鼎鼎知识库
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

01AntJob的运作原理.md 3.9KB

3 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # 表结构
  2. ```
  3. //每一个应用实例就是一个App
  4. public class App
  5. {
  6. public int ID {get;set;}
  7. publi string DisplayName {get;set;}
  8. public string Secret {get;set;}
  9. }
  10. //描述App的工作状态
  11. public class AppOnline
  12. {
  13. public int ID {get;set;}
  14. public string Instantce {get;set;}
  15. public string Client {get;set;}
  16. public string Name {get;set;}
  17. public int ProcessId {get;set;}
  18. public string Version {get;set;}
  19. public string Server {get;set;}
  20. public int Tasks {get;set;}
  21. public int Total {get;set;}
  22. }
  23. //作业
  24. public class Job
  25. {
  26. public int ID {get;set;}
  27. public int AppID {get;set;}
  28. public string Name {get;set;}
  29. public string ClassName {get;set;}
  30. public string DisplayName {get;set;}
  31. public int Mode {get;set;}
  32. public string Topic {get;set;}
  33. public int MessageCount {get;set;}
  34. public DateTime Start {get;set;}
  35. public DateTime End {get;set;}
  36. public int Step {get;set;}
  37. public int BatchSize {get;set;}
  38. public int Offset {get;set;}
  39. }
  40. //作业的任务
  41. public class JobTask
  42. {
  43. public int ID {get;set;}
  44. public int AppID {get;set;}
  45. public int JobID {get;set;}
  46. }
  47. ```
  48. # 应用、调度中心、Web控制台
  49. ![](F:\SourceCodes\DDWiki\专题\后端\Starbust\AntJob.svg)
  50. - 计算应用:理解成一个应用实例,可能是本地开发环境的控制台程序,可能是服务器上的一个服务
  51. - 调度中心:调度所有的作业,需要被持久化
  52. - Web控制台:展示调度中心的持久化内容
  53. # 最简单的例子
  54. 应用要做的事情是把作业交给调度器开始执行。
  55. ```
  56. var set = AntSetting.Current;
  57. // 实例化调度器
  58. var sc = new Scheduler();
  59. // 使用分布式调度引擎替换默认的本地文件调度
  60. sc.Provider = new NetworkJobProvider
  61. {
  62. Server = set.Server,
  63. AppID = set.AppID,
  64. Secret = set.Secret,
  65. };
  66. // 添加作业处理器
  67. sc.Handlers.Add(new HelloJob());
  68. sc.Handlers.Add(new BuildPatient());
  69. sc.Handlers.Add(new BuildWill());
  70. // 启动调度引擎,调度器内部多线程处理
  71. sc.Start();
  72. ```
  73. 所有的作业都是`Handler`类型。
  74. ```
  75. [DisplayName("定时欢迎")]
  76. [Description("简单的定时任务")]
  77. internal class HelloJob : Handler
  78. {
  79. public HelloJob()
  80. {
  81. // 今天零点开始,每10秒一次
  82. var job = Job;
  83. job.Start = DateTime.Today;
  84. job.Step = 10;
  85. }
  86. protected override Int32 Execute(JobContext ctx)
  87. {
  88. // 当前任务时间
  89. var time = ctx.Task.Start;
  90. WriteLog("新生命蚂蚁调度系统!当前任务时间:{0}", time);
  91. // 成功处理数据量
  92. return 1;
  93. }
  94. }
  95. ```
  96. ![antjob1](F:\SourceCodes\DDWiki\专题\后端\Starbust\antjob1.png)
  97. # `Scheduler.Start()`做了什么?
  98. ```
  99. public class Scheduler
  100. {
  101. public List<Handler> Handlers { get; } = new List<Handler>();
  102. public IJobProvider Provider { get; set; }//作业提供者
  103. public void Start()
  104. {
  105. var prv = Provider;
  106. prv.Schedule = this;
  107. prv.Start();//作业提供者开始
  108. var jobs = prv.GetJobs();
  109. var hs = Handlers;
  110. foreach (var handler in hs)
  111. {
  112. handler.Schedule = this;
  113. handler.Provider = prv;
  114. var job = jobs.FirstOrDefault(e => e.Name == handler.Name);
  115. if(job!=null && job.Mode ==0) job.Mode = handler.Mode;
  116. handler.Job = job;
  117. handler.Start();//作业处理器开始
  118. }
  119. if(Period > 0) _timer = new TimeX(Loop, null, Period * 1000, "Job"){Async = true};
  120. }
  121. }
  122. ```
  123. `Scheduler`中维护着`Handler`的集合以及`IJobProvider`。`IJobProvider`需要知道`Scheduler`。`Handler`需要知道`Scheduler`和`IJobProvider`以及`IJob`。`Scheduler`让`IJobProvider`和每个`Handler`开始工作。更加重要的是,在实例化`TimeX`的过程中开始了任务调度,即`Scheudler`中的`Process()`。
  124. `Pr`