Skip to content

Commit

Permalink
work on signals
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
  • Loading branch information
butonic committed Jul 24, 2024
1 parent 15b34f2 commit bf56ddc
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 23 deletions.
6 changes: 5 additions & 1 deletion ocis/pkg/command/root.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion ocis/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}
}
Expand Down
6 changes: 4 additions & 2 deletions ocis/pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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))
}
8 changes: 4 additions & 4 deletions ocis/pkg/runtime/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)),
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions services/gateway/cmd/gateway/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
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() {
cfg := defaults.DefaultConfig()
cfg.Context, _ = signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
if err := command.Execute(defaults.DefaultConfig()); err != nil {
os.Exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion services/gateway/pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
21 changes: 7 additions & 14 deletions services/gateway/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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().
Expand All @@ -60,7 +62,6 @@ func Server(cfg *config.Config) *cli.Command {
Msg("Shutting down server")

cancel()
os.Exit(1)
})

debugServer, err := debug.Server(
Expand All @@ -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()
})

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

0 comments on commit bf56ddc

Please sign in to comment.