@@ -3,8 +3,6 @@ package services
33import (
44 "context"
55 "errors"
6- "fmt"
7- "sync"
86 "time"
97
108 "github.com/highcard-dev/daemon/internal/core/domain"
@@ -76,10 +74,9 @@ func (s *RuntimeSession) addQueueItem(cmd string, options coreservices.AddItemOp
7674 item = & runtimeQueueItem {doneChan : doneChan }
7775 s .queue [cmd ] = item
7876 s .setQueueStatusLocked (cmd , item , domain .ScrollLockStatusWaiting , nil )
79- taskChan := s .taskChan
8077 s .queueMu .Unlock ()
8178
82- taskChan <- cmd
79+ s . triggerRunQueue ()
8380
8481 if options .Wait {
8582 <- doneChan
@@ -127,29 +124,14 @@ func (s *RuntimeSession) HydrateFromState(statuses domain.ProcedureStatusMap) er
127124 return nil
128125}
129126
130- func (s * RuntimeSession ) Work () {
131- s .queueMu .Lock ()
132- taskChan := s .taskChan
133- taskDoneChan := s .taskDoneChan
134- shutdownChan := s .shutdownChan
135- shutdownDoneChan := s .shutdownDoneChan
136- s .queueMu .Unlock ()
137- defer close (shutdownDoneChan )
138-
139- for {
140- select {
141- case <- taskChan :
142- s .startRunQueue ()
143- case <- taskDoneChan :
144- s .startRunQueue ()
145- case <- shutdownChan :
146- s .workWg .Wait ()
147- s .queueMu .Lock ()
148- s .queue = make (map [string ]* runtimeQueueItem )
149- s .queueMu .Unlock ()
150- return
151- }
127+ func (s * RuntimeSession ) triggerRunQueue () {
128+ s .mu .Lock ()
129+ started := s .started
130+ s .mu .Unlock ()
131+ if ! started {
132+ return
152133 }
134+ s .startRunQueue ()
153135}
154136
155137func (s * RuntimeSession ) startRunQueue () {
@@ -241,15 +223,7 @@ func (s *RuntimeSession) RunQueue() {
241223 if i .doneChan != nil {
242224 close (i .doneChan )
243225 }
244-
245- s .queueMu .Lock ()
246- taskDoneChan := s .taskDoneChan
247- shutdownChan := s .shutdownChan
248- s .queueMu .Unlock ()
249- select {
250- case taskDoneChan <- struct {}{}:
251- case <- shutdownChan :
252- }
226+ s .triggerRunQueue ()
253227 }()
254228
255229 startedAt := time .Now ()
@@ -301,7 +275,6 @@ func (s *RuntimeSession) WaitUntilEmptyContext(ctx context.Context) error {
301275
302276 s .queueMu .Lock ()
303277 s .notifierChan = append (s .notifierChan , notifier )
304- shutdownChan := s .shutdownChan
305278 if ! s .hasActiveItemsLocked () {
306279 s .removeNotifierLocked (notifier )
307280 s .queueMu .Unlock ()
@@ -322,8 +295,6 @@ func (s *RuntimeSession) WaitUntilEmptyContext(ctx context.Context) error {
322295 }
323296 case <- ctx .Done ():
324297 return ctx .Err ()
325- case <- shutdownChan :
326- return fmt .Errorf ("queue manager shut down while waiting for commands" )
327298 }
328299 }
329300}
@@ -340,34 +311,32 @@ func (s *RuntimeSession) GetQueue() map[string]domain.ScrollLockStatus {
340311 return queue
341312}
342313
343- func (s * RuntimeSession ) shutdownQueueLoop () {
344- s .queueMu .Lock ()
345- shutdownChan := s .shutdownChan
346- shutdownDoneChan := s .shutdownDoneChan
347- s .queueMu .Unlock ()
314+ func (s * RuntimeSession ) stopDeploymentQueue () {
315+ s .mu .Lock ()
316+ s .started = false
317+ s .mu .Unlock ()
318+ s .drainQueueWork ()
319+ s .resetQueueState ()
320+ }
348321
349- s .shutdownOnce .Do (func () {
350- close (shutdownChan )
351- s .notify ()
352- })
322+ func (s * RuntimeSession ) drainQueueWork () {
323+ done := make (chan struct {})
324+ go func () {
325+ s .workWg .Wait ()
326+ close (done )
327+ }()
353328
354329 select {
355- case <- shutdownDoneChan :
330+ case <- done :
356331 case <- time .After (5 * time .Second ):
357- logger .Log ().Warn ("Timed out waiting for queue manager shutdown " )
332+ logger .Log ().Warn ("Timed out waiting for queue work to finish " )
358333 }
359334}
360335
361336func (s * RuntimeSession ) resetQueueState () {
362337 s .queueMu .Lock ()
363338 defer s .queueMu .Unlock ()
364339 s .queue = make (map [string ]* runtimeQueueItem )
365- s .taskChan = make (chan string , 100 )
366- s .taskDoneChan = make (chan struct {}, 1 )
367- s .shutdownChan = make (chan struct {})
368- s .shutdownDoneChan = make (chan struct {})
369- s .shutdownOnce = sync.Once {}
370- s .workWg = sync.WaitGroup {}
371340 s .notifierChan = make ([]chan []string , 0 )
372341}
373342
0 commit comments