Skip to content

Commit 02f7e9c

Browse files
committed
fix: use errors instead of fatal for serve cmd
1 parent e5f1311 commit 02f7e9c

File tree

2 files changed

+37
-25
lines changed

2 files changed

+37
-25
lines changed

cmd/daemon/serve.go

+35-23
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package daemon
22

33
import (
44
"crypto/tls"
5-
"net/http"
6-
"sync"
7-
85
"github.com/ory/kratos/schema"
6+
"golang.org/x/sync/errgroup"
7+
"net/http"
98

109
"github.com/ory/kratos/selfservice/flow/recovery"
1110

@@ -50,7 +49,7 @@ type options struct {
5049
ctx stdctx.Context
5150
}
5251

53-
func newOptions(ctx stdctx.Context, opts []Option) *options {
52+
func NewOptions(ctx stdctx.Context, opts []Option) *options {
5453
o := new(options)
5554
o.ctx = ctx
5655
for _, f := range opts {
@@ -73,9 +72,8 @@ func WithContext(ctx stdctx.Context) Option {
7372
}
7473
}
7574

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)
7977
ctx := modifiers.ctx
8078

8179
c := r.Config(cmd.Context())
@@ -144,14 +142,15 @@ func ServePublic(r driver.Registry, wg *sync.WaitGroup, cmd *cobra.Command, args
144142
}
145143
return server.ServeTLS(listener, "", "")
146144
}, 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
148147
}
149148
l.Println("Public httpd was shutdown gracefully")
149+
return nil
150150
}
151151

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)
155154
ctx := modifiers.ctx
156155

157156
c := r.Config(ctx)
@@ -206,9 +205,11 @@ func ServeAdmin(r driver.Registry, wg *sync.WaitGroup, cmd *cobra.Command, args
206205
}
207206
return server.ServeTLS(listener, "", "")
208207
}, 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
210210
}
211211
l.Println("Admin httpd was shutdown gracefully")
212+
return nil
212213
}
213214

214215
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
280281
)
281282
}
282283

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)
286286
ctx := modifiers.ctx
287287

288288
if d.Config(ctx).IsBackgroundCourierEnabled() {
289289
go courier.Watch(ctx, d)
290290
}
291+
return nil
291292
}
292293

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()
301313
}
302314
}

cmd/serve/root.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func NewServeCmd() (serveCmd *cobra.Command) {
2929
serveCmd = &cobra.Command{
3030
Use: "serve",
3131
Short: "Run the Ory Kratos server",
32-
Run: func(cmd *cobra.Command, args []string) {
32+
RunE: func(cmd *cobra.Command, args []string) error {
3333
d := driver.New(cmd.Context(), cmd.ErrOrStderr(), configx.WithFlags(cmd.Flags()))
3434

3535
if d.Config(cmd.Context()).IsInsecureDevMode() {
@@ -50,7 +50,7 @@ DON'T DO THIS IN PRODUCTION!
5050
d.Logger().Warnf("Config version is '%s' but kratos runs on version '%s'", configVersion, config.Version)
5151
}
5252

53-
daemon.ServeAll(d)(cmd, args)
53+
return daemon.ServeAll(d)(cmd, args)
5454
},
5555
}
5656
configx.RegisterFlags(serveCmd.PersistentFlags())

0 commit comments

Comments
 (0)