From d6045a74ea72214583624f2671d8a9744598fb5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Wed, 24 Jul 2024 12:25:16 +0200 Subject: [PATCH] work on signals MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jörn Friedrich Dreyer --- ocis/pkg/command/root.go | 6 +++++- ocis/pkg/command/server.go | 2 +- ocis/pkg/runtime/runtime.go | 6 ++++-- ocis/pkg/runtime/service/service.go | 8 ++++---- services/gateway/cmd/gateway/main.go | 7 ++++++- services/gateway/pkg/command/root.go | 2 +- services/gateway/pkg/command/server.go | 21 +++++++-------------- 7 files changed, 28 insertions(+), 24 deletions(-) diff --git a/ocis/pkg/command/root.go b/ocis/pkg/command/root.go index 2edf4a750f..666ee0544f 100644 --- a/ocis/pkg/command/root.go +++ b/ocis/pkg/command/root.go @@ -1,7 +1,10 @@ package command import ( + "context" "os" + "os/signal" + "syscall" "github.com/owncloud/ocis/v2/ocis-pkg/clihelper" "github.com/owncloud/ocis/v2/ocis-pkg/config" @@ -25,5 +28,6 @@ func Execute() error { ) } - return app.Run(os.Args) + ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + return app.RunContext(ctx, os.Args) } diff --git a/ocis/pkg/command/server.go b/ocis/pkg/command/server.go index 108038ce72..f9c3aed070 100644 --- a/ocis/pkg/command/server.go +++ b/ocis/pkg/command/server.go @@ -21,7 +21,7 @@ func Server(cfg *config.Config) *cli.Command { Action: func(c *cli.Context) error { // Prefer the in-memory registry as the default when running in single-binary mode r := runtime.New(cfg) - return r.Start() + return r.Start(c.Context) }, } } diff --git a/ocis/pkg/runtime/runtime.go b/ocis/pkg/runtime/runtime.go index 32e6b8e1a9..49ce93c3b7 100644 --- a/ocis/pkg/runtime/runtime.go +++ b/ocis/pkg/runtime/runtime.go @@ -1,6 +1,8 @@ package runtime import ( + "context" + "github.com/owncloud/ocis/v2/ocis-pkg/config" "github.com/owncloud/ocis/v2/ocis/pkg/runtime/service" ) @@ -18,6 +20,6 @@ func New(cfg *config.Config) Runtime { } // Start rpc runtime -func (r *Runtime) Start() error { - return service.Start(service.WithConfig(r.c)) +func (r *Runtime) Start(ctx context.Context) error { + return service.Start(ctx, service.WithConfig(r.c)) } diff --git a/ocis/pkg/runtime/service/service.go b/ocis/pkg/runtime/service/service.go index d42604b7e6..eac779809d 100644 --- a/ocis/pkg/runtime/service/service.go +++ b/ocis/pkg/runtime/service/service.go @@ -96,7 +96,7 @@ type Service struct { // calls are done explicitly to loadFromEnv(). // Since this is the public constructor, options need to be added, at the moment only logging options // are supported in order to match the running OwnCloud services structured log. -func NewService(options ...Option) (*Service, error) { +func NewService(ctx context.Context, options ...Option) (*Service, error) { opts := NewOptions() for _, f := range options { @@ -109,7 +109,7 @@ func NewService(options ...Option) (*Service, error) { log.Level(opts.Config.Log.Level), ) - globalCtx, cancelGlobal := context.WithCancel(context.Background()) + globalCtx, cancelGlobal := context.WithCancel(ctx) s := &Service{ Services: make([]serviceFuncMap, len(_waitFuncs)), @@ -352,12 +352,12 @@ func NewService(options ...Option) (*Service, error) { // Start a rpc service. By default, the package scope Start will run all default services to provide with a working // oCIS instance. -func Start(o ...Option) error { +func Start(ctx context.Context, o ...Option) error { // Start the runtime. Most likely this was called ONLY by the `ocis server` subcommand, but since we cannot protect // from the caller, the previous statement holds truth. // prepare a new rpc Service struct. - s, err := NewService(o...) + s, err := NewService(ctx, o...) if err != nil { return err } diff --git a/services/gateway/cmd/gateway/main.go b/services/gateway/cmd/gateway/main.go index d22e804011..503e12de1c 100644 --- a/services/gateway/cmd/gateway/main.go +++ b/services/gateway/cmd/gateway/main.go @@ -1,14 +1,19 @@ package main import ( + "context" "os" + "os/signal" + "syscall" "github.com/owncloud/ocis/v2/services/gateway/pkg/command" "github.com/owncloud/ocis/v2/services/gateway/pkg/config/defaults" ) func main() { - if err := command.Execute(defaults.DefaultConfig()); err != nil { + cfg := defaults.DefaultConfig() + cfg.Context, _ = signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + if err := command.Execute(cfg); err != nil { os.Exit(1) } } diff --git a/services/gateway/pkg/command/root.go b/services/gateway/pkg/command/root.go index fcf6db18db..940a43259c 100644 --- a/services/gateway/pkg/command/root.go +++ b/services/gateway/pkg/command/root.go @@ -30,5 +30,5 @@ func Execute(cfg *config.Config) error { Commands: GetCommands(cfg), }) - return app.Run(os.Args) + return app.RunContext(cfg.Context, os.Args) } diff --git a/services/gateway/pkg/command/server.go b/services/gateway/pkg/command/server.go index d34b1a9986..9f556d556e 100644 --- a/services/gateway/pkg/command/server.go +++ b/services/gateway/pkg/command/server.go @@ -37,7 +37,7 @@ func Server(cfg *config.Config) *cli.Command { return err } gr := run.Group{} - ctx, cancel := defineContext(cfg) + ctx, cancel := context.WithCancel(c.Context) defer cancel() @@ -51,7 +51,9 @@ func Server(cfg *config.Config) *cli.Command { runtime.WithRegistry(reg), runtime.WithTraceProvider(traceProvider), ) - + logger.Info(). + Str("server", cfg.Service.Name). + Msg("reva runtime exited") return nil }, func(err error) { logger.Error(). @@ -60,7 +62,6 @@ func Server(cfg *config.Config) *cli.Command { Msg("Shutting down server") cancel() - os.Exit(1) }) debugServer, err := debug.Server( @@ -74,6 +75,9 @@ func Server(cfg *config.Config) *cli.Command { } gr.Add(debugServer.ListenAndServe, func(_ error) { + logger.Info(). + Str("server", cfg.Service.Name). + Msg("Shutting down debug erver") cancel() }) @@ -86,14 +90,3 @@ func Server(cfg *config.Config) *cli.Command { }, } } - -// defineContext sets the context for the service. If there is a context configured it will create a new child from it, -// if not, it will create a root context that can be cancelled. -func defineContext(cfg *config.Config) (context.Context, context.CancelFunc) { - return func() (context.Context, context.CancelFunc) { - if cfg.Context == nil { - return context.WithCancel(context.Background()) - } - return context.WithCancel(cfg.Context) - }() -}