@@ -50,9 +50,15 @@ func Run(ctx context.Context, opt *options.ServerOption) error {
50
50
config .QPS = 100.0
51
51
config .Burst = 200.0
52
52
53
+ g , gCtx := errgroup .WithContext (ctx )
53
54
54
- // Start the health and metrics servers
55
- err = startHealthAndMetricsServers (ctx , opt )
55
+ // metrics server
56
+ metricsServer , err := NewServer (opt .MetricsListenPort , "/metrics" , metrics .PrometheusHandler ())
57
+ if err != nil {
58
+ return err
59
+ }
60
+
61
+ healthServer , err := NewServer (opt .HealthProbeListenPort , "/healthz" , healthHandler ())
56
62
if err != nil {
57
63
return err
58
64
}
@@ -63,54 +69,32 @@ func Run(ctx context.Context, opt *options.ServerOption) error {
63
69
return fmt .Errorf ("failed to create a job controller" )
64
70
}
65
71
66
- // Run the job controller in a goroutine and wait for it to exit
67
- wg := sync.WaitGroup {}
72
+ wg := & sync.WaitGroup {}
68
73
wg .Add (1 )
69
- go func () {
74
+ g . Go ( func () error {
70
75
defer wg .Done ()
71
- jobctrl .Run (ctx .Done ())
72
- }()
73
-
74
- wg .Wait ()
75
-
76
-
77
- return nil
78
- }
79
-
80
- func healthHandler () http.Handler {
81
- healthHandler := http .NewServeMux ()
82
- healthHandler .Handle ("/healthz" , & health.Handler {})
83
- return healthHandler
84
- }
85
-
86
- // Starts the health probe listener
87
- func startHealthAndMetricsServers (ctx context.Context , opt * options.ServerOption ) error {
88
- g , ctx := errgroup .WithContext (ctx )
89
-
90
- // metrics server
91
- metricsServer , err := NewServer (opt .MetricsListenPort , "/metrics" , metrics .PrometheusHandler ())
92
- if err != nil {
93
- return err
94
- }
95
-
96
- healthServer , err := NewServer (opt .HealthProbeListenPort , "/healthz" , healthHandler ())
97
- if err != nil {
98
- return err
99
- }
76
+ jobctrl .Run (gCtx .Done ())
77
+ return nil
78
+ })
100
79
101
80
g .Go (metricsServer .Start )
102
81
g .Go (healthServer .Start )
103
82
104
83
g .Go (func () error {
105
- <- ctx . Done ()
84
+ wg . Wait ()
106
85
return metricsServer .Shutdown ()
107
86
})
108
87
109
88
g .Go (func () error {
110
- <- ctx . Done ()
89
+ wg . Wait ()
111
90
return healthServer .Shutdown ()
112
91
})
113
92
114
- return g .Wait ()
93
+ return g .Wait ()
115
94
}
116
95
96
+ func healthHandler () http.Handler {
97
+ healthHandler := http .NewServeMux ()
98
+ healthHandler .Handle ("/healthz" , & health.Handler {})
99
+ return healthHandler
100
+ }
0 commit comments