@@ -147,7 +147,7 @@ func (executor *commandExecutor[C, R]) loop(shutdownCtx context.Context) {
147
147
close (executor .backlogChan )
148
148
149
149
// Add all outstanding commands in the backlog or cmd channels to the backlog slice.
150
- executor .backlog = append ( executor . backlog , ReadAll ( executor . backlogChan ) ... )
150
+ executor .drainBacklogChannel ( )
151
151
executor .backlog = append (executor .backlog , ReadAll (executor .cmdChan )... )
152
152
153
153
logrus .Debug ("Returning errors for outstanding requests due to shutdown." )
@@ -171,11 +171,14 @@ func (executor *commandExecutor[C, R]) loop(shutdownCtx context.Context) {
171
171
case cmd := <- executor .cmdChan :
172
172
logrus .Debugf ("Received command." )
173
173
if ! executor .backLogCommands {
174
+ logrus .Debugf ("Executing command immediately.." )
174
175
executor .executeCommand (ctx , cmd )
175
176
} else {
177
+ logrus .Debugf ("Backlog commands set, adding command to backlog (current backlog size: %d)." , len (executor .backlog ))
176
178
executor .backlog = append (executor .backlog , cmd )
177
179
}
178
180
case cmd := <- executor .backlogChan :
181
+ logrus .Debugf ("Received backlog command (current backlog size: %d)." , len (executor .backlog ))
179
182
executor .backlog = append (executor .backlog , cmd )
180
183
case signal := <- executor .drainAndBacklogSig :
181
184
logrus .Debugf ("Received requeue signal." )
@@ -186,25 +189,30 @@ func (executor *commandExecutor[C, R]) loop(shutdownCtx context.Context) {
186
189
defer signal .Send ()
187
190
defer close (draining )
188
191
189
- logrus .Debugf ("Waiting for inflight commands to finish..." )
192
+ logrus .Debug ("Waiting for inflight commands to finish..." )
190
193
executor .inflightCmds .Wait ()
191
194
executor .errBuff .Clear ()
192
- logrus .Debugf ("Inflight commands finished, notifying listeners." )
195
+ logrus .Debug ("Inflight commands finished, notifying listeners." )
193
196
}()
194
197
case <- executor .resumeBackloggedSig .Receive ():
195
198
logrus .Debugf ("Received resume signal." )
196
199
if draining != nil {
200
+ logrus .Debug ("Waiting for drain to finish..." )
197
201
// If the draining channel is not nil and hasn't been closed then we're still draining. We need to
198
202
// delay resuming so the backlog can be written too.
199
203
if _ , read := ReadNoWait (draining ); ! read {
204
+ logrus .Debug ("delay resume signal not set, setting it." )
200
205
delayResume = draining
201
206
continue
202
207
}
208
+ logrus .Debug ("delay resume signal already set." )
203
209
}
204
210
205
211
// Handle the backlog before resuming execution.
206
212
ctx , stopCommands = executor .execBacklog (shutdownCtx )
207
213
case <- delayResume :
214
+ logrus .Debug ("Delay resume signal received, handling backlog." )
215
+
208
216
// Handle the backlog before resuming execution.
209
217
ctx , stopCommands = executor .execBacklog (shutdownCtx )
210
218
@@ -214,7 +222,17 @@ func (executor *commandExecutor[C, R]) loop(shutdownCtx context.Context) {
214
222
}
215
223
}
216
224
225
+ func (executor * commandExecutor [C , R ]) drainBacklogChannel () {
226
+ logrus .Debugf ("Backlog size: %d, adding to backlog." , len (executor .backlog ))
227
+ executor .backlog = append (executor .backlog , ReadAll (executor .backlogChan )... )
228
+ }
229
+
217
230
func (executor * commandExecutor [C , R ]) execBacklog (shutdownCtx context.Context ) (context.Context , func ()) {
231
+ // Just in case there's anything left on the backlog channel ensure it's drained off and added to the backlog slice.
232
+ if len (executor .backlog ) > 0 {
233
+ executor .drainBacklogChannel ()
234
+ }
235
+
218
236
ctx , stopCommands := context .WithCancel (shutdownCtx )
219
237
for _ , cmd := range executor .backlog {
220
238
executor .executeCommand (ctx , cmd )
0 commit comments