forked from ARSBlue/ToolBox-4-Iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventQueue.cls
221 lines (192 loc) · 7.27 KB
/
EventQueue.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
Include (arsblue.OS, arsblue.Status)
/// This class defines an event queue.
/// An event queue is reponsible for a list of event listeners to handle them automatically like non-static listeners.
///
/// ARSBlue ToolBox-4-Iris
/// Copyright © 2019 ARS Blue GmbH
/// http://www.ars-blue.at
Class arsblue.event.EventQueue Extends (%Persistent, arsblue.event.SystemEventAdapter)
{
/// The name of the event queue.
Property Name As %String [ Required ];
/// The event queue run flag.
/// If this flag is set to true, the event queue starts immediately event listening until the flag is set to false.
Property EventRun As %Boolean [ InitialExpression = {$$$NO} ];
/// The event log level.
/// An event log level of INFO contains also WARNINGs and ERRORs, an event log level of WARNING contains also ERRORs,
/// an event log level of ERROR (default) contains only ERRORs, an event log level of NOLOG does not log anything.
Property EventLogLevel As %EnumString(VALUELIST = ",NOLOG,INFO,WARNING,ERROR") [ InitialExpression = "ERROR" ];
/// The event hold on flag.
/// An event hold on flag of WARNING stopps the event queue on WARNINGs and ERRORs (the last event will be retried on next start).
/// An event hold on flag of ERROR (default) stopps the event queue only on ERRORs (the last event will be retried on next start).
/// An event hold on flag of NOHOLD does not stopp at any case (fire and forget).
Property EventHoldOn As %EnumString(VALUELIST = ",NOHOLD,WARNING,ERROR") [ InitialExpression = "ERROR" ];
/// The amount of events processed per lookup (defaults to 10).
/// NOTE: you need to restart the event queue for changes to take place!
Property EventAmountPerLookup As %Integer [ InitialExpression = 10, Required ];
/// The event queue name index.
Index Name On Name [ Unique ];
/// The event queue run index.
Index EventRun On EventRun [ Type = bitmap ];
/// This expression delivers the event queue resource.
Method GetEventQueueResource() As %String
{
quit $$$NspDest(..%ClassName(1))_"("""_..%Id()_""")"
}
/// This trigger is responsible for starting and stopping of the event listener
Trigger EventQueueAfter [ Event = INSERT/UPDATE/DELETE, Foreach = row/object, Time = AFTER ]
{
// on new event queue: check to create a system event for all event queues for startup and shutdown
if (({%%OPERATION}="INSERT") && ('$IsObject(##class(arsblue.event.SystemEvent).GetEventListener({%%CLASSNAMEQ},$$$NO,,"SYSTEM"))))
{
do ##class(arsblue.event.SystemEvent).AddEventListener(,{%%CLASSNAMEQ},$$$NO,,"SYSTEM")
}
if ($CASE({%%OPERATION},"INSERT":$$$YES,"UPDATE":$$$YES,:$$$NO))
{
if ({EventRun*O}'={EventRun*N})
{
do:({EventRun*N}) ..Start({Name*N})
do:('{EventRun*N}) ..Stop({Name*N})
}
}
else
{
do $System.Event.Delete($$$NspDest({%%CLASSNAMEQ})_"("""_{ID}_""")")
}
}
/// This method runs an event queue by name.
Method Run() As %Status [ Private ]
{
$$$QuitIf('$System.Event.Create(..GetEventQueueResource()),"cannot start already running event queue "_Name)
kill sql
set sql($I(sql))="select top "_..EventAmountPerLookup_" e.ID as ""EventId"""
set sql($I(sql))=" from arsblue_event.EventQueue eq"
set sql($I(sql))=" right join arsblue_event.EventListener el on eq.ID=el.EventQueue"
set sql($I(sql))=" right join arsblue_event.Event e on el.ID=e.EventListener"
set sql($I(sql))=" where eq.ID=?"
set sql($I(sql))=" order by e.ID"
set stmt=##class(%SQL.Statement).%New()
$$$QuitOnError(stmt.%Prepare(.sql))
set eventQueueId=..%Id()
while ($System.Event.Defined(..GetEventQueueResource()))
{
hang 0.1 // give the others CPU time
do ..%LoadData(eventQueueId) // reload data
$$$ContinueOnSQLError(rs,stmt.%Execute(eventQueueId))
while (rs.%Next(.status))
{
$$$ContinueOnError(status)
set event=$$$NULLOREF
set eventId=rs.EventId
#dim status as %Status=$$$OK
try
{
set event=##class(arsblue.event.Event).%OpenId(eventId,,.status) $$$Throw(status)
set status=event.FireEvent()
}
catch (exc)
{
set status=exc.AsStatus()
}
do ##class(arsblue.event.EventQueueLog).Log($this,event,status)
if ($$$ISERR(status) && (($$$ISWARNING(status) && (..EventHoldOn="WARNING")) || ('$$$ISWARNING(status) && (..EventHoldOn="ERROR"))))
{
do:($IsObject(event)) event.RestoreEvent()
do ..Stop(..Name)
quit
}
kill event
}
}
quit $$$OK
}
/// This method starts an event queue by name.
/// <ul>
/// <li>Name...the event queue name.</li>
/// <li>ServerMode...true (default) if the event queue shall run as separate process in background, false otherwise.</li>
/// </ul>
/// Returns status OK if successfully started/running event queue, any other status signals failure!
ClassMethod Start(Name As %String = "", ServerMode As %Boolean = {$$$YES}) As %Status
{
set eventQueue=##class(arsblue.event.EventQueue).NameOpen(Name,0)
$$$QuitIf('$IsObject(eventQueue),"cannot start unknown event queue "_Name)
if (ServerMode)
{
job ..Start(Name,$$$NO)
quit $$$OK
}
else
{
quit eventQueue.Run()
}
}
/// This method stops an event queue by name.
/// <ul>
/// <li>Name...the event queue name.</li>
/// </ul>
/// Returns status OK if successfully stopped event queue, any other status signals failure!
ClassMethod Stop(Name As %String = "") As %Status
{
set eventQueue=##class(arsblue.event.EventQueue).NameOpen(Name,0)
$$$QuitIf('$IsObject(eventQueue),"cannot stopp unknown event queue "_Name)
$$$QuitIf('$System.Event.Delete(eventQueue.GetEventQueueResource()),"cannot shutdown already stopped event queue "_Name)
quit $$$OK
}
/// This method will be called when a system event was fired.
/// <ul>
/// <li>Event...the system event.</li>
/// </ul>
/// Returns status OK if successfully processed event, any other status signals warning or failure (only relevant for event queue)!
ClassMethod OnSystemEvent(Event As arsblue.event.SystemEvent) As %Status
{
set stmt=##class(%SQL.Statement).%New()
$$$QuitOnError(stmt.%Prepare("select Name, EventRun from arsblue_event.EventQueue"))
$$$QuitOnSQLError(rs,stmt.%Execute())
while (rs.%Next(.status))
{
$$$ContinueOnError(status)
set Name=rs.Name,EventRun=rs.EventRun
if (Event.Action = 1) // startup
{
set EventRun=$G(^arsblue.event.EventQueueI("$SystemEvent",Name),EventRun)
kill ^arsblue.event.EventQueueI("$SystemEvent",Name)
do:(EventRun) ..Start(Name)
}
else // 2 = shutdown
{
set ^arsblue.event.EventQueueI("$SystemEvent",Name)=EventRun
do:(EventRun) ..Stop(Name)
}
}
quit $$$OK
}
Storage Default
{
<Data name="EventQueueDefaultData">
<Value name="1">
<Value>%%CLASSNAME</Value>
</Value>
<Value name="2">
<Value>Name</Value>
</Value>
<Value name="3">
<Value>EventRun</Value>
</Value>
<Value name="4">
<Value>EventLogLevel</Value>
</Value>
<Value name="5">
<Value>EventHoldOn</Value>
</Value>
<Value name="6">
<Value>EventAmountPerLookup</Value>
</Value>
</Data>
<DataLocation>^arsblue.event.EventQueueD</DataLocation>
<DefaultData>EventQueueDefaultData</DefaultData>
<IdLocation>^arsblue.event.EventQueueD</IdLocation>
<IndexLocation>^arsblue.event.EventQueueI</IndexLocation>
<StreamLocation>^arsblue.event.EventQueueS</StreamLocation>
<Type>%Storage.Persistent</Type>
}
}