|
@@ -0,0 +1,136 @@
|
|
1
|
+`Coravel`不仅可以执行后台定时程序,还具备队广播事件功能。
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+引用
|
|
6
|
+
|
|
7
|
+```
|
|
8
|
+<PackageReference Include="Coravel" Version="3.1.0" />
|
|
9
|
+```
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+# 后台定时程序
|
|
14
|
+
|
|
15
|
+如果执行后台定时程序,需要定义一个`IInvocable`对象
|
|
16
|
+
|
|
17
|
+```
|
|
18
|
+ public class RegisterDevices : IInvocable
|
|
19
|
+ {
|
|
20
|
+ ublic async System.Threading.Tasks.Task Invoke()
|
|
21
|
+ {
|
|
22
|
+
|
|
23
|
+ }
|
|
24
|
+ }
|
|
25
|
+```
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+注入到容器中
|
|
30
|
+
|
|
31
|
+```
|
|
32
|
+services.AddScheduler();//计划
|
|
33
|
+services.AddQueue();//队列
|
|
34
|
+services.AddEvents();//广播
|
|
35
|
+
|
|
36
|
+services.AddTransient<RegisterDevices>();//注册IInvocable
|
|
37
|
+```
|
|
38
|
+
|
|
39
|
+在请求管道中配置 频率等行为
|
|
40
|
+
|
|
41
|
+```
|
|
42
|
+var provider = app.ApplicationServices; ;//获取IServiceProvider
|
|
43
|
+
|
|
44
|
+provider.UseScheduler(s =>
|
|
45
|
+ {
|
|
46
|
+ #region 本地相关
|
|
47
|
+
|
|
48
|
+ //更新所有模块的上传频率
|
|
49
|
+ s.OnWorker("WRegisterDevices");//让任务之间并行不会有延时影响
|
|
50
|
+ s.Schedule<RegisterDevices>()
|
|
51
|
+ .Cron("*/1 * * * *")//每1分钟运行一次
|
|
52
|
+ .PreventOverlapping("RegisterDevices");//当第一次执行不完,第二次就不执行
|
|
53
|
+ })
|
|
54
|
+
|
|
55
|
+ .OnError(exception =>
|
|
56
|
+ {
|
|
57
|
+ Console.WriteLine(exception.Message);
|
|
58
|
+ });
|
|
59
|
+```
|
|
60
|
+
|
|
61
|
+# 事件广播
|
|
62
|
+
|
|
63
|
+`Coravel`会把事件放队列中,依次执行。
|
|
64
|
+
|
|
65
|
+定义事件。
|
|
66
|
+
|
|
67
|
+```
|
|
68
|
+ public class SceneExecuted : IEvent
|
|
69
|
+ {
|
|
70
|
+ public SceneArgs SceneArgs { get; set; }
|
|
71
|
+
|
|
72
|
+ public SceneExecuted(SceneArgs sceneArgs)
|
|
73
|
+ {
|
|
74
|
+ SceneArgs = sceneArgs;
|
|
75
|
+ }
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ public class SceneArgs
|
|
79
|
+ {
|
|
80
|
+ public string SceneId { get; set; }
|
|
81
|
+ public string SceneScheduleId { get; set; }
|
|
82
|
+ public string StartTime { get; set; }
|
|
83
|
+ }
|
|
84
|
+```
|
|
85
|
+
|
|
86
|
+定义事件观察者。
|
|
87
|
+
|
|
88
|
+```
|
|
89
|
+ public class SceneExecuteListener : IListener<SceneExecuted>
|
|
90
|
+ {
|
|
91
|
+ public async System.Threading.Tasks.Task HandleAsync(SceneExecuted broadcasted)
|
|
92
|
+ {
|
|
93
|
+
|
|
94
|
+ }
|
|
95
|
+ }
|
|
96
|
+```
|
|
97
|
+
|
|
98
|
+在容器中注册
|
|
99
|
+
|
|
100
|
+```
|
|
101
|
+services.AddTransient<SceneExecuted>(); //IEvent
|
|
102
|
+services.AddTransient<SceneExecuteListener>();//IListener
|
|
103
|
+services.AddTransient<SceneArgs>();
|
|
104
|
+```
|
|
105
|
+
|
|
106
|
+在请求管道中定义事件行为
|
|
107
|
+
|
|
108
|
+```
|
|
109
|
+ //事件
|
|
110
|
+ IEventRegistration registration = provider.ConfigureEvents();
|
|
111
|
+
|
|
112
|
+//场景的执行情况上传到云端,cloudgo=1时执行
|
|
113
|
+registration
|
|
114
|
+.Register<SceneExecuted>()
|
|
115
|
+.Subscribe<SceneExecuteListener>();
|
|
116
|
+
|
|
117
|
+//队列
|
|
118
|
+provider
|
|
119
|
+.ConfigureQueue()
|
|
120
|
+//.LogQueuedTaskProgress(Services.GetService<ILogger<IQueue>>())
|
|
121
|
+.OnError(exception =>
|
|
122
|
+{
|
|
123
|
+Console.WriteLine(exception.Message);
|
|
124
|
+});
|
|
125
|
+```
|
|
126
|
+
|
|
127
|
+在`appsettings.json`中定义队列间隔
|
|
128
|
+
|
|
129
|
+```
|
|
130
|
+"Coravel": {
|
|
131
|
+ "Queue": {
|
|
132
|
+ "ConsummationDelay": 30 //队列项的等待时间
|
|
133
|
+ }
|
|
134
|
+}
|
|
135
|
+```
|
|
136
|
+
|