diff --git a/corteza/corteza.go b/corteza/corteza.go index 77b54e53b..5087e8341 100644 --- a/corteza/corteza.go +++ b/corteza/corteza.go @@ -86,8 +86,9 @@ func (app *App) Activate(ctx context.Context) (err error) { // Start scheduler scheduler.Service().Start(ctx) - // Load corredor scripts + // Load corredor scripts & init watcher (script reloader) corredor.Service().Load(ctx) + corredor.Service().Watch(ctx) return nil } diff --git a/pkg/app/options/corredor.go b/pkg/app/options/corredor.go index 61bf4721d..3102037d1 100644 --- a/pkg/app/options/corredor.go +++ b/pkg/app/options/corredor.go @@ -20,6 +20,7 @@ type ( DefaultExecTimeout time.Duration `env:"CORREDOR_DEFAULT_EXEC_TIMEOUT"` ListTimeout time.Duration `env:"CORREDOR_LIST_TIMEOUT"` + ListRefresh time.Duration `env:"CORREDOR_LIST_REFRESH"` // Allow scripts to have runner explicitly defined RunAsEnabled bool `env:"CORREDOR_RUN_AS_ENABLED"` @@ -41,6 +42,7 @@ func Corredor() (o *CorredorOpt) { MaxBackoffDelay: time.Minute, DefaultExecTimeout: time.Minute, ListTimeout: time.Second * 2, + ListRefresh: time.Second * 5, Log: false, TlsCertEnabled: true, diff --git a/pkg/corredor/service-corredor.pb.go b/pkg/corredor/service-corredor.pb.go index da1d637aa..4ecb56d86 100644 --- a/pkg/corredor/service-corredor.pb.go +++ b/pkg/corredor/service-corredor.pb.go @@ -395,6 +395,7 @@ type ServerScript struct { Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` Label string `protobuf:"bytes,2,opt,name=label,proto3" json:"label,omitempty"` Description string `protobuf:"bytes,3,opt,name=description,proto3" json:"description,omitempty"` + UpdatedAt string `protobuf:"bytes,12,opt,name=updatedAt,proto3" json:"updatedAt,omitempty"` Security *Security `protobuf:"bytes,13,opt,name=security,proto3" json:"security,omitempty"` Triggers []*Trigger `protobuf:"bytes,14,rep,name=triggers,proto3" json:"triggers,omitempty"` Errors []string `protobuf:"bytes,15,rep,name=errors,proto3" json:"errors,omitempty"` @@ -449,6 +450,13 @@ func (m *ServerScript) GetDescription() string { return "" } +func (m *ServerScript) GetUpdatedAt() string { + if m != nil { + return m.UpdatedAt + } + return "" +} + func (m *ServerScript) GetSecurity() *Security { if m != nil { return m.Security @@ -476,6 +484,7 @@ type ClientScript struct { Description string `protobuf:"bytes,3,opt,name=description,proto3" json:"description,omitempty"` Bundle string `protobuf:"bytes,4,opt,name=bundle,proto3" json:"bundle,omitempty"` Type string `protobuf:"bytes,6,opt,name=type,proto3" json:"type,omitempty"` + UpdatedAt string `protobuf:"bytes,12,opt,name=updatedAt,proto3" json:"updatedAt,omitempty"` Security *Security `protobuf:"bytes,13,opt,name=security,proto3" json:"security,omitempty"` Triggers []*Trigger `protobuf:"bytes,14,rep,name=triggers,proto3" json:"triggers,omitempty"` Errors []string `protobuf:"bytes,15,rep,name=errors,proto3" json:"errors,omitempty"` @@ -544,6 +553,13 @@ func (m *ClientScript) GetType() string { return "" } +func (m *ClientScript) GetUpdatedAt() string { + if m != nil { + return m.UpdatedAt + } + return "" +} + func (m *ClientScript) GetSecurity() *Security { if m != nil { return m.Security @@ -863,52 +879,53 @@ func init() { func init() { proto.RegisterFile("service-corredor.proto", fileDescriptor_c06bb92bf45e37e2) } var fileDescriptor_c06bb92bf45e37e2 = []byte{ - // 711 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x56, 0xcd, 0x6e, 0xd3, 0x40, - 0x10, 0xd6, 0x26, 0x69, 0x7e, 0xc6, 0x49, 0x29, 0xab, 0x36, 0xb5, 0x72, 0x80, 0xb0, 0x70, 0x88, - 0x40, 0x8d, 0x50, 0x2a, 0x54, 0x28, 0x5c, 0xda, 0x52, 0x21, 0x50, 0x91, 0x90, 0x5b, 0x1e, 0x20, - 0x75, 0x46, 0x91, 0x85, 0xf1, 0xba, 0xbb, 0xeb, 0x40, 0x1e, 0x80, 0x13, 0x07, 0x4e, 0x9c, 0x78, - 0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13, 0x34, 0x1d, 0xa7, 0xcf, 0xa3, 0xaf, 0x04, 0x3a, 0xf6, 0x6c, 0x4a, 0xfa, 0x08, - 0x6a, 0xe9, 0xe7, 0x80, 0x6e, 0x2d, 0xfd, 0x3e, 0xf5, 0xba, 0xcb, 0xbf, 0x1a, 0xf4, 0x35, 0xd4, - 0xd2, 0x4d, 0x44, 0xef, 0x2c, 0x5f, 0xa6, 0xd6, 0xca, 0xec, 0xb1, 0xeb, 0x20, 0x99, 0xbb, 0xd1, - 0x37, 0x02, 0x1d, 0x7b, 0xbe, 0x24, 0x7d, 0x5a, 0xe4, 0xbb, 0x5d, 0x5a, 0x35, 0xc6, 0xb1, 0x5b, - 0xbe, 0x58, 0xcd, 0x6e, 0xc5, 0x42, 0xb7, 0xd9, 0xad, 0x5a, 0xb3, 0xa3, 0x16, 0x34, 0x4e, 0x15, - 0x17, 0xe3, 0x29, 0x9e, 0xd7, 0xf5, 0x9f, 0xc0, 0xee, 0xaf, 0x00, 0x00, 0x00, 0xff, 0xff, 0xef, - 0x2c, 0x4a, 0xb8, 0x23, 0x08, 0x00, 0x00, + // 725 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xcc, 0x56, 0xcd, 0x6e, 0xd3, 0x4e, + 0x10, 0x97, 0x93, 0x34, 0x1f, 0xe3, 0xa4, 0xff, 0xfe, 0x57, 0x6d, 0x6a, 0x45, 0x08, 0xc2, 0xc2, + 0x21, 0x02, 0x35, 0x42, 0xa9, 0x50, 0xa1, 0x70, 0x69, 0x4b, 0x85, 0x40, 0x45, 0x42, 0x6e, 0x79, + 0x00, 0xd7, 0x1e, 0x45, 0x16, 0xc6, 0xeb, 0xee, 0xae, 0x03, 0x79, 0x00, 0x4e, 0x1c, 0x38, 0x71, + 0xea, 0x73, 0xf0, 0x5c, 0xbc, 0x02, 0xf2, 0x7a, 0xed, 0x6c, 0xeb, 0xa4, 0x02, 0x09,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, 0x85, 0xc7, 0x2b, 0x32, 0x72, 0x1b, 0x00, 0x67, 0x18, 0xcb, 0x8c, 0x11, 0x4e, + 0x7d, 0x58, 0x1f, 0x75, 0x5c, 0x43, 0x42, 0x4f, 0xc0, 0xa9, 0x06, 0xd5, 0x7d, 0x78, 0x04, 0x2d, + 0xa1, 0xa4, 0xc2, 0xb1, 0x54, 0x23, 0xfa, 0x8b, 0x46, 0x98, 0x46, 0x6e, 0x01, 0xa3, 0x5f, 0x2c, + 0xd8, 0x3e, 0x8a, 0x42, 0x8c, 0xe5, 0x5f, 0xac, 0x81, 0xf4, 0xa1, 0x79, 0x9e, 0xc6, 0x41, 0x84, + 0x4e, 0x43, 0x59, 0x6b, 0x2e, 0xab, 0xad, 0x9a, 0xcc, 0x2f, 0xd4, 0x66, 0x1a, 0x2d, 0x6a, 0xbb, + 0x07, 0xbd, 0x43, 0xe5, 0xf7, 0x86, 0xc9, 0xa5, 0xcf, 0x61, 0xbd, 0x00, 0xe9, 0x40, 0x0f, 0xa0, + 0x95, 0xa7, 0x53, 0x04, 0xda, 0x58, 0x04, 0xd2, 0xd0, 0x02, 0x40, 0x7f, 0x58, 0xd0, 0x35, 0x1b, + 0xbb, 0x74, 0x39, 0x36, 0x61, 0x2d, 0xf2, 0xce, 0x31, 0x2a, 0x06, 0x48, 0x31, 0x64, 0x08, 0x76, + 0x80, 0x79, 0xaa, 0x21, 0x8b, 0x9d, 0xba, 0xd2, 0x99, 0x22, 0x72, 0x0b, 0x3a, 0x69, 0x12, 0x78, + 0x12, 0x83, 0x03, 0xe9, 0x74, 0x95, 0x7e, 0x21, 0x20, 0x63, 0x68, 0x0b, 0xf4, 0x53, 0x1e, 0xca, + 0xb9, 0xd3, 0x1b, 0x5a, 0x23, 0x7b, 0x42, 0xcc, 0x8f, 0x9d, 0x6b, 0xdc, 0x12, 0x43, 0x76, 0xa0, + 0x2d, 0x79, 0x38, 0x9d, 0x22, 0x17, 0xce, 0xba, 0xaa, 0xeb, 0xff,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x1a, 0x3e, + 0x0b, 0x50, 0xcf, 0xa3, 0xa2, 0x27, 0xdf, 0x2c, 0xe8, 0x99, 0x7b, 0x2d, 0xc8, 0x63, 0x68, 0x64, + 0x4f, 0x09, 0xd9, 0x5a, 0xfa, 0xb6, 0x0d, 0xfa, 0xcb, 0x5f, 0x1c, 0xf2, 0x06, 0x1a, 0xd9, 0x15, + 0x23, 0x77, 0x97, 0x1f, 0x62, 0xe3, 0xdc, 0x0e, 0xe8, 0x4d, 0x90, 0xdc, 0xdd, 0xe4, 0xd2, 0x82, + 0x9e, 0xb9, 0x7d, 0x82, 0x3c, 0x2b, 0xeb, 0xdd, 0xae, 0x9c, 0x29, 0xed, 0xd8, 0xa9, 0x2a, 0x56, + 0x67, 0xb7, 0xe2, 0x31, 0x30, 0xb3, 0x5b, 0x75, 0xa2, 0x27, 0x1d, 0x68, 0x9d, 0x4a, 0xc6, 0xbd, + 0x29, 0x9e, 0x37, 0xd5, 0x5f, 0xc4, 0xee, 0xcf, 0x00, 0x00, 0x00, 0xff, 0xff, 0xd6, 0x7e, 0x48, + 0x92, 0x5f, 0x08, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. diff --git a/pkg/corredor/service.go b/pkg/corredor/service.go index ff722edeb..2d0f94b31 100644 --- a/pkg/corredor/service.go +++ b/pkg/corredor/service.go @@ -2,6 +2,7 @@ package corredor import ( "context" + "github.com/cortezaproject/corteza-server/pkg/sentry" "github.com/go-chi/chi/middleware" "github.com/pkg/errors" "go.uber.org/zap" @@ -9,6 +10,7 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" + "time" "github.com/cortezaproject/corteza-server/pkg/app/options" "github.com/cortezaproject/corteza-server/pkg/auth" @@ -32,8 +34,10 @@ type ( manual map[string]map[string]bool // Combined list of client and server scripts - sScripts ScriptSet - cScripts ScriptSet + sScripts ScriptSet + sScriptsTS time.Time + cScripts ScriptSet + cScriptsTS time.Time conn *grpc.ClientConn @@ -146,6 +150,25 @@ func (svc *service) connect(ctx context.Context) (err error) { return } +// Watch watches for changes +func (svc *service) Watch(ctx context.Context) { + go func() { + defer sentry.Recover() + var ticker = time.NewTicker(svc.opt.ListRefresh) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + svc.Load(ctx) + } + } + }() + + svc.log.Debug("watcher initialized") +} + func (svc *service) SetEventRegistry(er eventRegistry) { svc.eventRegistry = er } @@ -263,18 +286,27 @@ func (svc *service) loadServerScripts(ctx context.Context) { rsp *ServerScriptListResponse ) - svc.log.Debug("reloading server scripts") - ctx, cancel := context.WithTimeout(ctx, svc.opt.ListTimeout) defer cancel() + if !svc.sScriptsTS.IsZero() { + ctx = metadata.NewOutgoingContext(ctx, metadata.MD{ + "if-modified-since": []string{svc.sScriptsTS.Format(time.RFC3339)}, + }) + } + rsp, err = svc.ssClient.List(ctx, &ServerScriptListRequest{}, grpc.WaitForReady(true)) if err != nil { svc.log.Error("could not load corredor server scripts", zap.Error(err)) return } - svc.registerServerScripts(rsp.Scripts...) + svc.sScriptsTS = time.Now() + + if len(rsp.Scripts) > 0 { + svc.log.Debug("reloading server scripts") + svc.registerServerScripts(rsp.Scripts...) + } } // Registers Corredor scripts to eventbus and list of manual scripts @@ -391,7 +423,6 @@ func (svc *service) registerServerScripts(ss ...*ServerScript) { svc.log.Debug( "script registered", zap.String("script", s.Name), - zap.Stringer("security", s.Security), zap.Int("manual", len(svc.manual[script.Name])), zap.Int("triggers", len(svc.registered[script.Name])), ) @@ -636,18 +667,27 @@ func (svc *service) loadClientScripts(ctx context.Context) { rsp *ClientScriptListResponse ) - svc.log.Debug("reloading client scripts") - ctx, cancel := context.WithTimeout(ctx, svc.opt.ListTimeout) defer cancel() + if !svc.sScriptsTS.IsZero() { + ctx = metadata.NewOutgoingContext(ctx, metadata.MD{ + "if-modified-since": []string{svc.sScriptsTS.Format(time.RFC3339)}, + }) + } + rsp, err = svc.csClient.List(ctx, &ClientScriptListRequest{}, grpc.WaitForReady(true)) if err != nil { svc.log.Error("could not load corredor client scripts", zap.Error(err)) return } - svc.registerClientScripts(rsp.Scripts...) + svc.sScriptsTS = time.Now() + + if len(rsp.Scripts) > 0 { + svc.log.Debug("reloading client scripts") + svc.registerClientScripts(rsp.Scripts...) + } } func (svc *service) registerClientScripts(ss ...*ClientScript) { @@ -672,14 +712,12 @@ func (svc *service) GetBundle(ctx context.Context, name, bType string) *Bundle { rsp *BundleResponse ) - svc.log.Debug("reloading server scripts") - ctx, cancel := context.WithTimeout(ctx, svc.opt.ListTimeout) defer cancel() rsp, err = svc.csClient.Bundle(ctx, &BundleRequest{Name: name}, grpc.WaitForReady(true)) if err != nil { - svc.log.Error("could not load corredor server scripts", zap.Error(err)) + svc.log.Error("could not load client scripts bundle from corredor", zap.Error(err)) return nil }