@@ -33,7 +33,7 @@ type eventExecutor struct {
33
33
bThreads * sql.BackgroundThreads
34
34
list * enabledEventsList
35
35
runningEventsStatus * runningEventsStatus
36
- ctxGetterFunc func () (* sql.Context , func () error , error )
36
+ ctxGetterFunc func () (* sql.Context , error )
37
37
queryRunFunc func (ctx * sql.Context , dbName , query , username , address string ) error
38
38
stop atomic.Bool
39
39
catalog sql.Catalog
@@ -43,7 +43,7 @@ type eventExecutor struct {
43
43
44
44
// newEventExecutor returns a new eventExecutor instance with an empty enabled events list.
45
45
// The enabled events list is loaded only when the EventScheduler status is ENABLED.
46
- func newEventExecutor (bgt * sql.BackgroundThreads , ctxFunc func () (* sql.Context , func () error , error ), runQueryFunc func (ctx * sql.Context , dbName , query , username , address string ) error , period int ) * eventExecutor {
46
+ func newEventExecutor (bgt * sql.BackgroundThreads , ctxFunc func () (* sql.Context , error ), runQueryFunc func (ctx * sql.Context , dbName , query , username , address string ) error , period int ) * eventExecutor {
47
47
return & eventExecutor {
48
48
bThreads : bgt ,
49
49
list : newEnabledEventsList ([]* enabledEvent {}),
@@ -75,35 +75,73 @@ func (ee *eventExecutor) start() {
75
75
for {
76
76
time .Sleep (pollingDuration )
77
77
78
- ctx , _ , err := ee .ctxGetterFunc ()
79
- if err != nil {
80
- logrus .Errorf ("unable to create context for event executor: %s" , err )
81
- continue
82
- }
78
+ type res int
79
+ const (
80
+ res_fallthrough res = iota
81
+ res_continue
82
+ res_return
83
+ )
83
84
84
- needsToReloadEvents , err := ee .needsToReloadEvents (ctx )
85
- if err != nil {
86
- ctx .GetLogger ().Errorf ("unable to determine if events need to be reloaded: %s" , err )
87
- }
88
- if needsToReloadEvents {
89
- err := ee .loadAllEvents (ctx )
85
+ var timeNow , nextAt time.Time
86
+ var lgr * logrus.Entry
87
+
88
+ result := func () res {
89
+ ctx , err := ee .ctxGetterFunc ()
90
90
if err != nil {
91
- ctx .GetLogger ().Errorf ("unable to reload events: %s" , err )
91
+ logrus .Errorf ("unable to create context for event executor: %s" , err )
92
+ return res_continue
92
93
}
93
- }
94
+ lgr = ctx . GetLogger ()
94
95
95
- timeNow := time .Now ()
96
- if ee .stop .Load () {
97
- logrus .Trace ("Stopping eventExecutor" )
98
- return
99
- } else if ee .list .len () == 0 {
100
- continue
101
- }
96
+ defer sql .SessionEnd (ctx .Session )
97
+ sql .SessionCommandBegin (ctx .Session )
98
+ defer sql .SessionCommandEnd (ctx .Session )
102
99
103
- // safeguard list entry getting removed while in check
104
- nextAt , ok := ee .list .getNextExecutionTime ()
105
- if ! ok {
100
+ err = beginTx (ctx )
101
+ if err != nil {
102
+ lgr .Errorf ("unable to begin transaction for event executor: %s" , err )
103
+ return res_continue
104
+ }
105
+
106
+ needsToReloadEvents , err := ee .needsToReloadEvents (ctx )
107
+ if err != nil {
108
+ lgr .Errorf ("unable to determine if events need to be reloaded: %s" , err )
109
+ }
110
+ if needsToReloadEvents {
111
+ err := ee .loadAllEvents (ctx )
112
+ if err != nil {
113
+ lgr .Errorf ("unable to reload events: %s" , err )
114
+ }
115
+ }
116
+
117
+ if ee .stop .Load () {
118
+ logrus .Trace ("Stopping eventExecutor" )
119
+ return res_return
120
+ } else if ee .list .len () == 0 {
121
+ rollbackTx (ctx )
122
+ return res_continue
123
+ }
124
+
125
+ // safeguard list entry getting removed while in check
126
+ timeNow = time .Now ()
127
+ var ok bool
128
+ nextAt , ok = ee .list .getNextExecutionTime ()
129
+ if ! ok {
130
+ rollbackTx (ctx )
131
+ return res_continue
132
+ }
133
+
134
+ err = commitTx (ctx )
135
+ if err != nil {
136
+ lgr .Errorf ("unable to commit transaction for reloading events: %s" , err )
137
+ }
138
+ return res_fallthrough
139
+ }()
140
+
141
+ if result == res_continue {
106
142
continue
143
+ } else if result == res_return {
144
+ return
107
145
}
108
146
109
147
secondsUntilExecution := nextAt .Sub (timeNow ).Seconds ()
@@ -117,22 +155,33 @@ func (ee *eventExecutor) start() {
117
155
} else if secondsUntilExecution <= 0.0000001 {
118
156
curEvent := ee .list .pop ()
119
157
if curEvent != nil {
120
- ctx .GetLogger ().Debugf ("Executing event %s, seconds until execution: %f" , curEvent .name (), secondsUntilExecution )
121
- ctx , commit , err := ee .ctxGetterFunc ()
122
- if err != nil {
123
- ctx .GetLogger ().Errorf ("Received error '%s' getting ctx in event scheduler" , err )
124
- }
125
- err = ee .executeEventAndUpdateList (ctx , curEvent , timeNow )
126
- if err != nil {
127
- ctx .GetLogger ().Errorf ("Received error '%s' executing event: %s" , err , curEvent .event .Name )
128
- }
129
- err = commit ()
130
- if err != nil {
131
- ctx .GetLogger ().Errorf ("Received error '%s' executing event: %s" , err , curEvent .event .Name )
132
- }
158
+ func () {
159
+ lgr .Debugf ("Executing event %s, seconds until execution: %f" , curEvent .name (), secondsUntilExecution )
160
+ ctx , err := ee .ctxGetterFunc ()
161
+ if err != nil {
162
+ ctx .GetLogger ().Errorf ("Received error '%s' getting ctx in event scheduler" , err )
163
+ return
164
+ }
165
+ defer sql .SessionEnd (ctx .Session )
166
+ sql .SessionCommandBegin (ctx .Session )
167
+ defer sql .SessionCommandEnd (ctx .Session )
168
+ err = beginTx (ctx )
169
+ if err != nil {
170
+ ctx .GetLogger ().Errorf ("Received error '%s' beginning transaction in event scheduler" , err )
171
+ return
172
+ }
173
+ err = ee .executeEventAndUpdateList (ctx , curEvent , timeNow )
174
+ if err != nil {
175
+ ctx .GetLogger ().Errorf ("Received error '%s' executing event: %s" , err , curEvent .event .Name )
176
+ }
177
+ err = commitTx (ctx )
178
+ if err != nil {
179
+ ctx .GetLogger ().Errorf ("Received error '%s' executing event: %s" , err , curEvent .event .Name )
180
+ }
181
+ }()
133
182
}
134
183
} else {
135
- ctx . GetLogger () .Tracef ("Not executing event %s yet, seconds until execution: %f" , ee .list .peek ().name (), secondsUntilExecution )
184
+ lgr .Tracef ("Not executing event %s yet, seconds until execution: %f" , ee .list .peek ().name (), secondsUntilExecution )
136
185
}
137
186
}
138
187
}
@@ -253,11 +302,19 @@ func (ee *eventExecutor) executeEvent(event *enabledEvent) (bool, error) {
253
302
return
254
303
default :
255
304
// get a new session sql.Context for each event definition execution
256
- sqlCtx , commit , err := ee .ctxGetterFunc ()
305
+ sqlCtx , err := ee .ctxGetterFunc ()
257
306
if err != nil {
258
307
logrus .WithField ("query" , event .event .EventBody ).Errorf ("unable to get context for executed query: %v" , err )
259
308
return
260
309
}
310
+ defer sql .SessionEnd (sqlCtx .Session )
311
+ sql .SessionCommandBegin (sqlCtx .Session )
312
+ defer sql .SessionCommandEnd (sqlCtx .Session )
313
+ err = beginTx (sqlCtx )
314
+ if err != nil {
315
+ logrus .WithField ("query" , event .event .EventBody ).Errorf ("unable to begin transaction on context for executed query: %v" , err )
316
+ return
317
+ }
261
318
262
319
// Note that we pass in the full CREATE EVENT statement so that the engine can parse it
263
320
// and pull out the plan nodes for the event body, since the event body doesn't always
@@ -266,11 +323,12 @@ func (ee *eventExecutor) executeEvent(event *enabledEvent) (bool, error) {
266
323
err = ee .queryRunFunc (sqlCtx , event .edb .Name (), event .event .CreateEventStatement (), event .username , event .address )
267
324
if err != nil {
268
325
logrus .WithField ("query" , event .event .EventBody ).Errorf ("unable to execute query: %v" , err )
326
+ rollbackTx (sqlCtx )
269
327
return
270
328
}
271
329
272
330
// must commit after done using the sql.Context
273
- err = commit ( )
331
+ err = commitTx ( sqlCtx )
274
332
if err != nil {
275
333
logrus .WithField ("query" , event .event .EventBody ).Errorf ("unable to commit transaction: %v" , err )
276
334
return
@@ -290,9 +348,19 @@ func (ee *eventExecutor) reevaluateEvent(edb sql.EventDatabase, event sql.EventD
290
348
return
291
349
}
292
350
293
- ctx , commit , err := ee .ctxGetterFunc ()
351
+ ctx , err := ee .ctxGetterFunc ()
294
352
if err != nil {
295
353
ctx .GetLogger ().Errorf ("Received error '%s' getting ctx in event scheduler" , err )
354
+ return
355
+ }
356
+ defer sql .SessionEnd (ctx .Session )
357
+ sql .SessionCommandBegin (ctx .Session )
358
+ defer sql .SessionCommandEnd (ctx .Session )
359
+
360
+ err = beginTx (ctx )
361
+ if err != nil {
362
+ ctx .GetLogger ().Errorf ("Received error '%s' beginning transaction on ctx in event scheduler" , err )
363
+ return
296
364
}
297
365
298
366
newEvent , created , err := newEnabledEvent (ctx , edb , event , time .Now ())
@@ -302,7 +370,7 @@ func (ee *eventExecutor) reevaluateEvent(edb sql.EventDatabase, event sql.EventD
302
370
ee .list .add (newEvent )
303
371
}
304
372
305
- err = commit ( )
373
+ err = commitTx ( ctx )
306
374
if err != nil {
307
375
ctx .GetLogger ().Errorf ("Received error '%s' re-evaluating event to scheduler: %s" , err , event .Name )
308
376
}
0 commit comments