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 cb381f5ca8..8129e1a642 100644 --- a/pkg/micro/runtime/runtime.go +++ b/pkg/micro/runtime/runtime.go @@ -8,77 +8,76 @@ 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" ) -// 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{ - "runtime", // :8088 - "registry", // :8000 - "broker", // :8001 - "router", // :8084 - "proxy", // :8081 - "api", // :8080 - "web", // :8082 -} + // 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 micro' runtime +// 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 + + services []*gorun.Service } // 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(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) @@ -86,17 +85,22 @@ 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 _, 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) + } } - r.Logger.Info().Msgf("Service runtime shutdown") os.Exit(0) } @@ -104,33 +108,26 @@ func (r *Runtime) Trap() { func (r *Runtime) Start() { env := os.Environ() - for _, service := range r.Services { + for i := range r.services { 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], r.services[i].Name), gorun.WithEnv(env), gorun.WithOutput(os.Stdout), } - muService := &gorun.Service{Name: service} - if err := (*r.R).Create(muService, args...); err != nil { - r.Logger.Error().Msgf("Failed to create runtime enviroment: %v", err) - } + go (*r.R).Create(r.services[i], args...) } } -// 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()...) - 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" }