work on signals

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
This commit is contained in:
Jörn Friedrich Dreyer
2024-07-24 12:25:16 +02:00
parent 06cc70f978
commit d6045a74ea
7 changed files with 28 additions and 24 deletions

View File

@@ -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)
}

View File

@@ -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)
},
}
}

View File

@@ -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))
}

View File

@@ -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
}

View File

@@ -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)
}
}

View File

@@ -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)
}

View File

@@ -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)
}()
}