Skip to content

Commit 0d92fca

Browse files
committed
refactor: addition of wait groups to ensure order of shutdown and other minor edits from reviews
1 parent 19a5662 commit 0d92fca

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

cmd/kar-controllers/app/generic-server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (s *Server) Shutdown() error {
103103
if err := s.httpServer.Shutdown(shutdownCtx); err != nil {
104104
return fmt.Errorf("failed to shutdown server gracefully: %v", err)
105105
}
106-
return s.httpServer.Shutdown(shutdownCtx)
106+
return nil
107107
}
108108

109109
// newListener creates a new TCP listener bound to the given address.

cmd/kar-controllers/app/server.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"fmt"
2323
"net/http"
24+
"sync"
2425

2526
"golang.org/x/sync/errgroup"
2627
"k8s.io/client-go/rest"
@@ -79,8 +80,22 @@ func Run(ctx context.Context, opt *options.ServerOption) error {
7980
return err
8081
}
8182

82-
// Start the job controller
83-
go jobctrl.Run(ctx.Done())
83+
// Create the job controller
84+
jobctrl := queuejob.NewJobController(config, opt)
85+
if jobctrl == nil {
86+
return fmt.Errorf("failed to create a job controller")
87+
}
88+
89+
// Run the job controller in a goroutine and wait for it to exit
90+
wg := sync.WaitGroup{}
91+
wg.Add(1)
92+
go func() {
93+
defer wg.Done()
94+
jobctrl.Run(ctx.Done())
95+
}()
96+
97+
wg.Wait()
98+
8499

85100
return nil
86101
}

cmd/kar-controllers/main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,4 @@ func main() {
4343
os.Exit(1)
4444
}
4545

46-
fmt.Println("Shutting down gracefully")
47-
4846
}

0 commit comments

Comments
 (0)