From e544840d7ddbd4da6a8fe05789044b0bad7d13b0 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Mon, 17 Feb 2020 14:17:05 +0100 Subject: [PATCH 1/4] magical fix --- pkg/micro/runtime/runtime.go | 81 +++++++++++++----------------------- 1 file changed, 30 insertions(+), 51 deletions(-) diff --git a/pkg/micro/runtime/runtime.go b/pkg/micro/runtime/runtime.go index cb381f5ca8..cee1111dd8 100644 --- a/pkg/micro/runtime/runtime.go +++ b/pkg/micro/runtime/runtime.go @@ -8,16 +8,9 @@ import ( "github.com/micro/cli/v2" gorun "github.com/micro/go-micro/v2/runtime" "github.com/micro/micro/v2/api" - "github.com/micro/micro/v2/broker" - "github.com/micro/micro/v2/health" - "github.com/micro/micro/v2/monitor" "github.com/micro/micro/v2/proxy" "github.com/micro/micro/v2/registry" - "github.com/micro/micro/v2/router" "github.com/micro/micro/v2/runtime" - "github.com/micro/micro/v2/server" - "github.com/micro/micro/v2/store" - "github.com/micro/micro/v2/tunnel" "github.com/micro/micro/v2/web" "github.com/owncloud/ocis-pkg/v2/log" ) @@ -27,13 +20,11 @@ var OwncloudNamespace = "com.owncloud." // RuntimeServices to start as part of the fullstack option var RuntimeServices = []string{ - "runtime", // :8088 - "registry", // :8000 - "broker", // :8001 - "router", // :8084 - "proxy", // :8081 "api", // :8080 + "proxy", // :8081 "web", // :8082 + "registry", // :8000 + "runtime", // :8088 (future proof. We want to be able to control extensions through a runtime) } // Extensions are ocis extension services @@ -59,22 +50,28 @@ var Extensions = []string{ "konnectd", } -// Runtime is a micro' runtime +var _services []*gorun.Service + +// Runtime is a wrapper around micro's own runtime type Runtime struct { - Services []string - Logger log.Logger - R *gorun.Runtime + Logger log.Logger + R *gorun.Runtime } // New creates a new ocis + micro runtime func New(opts ...Option) Runtime { options := newOptions(opts...) - return Runtime{ - Services: options.Services, - Logger: options.Logger, - R: options.MicroRuntime, + r := Runtime{ + Logger: options.Logger, + R: options.MicroRuntime, } + + for _, v := range append(RuntimeServices, Extensions...) { + _services = append(_services, &gorun.Service{Name: v}) + } + + return r } // Trap waits for a sigkill to stop the runtime @@ -96,7 +93,11 @@ func (r *Runtime) Trap() { r.Logger.Err(err) } - r.Logger.Info().Msgf("Service runtime shutdown") + for _, v := range _services { + r.Logger.Info().Msgf("gracefully stopping service %v", v.Name) + (*r.R).Delete(v) + } + os.Exit(0) } @@ -104,16 +105,15 @@ func (r *Runtime) Trap() { func (r *Runtime) Start() { env := os.Environ() - for _, service := range r.Services { + for _, service := range _services { + r.Logger.Info().Msgf("args: %v %v", os.Args[0], service.Name) // TODO uncommenting this line causes some issues where the binary calls itself with the `server` as argument args := []gorun.CreateOption{ - // the binary calls itself with the micro service as a subcommand as first argument - gorun.WithCommand(os.Args[0], service), + gorun.WithCommand(os.Args[0], service.Name), gorun.WithEnv(env), gorun.WithOutput(os.Stdout), } - muService := &gorun.Service{Name: service} - if err := (*r.R).Create(muService, args...); err != nil { + if err := (*r.R).Create(service, args...); err != nil { r.Logger.Error().Msgf("Failed to create runtime enviroment: %v", err) } } @@ -124,13 +124,10 @@ func AddRuntime(app *cli.App) { setDefaults() app.Commands = append(app.Commands, api.Commands()...) - app.Commands = append(app.Commands, broker.Commands()...) - app.Commands = append(app.Commands, health.Commands()...) app.Commands = append(app.Commands, proxy.Commands()...) - app.Commands = append(app.Commands, router.Commands()...) + app.Commands = append(app.Commands, web.Commands()...) app.Commands = append(app.Commands, registry.Commands()...) app.Commands = append(app.Commands, runtime.Commands()...) - app.Commands = append(app.Commands, web.Commands()...) } // provide a config.Config with default values? @@ -140,34 +137,16 @@ func setDefaults() { api.Namespace = OwncloudNamespace + "api" api.HeaderPrefix = "X-Micro-Owncloud-" - // broker - broker.Name = OwncloudNamespace + "http.broker" - // proxy proxy.Name = OwncloudNamespace + "proxy" - // monitor - monitor.Name = OwncloudNamespace + "monitor" - - // router - router.Name = OwncloudNamespace + "router" - - // tunnel - tunnel.Name = OwncloudNamespace + "tunnel" + // web + web.Name = OwncloudNamespace + "web" + web.Namespace = OwncloudNamespace + "web" // registry registry.Name = OwncloudNamespace + "registry" // runtime runtime.Name = OwncloudNamespace + "runtime" - - // server - server.Name = OwncloudNamespace + "server" - - // store - store.Name = OwncloudNamespace + "store" - - // web - web.Name = OwncloudNamespace + "web" - web.Namespace = OwncloudNamespace + "web" } From e0cfee48f46521444970d64c894615dc9f5ecba5 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Mon, 17 Feb 2020 14:22:52 +0100 Subject: [PATCH 2/4] embedded services as part of the Runtime struct --- pkg/micro/runtime/runtime.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/micro/runtime/runtime.go b/pkg/micro/runtime/runtime.go index cee1111dd8..420c659553 100644 --- a/pkg/micro/runtime/runtime.go +++ b/pkg/micro/runtime/runtime.go @@ -50,12 +50,12 @@ var Extensions = []string{ "konnectd", } -var _services []*gorun.Service - // Runtime is a wrapper around micro's own runtime type Runtime struct { Logger log.Logger R *gorun.Runtime + + services []*gorun.Service } // New creates a new ocis + micro runtime @@ -68,7 +68,7 @@ func New(opts ...Option) Runtime { } for _, v := range append(RuntimeServices, Extensions...) { - _services = append(_services, &gorun.Service{Name: v}) + r.services = append(r.services, &gorun.Service{Name: v}) } return r @@ -93,7 +93,7 @@ func (r *Runtime) Trap() { r.Logger.Err(err) } - for _, v := range _services { + for _, v := range r.services { r.Logger.Info().Msgf("gracefully stopping service %v", v.Name) (*r.R).Delete(v) } @@ -105,7 +105,7 @@ func (r *Runtime) Trap() { func (r *Runtime) Start() { env := os.Environ() - for _, service := range _services { + for _, service := range r.services { r.Logger.Info().Msgf("args: %v %v", os.Args[0], service.Name) // TODO uncommenting this line causes some issues where the binary calls itself with the `server` as argument args := []gorun.CreateOption{ gorun.WithCommand(os.Args[0], service.Name), From 4a9fe6ec5fe60564ea2bc53ed52ae4f2d632b22b Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Mon, 17 Feb 2020 14:43:56 +0100 Subject: [PATCH 3/4] rename default services --- pkg/command/root.go | 9 ++-- pkg/command/server.go | 5 +- pkg/micro/runtime/command.go | 2 +- pkg/micro/runtime/runtime.go | 90 +++++++++++++++++++----------------- 4 files changed, 52 insertions(+), 54 deletions(-) diff --git a/pkg/command/root.go b/pkg/command/root.go index 375a0d061e..e5a6f2298d 100644 --- a/pkg/command/root.go +++ b/pkg/command/root.go @@ -76,6 +76,7 @@ func Execute() error { }, } + // Load commands from the registry for _, fn := range register.Commands { app.Commands = append( app.Commands, @@ -83,11 +84,7 @@ func Execute() error { ) } - runtime.AddRuntime(app) - app.Commands = append( - app.Commands, - runtime.Command(app), - ) + runtime.AddMicroPlatform(app) cli.HelpFlag = &cli.BoolFlag{ Name: "help,h", @@ -102,7 +99,7 @@ func Execute() error { return app.Run(os.Args) } -// NewLogger initializes a service-specific logger instance. +// NewLogger initializes a service-specific logger instance func NewLogger(cfg *config.Config) log.Logger { return log.NewLogger( log.Name("ocis"), diff --git a/pkg/command/server.go b/pkg/command/server.go index caa8f2d0fc..0bfbe10db0 100644 --- a/pkg/command/server.go +++ b/pkg/command/server.go @@ -36,15 +36,12 @@ func Server(cfg *config.Config) *cli.Command { } runtime := runtime.New( - runtime.Services(append(runtime.RuntimeServices, runtime.Extensions...)), + runtime.Services(append(runtime.MicroServices, runtime.Extensions...)), runtime.Logger(logger), runtime.MicroRuntime(cmd.DefaultCmd.Options().Runtime), ) - // fork uses the micro runtime to fork go-micro services runtime.Start() - - // trap blocks until a kill signal is sent runtime.Trap() return nil diff --git a/pkg/micro/runtime/command.go b/pkg/micro/runtime/command.go index 0ce1f514c6..c9ea321a2d 100644 --- a/pkg/micro/runtime/command.go +++ b/pkg/micro/runtime/command.go @@ -14,7 +14,7 @@ func Command(app *cli.App) *cli.Command { Category: "Micro", Action: func(c *cli.Context) error { runtime := New( - Services(RuntimeServices), + Services(MicroServices), Logger(log.NewLogger()), MicroRuntime(cmd.DefaultCmd.Options().Runtime), ) diff --git a/pkg/micro/runtime/runtime.go b/pkg/micro/runtime/runtime.go index 420c659553..9745fe485f 100644 --- a/pkg/micro/runtime/runtime.go +++ b/pkg/micro/runtime/runtime.go @@ -15,40 +15,42 @@ import ( "github.com/owncloud/ocis-pkg/v2/log" ) -// OwncloudNamespace is the base path for micro' services to use -var OwncloudNamespace = "com.owncloud." +var ( + // OwncloudNamespace is the base path for micro' services to use + OwncloudNamespace = "com.owncloud." -// RuntimeServices to start as part of the fullstack option -var RuntimeServices = []string{ - "api", // :8080 - "proxy", // :8081 - "web", // :8082 - "registry", // :8000 - "runtime", // :8088 (future proof. We want to be able to control extensions through a runtime) -} + // MicroServices to start as part of the fullstack option + MicroServices = []string{ + "api", // :8080 + "proxy", // :8081 + "web", // :8082 + "registry", // :8000 + "runtime", // :8088 (future proof. We want to be able to control extensions through a runtime) + } -// Extensions are ocis extension services -var Extensions = []string{ - "hello", - "phoenix", - "graph", - "graph-explorer", - "ocs", - "webdav", - "reva-frontend", - "reva-gateway", - "reva-users", - "reva-auth-basic", - "reva-auth-bearer", - "reva-sharing", - "reva-storage-root", - "reva-storage-home", - "reva-storage-home-data", - "reva-storage-oc", - "reva-storage-oc-data", - "devldap", - "konnectd", -} + // Extensions are ocis extension services + Extensions = []string{ + "hello", + "phoenix", + "graph", + "graph-explorer", + "ocs", + "webdav", + "reva-frontend", + "reva-gateway", + "reva-users", + "reva-auth-basic", + "reva-auth-bearer", + "reva-sharing", + "reva-storage-root", + "reva-storage-home", + "reva-storage-home-data", + "reva-storage-oc", + "reva-storage-oc-data", + "devldap", + "konnectd", + } +) // Runtime is a wrapper around micro's own runtime type Runtime struct { @@ -67,15 +69,15 @@ func New(opts ...Option) Runtime { R: options.MicroRuntime, } - for _, v := range append(RuntimeServices, Extensions...) { + for _, v := range append(MicroServices, Extensions...) { r.services = append(r.services, &gorun.Service{Name: v}) } return r } -// Trap waits for a sigkill to stop the runtime -func (r *Runtime) Trap() { +// Trap listen and blocks for termination signals +func (r Runtime) Trap() { shutdown := make(chan os.Signal, 1) signal.Notify(shutdown, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT) @@ -83,19 +85,20 @@ func (r *Runtime) Trap() { os.Exit(1) } - // block until there is a value for range shutdown { r.Logger.Info().Msg("shutdown signal received") close(shutdown) } if err := (*r.R).Stop(); err != nil { - r.Logger.Err(err) + r.Logger.Err(err).Msgf("error while shutting down") } - for _, v := range r.services { - r.Logger.Info().Msgf("gracefully stopping service %v", v.Name) - (*r.R).Delete(v) + for _, s := range r.services { + r.Logger.Info().Msgf("gracefully stopping service %v", s.Name) + if err := (*r.R).Delete(s); err != nil { + r.Logger.Err(err).Msgf("error while deleting service: %v", s.Name) + } } os.Exit(0) @@ -106,7 +109,8 @@ func (r *Runtime) Start() { env := os.Environ() for _, service := range r.services { - r.Logger.Info().Msgf("args: %v %v", os.Args[0], service.Name) // TODO uncommenting this line causes some issues where the binary calls itself with the `server` as argument + // TODO uncommenting this line causes some issues where the binary calls itself with the `server` as argument + r.Logger.Info().Msgf("args: %v %v", os.Args[0], service.Name) args := []gorun.CreateOption{ gorun.WithCommand(os.Args[0], service.Name), gorun.WithEnv(env), @@ -119,8 +123,8 @@ func (r *Runtime) Start() { } } -// AddRuntime adds the micro subcommands to the cli app -func AddRuntime(app *cli.App) { +// AddMicroPlatform adds the micro subcommands to the cli app +func AddMicroPlatform(app *cli.App) { setDefaults() app.Commands = append(app.Commands, api.Commands()...) From 0de18a4d3e07344148aace44d68d946ac0a28bf8 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Mon, 17 Feb 2020 14:57:50 +0100 Subject: [PATCH 4/4] spawn as a separate goroutine --- pkg/micro/runtime/runtime.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/pkg/micro/runtime/runtime.go b/pkg/micro/runtime/runtime.go index 9745fe485f..8129e1a642 100644 --- a/pkg/micro/runtime/runtime.go +++ b/pkg/micro/runtime/runtime.go @@ -108,18 +108,14 @@ func (r Runtime) Trap() { func (r *Runtime) Start() { env := os.Environ() - for _, service := range r.services { - // TODO uncommenting this line causes some issues where the binary calls itself with the `server` as argument - r.Logger.Info().Msgf("args: %v %v", os.Args[0], service.Name) + for i := range r.services { args := []gorun.CreateOption{ - gorun.WithCommand(os.Args[0], service.Name), + gorun.WithCommand(os.Args[0], r.services[i].Name), gorun.WithEnv(env), gorun.WithOutput(os.Stdout), } - if err := (*r.R).Create(service, args...); err != nil { - r.Logger.Error().Msgf("Failed to create runtime enviroment: %v", err) - } + go (*r.R).Create(r.services[i], args...) } }