@@ -73,9 +73,15 @@ func Run(ctx context.Context, opt *options.ServerOption) error {
73
73
return fmt .Errorf ("failed to create a job controller" )
74
74
}
75
75
76
+ g , gCtx := errgroup .WithContext (ctx )
76
77
77
- // Start the health and metrics servers
78
- err = startHealthAndMetricsServers (ctx , opt )
78
+ // metrics server
79
+ metricsServer , err := NewServer (opt .MetricsListenPort , "/metrics" , metrics .PrometheusHandler ())
80
+ if err != nil {
81
+ return err
82
+ }
83
+
84
+ healthServer , err := NewServer (opt .HealthProbeListenPort , "/healthz" , healthHandler ())
79
85
if err != nil {
80
86
return err
81
87
}
@@ -86,54 +92,32 @@ func Run(ctx context.Context, opt *options.ServerOption) error {
86
92
return fmt .Errorf ("failed to create a job controller" )
87
93
}
88
94
89
- // Run the job controller in a goroutine and wait for it to exit
90
- wg := sync.WaitGroup {}
95
+ wg := & sync.WaitGroup {}
91
96
wg .Add (1 )
92
- go func () {
97
+ g . Go ( func () error {
93
98
defer wg .Done ()
94
- jobctrl .Run (ctx .Done ())
95
- }()
96
-
97
- wg .Wait ()
98
-
99
-
100
- return nil
101
- }
102
-
103
- func healthHandler () http.Handler {
104
- healthHandler := http .NewServeMux ()
105
- healthHandler .Handle ("/healthz" , & health.Handler {})
106
- return healthHandler
107
- }
108
-
109
- // Starts the health probe listener
110
- func startHealthAndMetricsServers (ctx context.Context , opt * options.ServerOption ) error {
111
- g , ctx := errgroup .WithContext (ctx )
112
-
113
- // metrics server
114
- metricsServer , err := NewServer (opt .MetricsListenPort , "/metrics" , metrics .PrometheusHandler ())
115
- if err != nil {
116
- return err
117
- }
118
-
119
- healthServer , err := NewServer (opt .HealthProbeListenPort , "/healthz" , healthHandler ())
120
- if err != nil {
121
- return err
122
- }
99
+ jobctrl .Run (gCtx .Done ())
100
+ return nil
101
+ })
123
102
124
103
g .Go (metricsServer .Start )
125
104
g .Go (healthServer .Start )
126
105
127
106
g .Go (func () error {
128
- <- ctx . Done ()
107
+ wg . Wait ()
129
108
return metricsServer .Shutdown ()
130
109
})
131
110
132
111
g .Go (func () error {
133
- <- ctx . Done ()
112
+ wg . Wait ()
134
113
return healthServer .Shutdown ()
135
114
})
136
115
137
- return g .Wait ()
116
+ return g .Wait ()
138
117
}
139
118
119
+ func healthHandler () http.Handler {
120
+ healthHandler := http .NewServeMux ()
121
+ healthHandler .Handle ("/healthz" , & health.Handler {})
122
+ return healthHandler
123
+ }
0 commit comments