@@ -2,10 +2,9 @@ package daemon
2
2
3
3
import (
4
4
"crypto/tls"
5
- "net/http"
6
- "sync"
7
-
8
5
"github.com/ory/kratos/schema"
6
+ "golang.org/x/sync/errgroup"
7
+ "net/http"
9
8
10
9
"github.com/ory/kratos/selfservice/flow/recovery"
11
10
@@ -50,7 +49,7 @@ type options struct {
50
49
ctx stdctx.Context
51
50
}
52
51
53
- func newOptions (ctx stdctx.Context , opts []Option ) * options {
52
+ func NewOptions (ctx stdctx.Context , opts []Option ) * options {
54
53
o := new (options )
55
54
o .ctx = ctx
56
55
for _ , f := range opts {
@@ -73,9 +72,8 @@ func WithContext(ctx stdctx.Context) Option {
73
72
}
74
73
}
75
74
76
- func ServePublic (r driver.Registry , wg * sync.WaitGroup , cmd * cobra.Command , args []string , opts ... Option ) {
77
- defer wg .Done ()
78
- modifiers := newOptions (cmd .Context (), opts )
75
+ func ServePublic (r driver.Registry , cmd * cobra.Command , args []string , opts ... Option ) error {
76
+ modifiers := NewOptions (cmd .Context (), opts )
79
77
ctx := modifiers .ctx
80
78
81
79
c := r .Config (cmd .Context ())
@@ -144,14 +142,15 @@ func ServePublic(r driver.Registry, wg *sync.WaitGroup, cmd *cobra.Command, args
144
142
}
145
143
return server .ServeTLS (listener , "" , "" )
146
144
}, server .Shutdown ); err != nil {
147
- l .Fatalf ("Failed to gracefully shutdown public httpd: %s" , err )
145
+ l .Errorf ("Failed to gracefully shutdown public httpd: %s" , err )
146
+ return err
148
147
}
149
148
l .Println ("Public httpd was shutdown gracefully" )
149
+ return nil
150
150
}
151
151
152
- func ServeAdmin (r driver.Registry , wg * sync.WaitGroup , cmd * cobra.Command , args []string , opts ... Option ) {
153
- defer wg .Done ()
154
- modifiers := newOptions (cmd .Context (), opts )
152
+ func ServeAdmin (r driver.Registry , cmd * cobra.Command , args []string , opts ... Option ) error {
153
+ modifiers := NewOptions (cmd .Context (), opts )
155
154
ctx := modifiers .ctx
156
155
157
156
c := r .Config (ctx )
@@ -206,9 +205,11 @@ func ServeAdmin(r driver.Registry, wg *sync.WaitGroup, cmd *cobra.Command, args
206
205
}
207
206
return server .ServeTLS (listener , "" , "" )
208
207
}, server .Shutdown ); err != nil {
209
- l .Fatalf ("Failed to gracefully shutdown admin httpd: %s" , err )
208
+ l .Errorf ("Failed to gracefully shutdown admin httpd: %s" , err )
209
+ return err
210
210
}
211
211
l .Println ("Admin httpd was shutdown gracefully" )
212
+ return nil
212
213
}
213
214
214
215
func sqa (ctx stdctx.Context , cmd * cobra.Command , d driver.Registry ) * metricsx.Service {
@@ -280,23 +281,34 @@ func sqa(ctx stdctx.Context, cmd *cobra.Command, d driver.Registry) *metricsx.Se
280
281
)
281
282
}
282
283
283
- func bgTasks (d driver.Registry , wg * sync.WaitGroup , cmd * cobra.Command , args []string , opts ... Option ) {
284
- defer wg .Done ()
285
- modifiers := newOptions (cmd .Context (), opts )
284
+ func bgTasks (d driver.Registry , cmd * cobra.Command , args []string , opts ... Option ) error {
285
+ modifiers := NewOptions (cmd .Context (), opts )
286
286
ctx := modifiers .ctx
287
287
288
288
if d .Config (ctx ).IsBackgroundCourierEnabled () {
289
289
go courier .Watch (ctx , d )
290
290
}
291
+ return nil
291
292
}
292
293
293
- func ServeAll (d driver.Registry , opts ... Option ) func (cmd * cobra.Command , args []string ) {
294
- return func (cmd * cobra.Command , args []string ) {
295
- var wg sync.WaitGroup
296
- wg .Add (3 )
297
- go ServePublic (d , & wg , cmd , args , opts ... )
298
- go ServeAdmin (d , & wg , cmd , args , opts ... )
299
- go bgTasks (d , & wg , cmd , args , opts ... )
300
- wg .Wait ()
294
+ func ServeAll (d driver.Registry , opts ... Option ) func (cmd * cobra.Command , args []string ) error {
295
+ return func (cmd * cobra.Command , args []string ) error {
296
+ mods := NewOptions (cmd .Context (), opts )
297
+ ctx := mods .ctx
298
+
299
+ g , ctx := errgroup .WithContext (ctx )
300
+ cmd .SetContext (ctx )
301
+ opts = append (opts , WithContext (ctx ))
302
+
303
+ g .Go (func () error {
304
+ return ServePublic (d , cmd , args , opts ... )
305
+ })
306
+ g .Go (func () error {
307
+ return ServeAdmin (d , cmd , args , opts ... )
308
+ })
309
+ g .Go (func () error {
310
+ return bgTasks (d , cmd , args , opts ... )
311
+ })
312
+ return g .Wait ()
301
313
}
302
314
}
0 commit comments