From 6054e6b4a1b2231e0089c97ff0baa33eef00d819 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Wed, 10 Mar 2021 12:26:14 +0100 Subject: [PATCH] sequential access to viper config (still broken) + add delayed execution to storage-sharing --- accounts/pkg/command/root.go | 4 ++++ glauth/pkg/command/root.go | 4 ++++ graph/pkg/command/root.go | 4 ++++ idp/pkg/command/root.go | 4 ++++ ocis-pkg/sync/sync.go | 8 ++++++++ ocis/pkg/command/root.go | 4 ++++ ocis/pkg/runtime/service/service.go | 13 ++++++++++++- ocs/pkg/command/root.go | 4 ++++ onlyoffice/pkg/command/root.go | 4 ++++ proxy/pkg/command/root.go | 4 ++++ settings/pkg/command/root.go | 4 ++++ store/pkg/command/root.go | 4 ++++ thumbnails/pkg/command/root.go | 4 ++++ web/pkg/command/root.go | 4 ++++ webdav/pkg/command/root.go | 4 ++++ 15 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 ocis-pkg/sync/sync.go diff --git a/accounts/pkg/command/root.go b/accounts/pkg/command/root.go index 090f05f13..d4ded0213 100644 --- a/accounts/pkg/command/root.go +++ b/accounts/pkg/command/root.go @@ -5,6 +5,8 @@ import ( "os" "strings" + "github.com/owncloud/ocis/ocis-pkg/sync" + "github.com/owncloud/ocis/accounts/pkg/flagset" "github.com/micro/cli/v2" @@ -80,6 +82,8 @@ func NewLogger(cfg *config.Config) log.Logger { // ParseConfig loads accounts configuration from Viper known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { + sync.ParsingViperConfig.Lock() + defer sync.ParsingViperConfig.Unlock() logger := NewLogger(cfg) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) diff --git a/glauth/pkg/command/root.go b/glauth/pkg/command/root.go index 200f7b7dc..ec12f0adc 100644 --- a/glauth/pkg/command/root.go +++ b/glauth/pkg/command/root.go @@ -5,6 +5,8 @@ import ( "os" "strings" + "github.com/owncloud/ocis/ocis-pkg/sync" + "github.com/micro/cli/v2" "github.com/owncloud/ocis/glauth/pkg/config" "github.com/owncloud/ocis/glauth/pkg/flagset" @@ -68,6 +70,8 @@ func NewLogger(cfg *config.Config) log.Logger { // ParseConfig loads glauth configuration from Viper known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { + sync.ParsingViperConfig.Lock() + defer sync.ParsingViperConfig.Unlock() logger := NewLogger(cfg) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) diff --git a/graph/pkg/command/root.go b/graph/pkg/command/root.go index 89f0c0143..a179de92d 100644 --- a/graph/pkg/command/root.go +++ b/graph/pkg/command/root.go @@ -4,6 +4,8 @@ import ( "os" "strings" + "github.com/owncloud/ocis/ocis-pkg/sync" + "github.com/micro/cli/v2" "github.com/owncloud/ocis/graph/pkg/config" "github.com/owncloud/ocis/graph/pkg/flagset" @@ -66,6 +68,8 @@ func NewLogger(cfg *config.Config) log.Logger { // ParseConfig reads graph configuration from fs. func ParseConfig(c *cli.Context, cfg *config.Config) error { + sync.ParsingViperConfig.Lock() + defer sync.ParsingViperConfig.Unlock() logger := NewLogger(cfg) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) diff --git a/idp/pkg/command/root.go b/idp/pkg/command/root.go index 2fe55692b..3cddab916 100644 --- a/idp/pkg/command/root.go +++ b/idp/pkg/command/root.go @@ -5,6 +5,8 @@ import ( "os" "strings" + "github.com/owncloud/ocis/ocis-pkg/sync" + "github.com/micro/cli/v2" "github.com/owncloud/ocis/idp/pkg/config" "github.com/owncloud/ocis/idp/pkg/flagset" @@ -69,6 +71,8 @@ func NewLogger(cfg *config.Config) log.Logger { // ParseConfig load configuration for every extension func ParseConfig(c *cli.Context, cfg *config.Config) error { + sync.ParsingViperConfig.Lock() + defer sync.ParsingViperConfig.Unlock() logger := NewLogger(cfg) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) diff --git a/ocis-pkg/sync/sync.go b/ocis-pkg/sync/sync.go new file mode 100644 index 000000000..a279bdd41 --- /dev/null +++ b/ocis-pkg/sync/sync.go @@ -0,0 +1,8 @@ +package sync + +import "sync" + +var ( + // ParsingViperConfig addresses the fact that config parsing using Viper is not thread safe. + ParsingViperConfig sync.Mutex +) diff --git a/ocis/pkg/command/root.go b/ocis/pkg/command/root.go index da0216913..e57b41a34 100644 --- a/ocis/pkg/command/root.go +++ b/ocis/pkg/command/root.go @@ -4,6 +4,8 @@ import ( "os" "strings" + "github.com/owncloud/ocis/ocis-pkg/sync" + "github.com/micro/cli/v2" "github.com/owncloud/ocis/ocis-pkg/log" @@ -77,6 +79,8 @@ func NewLogger(cfg *config.Config) log.Logger { // ParseConfig load configuration for every extension func ParseConfig(c *cli.Context, cfg *config.Config) error { + sync.ParsingViperConfig.Lock() + defer sync.ParsingViperConfig.Unlock() logger := NewLogger(cfg) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) diff --git a/ocis/pkg/runtime/service/service.go b/ocis/pkg/runtime/service/service.go index a35fa3948..cd555c5fc 100644 --- a/ocis/pkg/runtime/service/service.go +++ b/ocis/pkg/runtime/service/service.go @@ -10,6 +10,7 @@ import ( "os/signal" "strings" "syscall" + "time" mzlog "github.com/asim/go-micro/plugins/logger/zerolog/v3" accounts "github.com/owncloud/ocis/accounts/pkg/command" @@ -37,6 +38,7 @@ import ( type Service struct { Supervisor *suture.Supervisor ServicesRegistry map[string]func(context.Context, *ociscfg.Config) suture.Service + Delayed map[string]func(context.Context, *ociscfg.Config) suture.Service Log zerolog.Logger serviceToken map[string][]suture.ServiceToken @@ -65,6 +67,7 @@ func NewService(options ...Option) (*Service, error) { s := &Service{ ServicesRegistry: make(map[string]func(context.Context, *ociscfg.Config) suture.Service), + Delayed: make(map[string]func(context.Context, *ociscfg.Config) suture.Service), Log: l, serviceToken: make(map[string][]suture.ServiceToken), @@ -94,7 +97,9 @@ func NewService(options ...Option) (*Service, error) { s.ServicesRegistry["storage-home"] = storage.NewStorageHome s.ServicesRegistry["storage-users"] = storage.NewStorageUsers s.ServicesRegistry["storage-public-link"] = storage.NewStoragePublicLink - s.ServicesRegistry["storage-sharing"] = storage.NewSharing + + // populate delayed services + s.Delayed["storage-sharing"] = storage.NewSharing return s, nil } @@ -159,6 +164,12 @@ func Start(o ...Option) error { // trap will block on halt channel for interruptions. go trap(s, halt) + time.Sleep(1 * time.Second) + // add delayed + for name := range s.Delayed { + s.serviceToken[name] = append(s.serviceToken[name], s.Supervisor.Add(s.Delayed[name](s.context, s.cfg))) + } + return http.Serve(l, nil) } diff --git a/ocs/pkg/command/root.go b/ocs/pkg/command/root.go index 1f91c15e7..7fd3e87e6 100644 --- a/ocs/pkg/command/root.go +++ b/ocs/pkg/command/root.go @@ -5,6 +5,8 @@ import ( "os" "strings" + "github.com/owncloud/ocis/ocis-pkg/sync" + "github.com/micro/cli/v2" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/log" @@ -66,6 +68,8 @@ func NewLogger(cfg *config.Config) log.Logger { // ParseConfig reads ocs configuration from fs. func ParseConfig(c *cli.Context, cfg *config.Config) error { + sync.ParsingViperConfig.Lock() + defer sync.ParsingViperConfig.Unlock() logger := NewLogger(cfg) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) diff --git a/onlyoffice/pkg/command/root.go b/onlyoffice/pkg/command/root.go index 9e4ef178a..a2f9170d9 100644 --- a/onlyoffice/pkg/command/root.go +++ b/onlyoffice/pkg/command/root.go @@ -5,6 +5,8 @@ import ( "os" "strings" + "github.com/owncloud/ocis/ocis-pkg/sync" + "github.com/micro/cli/v2" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/log" @@ -67,6 +69,8 @@ func NewLogger(cfg *config.Config) log.Logger { // ParseConfig loads onlyoffice configuration from Viper known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { + sync.ParsingViperConfig.Lock() + defer sync.ParsingViperConfig.Unlock() logger := NewLogger(cfg) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) diff --git a/proxy/pkg/command/root.go b/proxy/pkg/command/root.go index d23f499c4..ba2c2fec2 100644 --- a/proxy/pkg/command/root.go +++ b/proxy/pkg/command/root.go @@ -5,6 +5,8 @@ import ( "os" "strings" + "github.com/owncloud/ocis/ocis-pkg/sync" + "github.com/micro/cli/v2" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/log" @@ -70,6 +72,8 @@ func NewLogger(cfg *config.Config) log.Logger { // ParseConfig loads proxy configuration from Viper known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { + sync.ParsingViperConfig.Lock() + defer sync.ParsingViperConfig.Unlock() logger := NewLogger(cfg) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) diff --git a/settings/pkg/command/root.go b/settings/pkg/command/root.go index c7bd59c3c..d72c7a0d3 100644 --- a/settings/pkg/command/root.go +++ b/settings/pkg/command/root.go @@ -5,6 +5,8 @@ import ( "os" "strings" + "github.com/owncloud/ocis/ocis-pkg/sync" + "github.com/micro/cli/v2" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/log" @@ -69,6 +71,8 @@ func NewLogger(cfg *config.Config) log.Logger { // ParseConfig loads settings configuration from Viper known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { + sync.ParsingViperConfig.Lock() + defer sync.ParsingViperConfig.Unlock() logger := NewLogger(cfg) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) diff --git a/store/pkg/command/root.go b/store/pkg/command/root.go index 59cb43a1f..dc31f37dd 100644 --- a/store/pkg/command/root.go +++ b/store/pkg/command/root.go @@ -5,6 +5,8 @@ import ( "os" "strings" + "github.com/owncloud/ocis/ocis-pkg/sync" + "github.com/micro/cli/v2" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/log" @@ -69,6 +71,8 @@ func NewLogger(cfg *config.Config) log.Logger { // ParseConfig loads store configuration from Viper known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { + sync.ParsingViperConfig.Lock() + defer sync.ParsingViperConfig.Unlock() logger := NewLogger(cfg) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) diff --git a/thumbnails/pkg/command/root.go b/thumbnails/pkg/command/root.go index 3b08a5e43..9f9dff9f9 100644 --- a/thumbnails/pkg/command/root.go +++ b/thumbnails/pkg/command/root.go @@ -5,6 +5,8 @@ import ( "os" "strings" + "github.com/owncloud/ocis/ocis-pkg/sync" + "github.com/micro/cli/v2" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/log" @@ -66,6 +68,8 @@ func NewLogger(cfg *config.Config) log.Logger { // ParseConfig loads configuration from Viper known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { + sync.ParsingViperConfig.Lock() + defer sync.ParsingViperConfig.Unlock() logger := NewLogger(cfg) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) diff --git a/web/pkg/command/root.go b/web/pkg/command/root.go index d36096572..faa3b007e 100644 --- a/web/pkg/command/root.go +++ b/web/pkg/command/root.go @@ -5,6 +5,8 @@ import ( "os" "strings" + "github.com/owncloud/ocis/ocis-pkg/sync" + "github.com/micro/cli/v2" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/log" @@ -62,6 +64,8 @@ func NewLogger(cfg *config.Config) log.Logger { // ParseConfig loads proxy configuration from Viper known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { + sync.ParsingViperConfig.Lock() + defer sync.ParsingViperConfig.Unlock() logger := NewLogger(cfg) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) diff --git a/webdav/pkg/command/root.go b/webdav/pkg/command/root.go index 616a044de..85652f6a9 100644 --- a/webdav/pkg/command/root.go +++ b/webdav/pkg/command/root.go @@ -5,6 +5,8 @@ import ( "os" "strings" + "github.com/owncloud/ocis/ocis-pkg/sync" + "github.com/micro/cli/v2" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/log" @@ -64,6 +66,8 @@ func NewLogger(cfg *config.Config) log.Logger { // ParseConfig loads webdav configuration from Viper known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { + sync.ParsingViperConfig.Lock() + defer sync.ParsingViperConfig.Unlock() logger := NewLogger(cfg) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))