From efb8ba8b996f2a48840fec570d1da67c0bbd799f Mon Sep 17 00:00:00 2001 From: William Petit Date: Thu, 19 Oct 2023 20:05:59 +0200 Subject: [PATCH] feat(app): pass context to start process --- cmd/cli/command/app/run.go | 2 +- pkg/app/server.go | 14 +++++++------- pkg/app/server_module.go | 4 +++- pkg/http/handler.go | 9 +++++---- pkg/module/app/memory/module_test.go | 7 ++++--- pkg/module/auth/module_test.go | 10 ++++++---- pkg/module/blob/module_test.go | 4 +++- pkg/module/cast/module_test.go | 6 ++++-- pkg/module/fetch/module_test.go | 3 ++- pkg/module/lifecycle.go | 8 ++++---- pkg/module/rpc.go | 8 +++----- pkg/module/share/module_test.go | 3 ++- pkg/module/store/module_test.go | 3 ++- 13 files changed, 46 insertions(+), 35 deletions(-) diff --git a/cmd/cli/command/app/run.go b/cmd/cli/command/app/run.go index 8ef5674..903ec09 100644 --- a/cmd/cli/command/app/run.go +++ b/cmd/cli/command/app/run.go @@ -238,7 +238,7 @@ func runApp(ctx context.Context, path, address, documentStoreDSN, blobStoreDSN, authModuleMiddleware.AnonymousUser(key, jwa.HS256), ), ) - if err := handler.Load(bundle); err != nil { + if err := handler.Load(ctx, bundle); err != nil { return errors.Wrap(err, "could not load app bundle") } diff --git a/pkg/app/server.go b/pkg/app/server.go index 9df68dd..c5f2fd7 100644 --- a/pkg/app/server.go +++ b/pkg/app/server.go @@ -13,7 +13,7 @@ import ( var ( ErrFuncDoesNotExist = errors.New("function does not exist") - ErUnknownError = errors.New("unknown error") + ErrUnknownError = errors.New("unknown error") ) type Server struct { @@ -90,7 +90,7 @@ func (s *Server) Exec(ctx context.Context, callableOrFuncname any, args ...inter if ok { logger.Error(ctx, "recovered runtime error", logger.E(errors.WithStack(revoveredErr))) - err = errors.WithStack(ErUnknownError) + err = errors.WithStack(ErrUnknownError) return } @@ -162,7 +162,7 @@ func (s *Server) WaitForPromise(promise *goja.Promise) goja.Value { return value } -func (s *Server) Start() error { +func (s *Server) Start(ctx context.Context) error { s.loop.Start() var err error @@ -171,7 +171,7 @@ func (s *Server) Start() error { rt.SetFieldNameMapper(goja.TagFieldNameMapper("goja", true)) rt.SetRandSource(createRandomSource()) - if err = s.initModules(rt); err != nil { + if err = s.initModules(ctx, rt); err != nil { err = errors.WithStack(err) } }) @@ -186,7 +186,7 @@ func (s *Server) Stop() { s.loop.Stop() } -func (s *Server) initModules(rt *goja.Runtime) error { +func (s *Server) initModules(ctx context.Context, rt *goja.Runtime) error { modules := make([]ServerModule, 0, len(s.factories)) for _, moduleFactory := range s.factories { @@ -206,9 +206,9 @@ func (s *Server) initModules(rt *goja.Runtime) error { continue } - logger.Debug(context.Background(), "initializing module", logger.F("module", initMod.Name())) + logger.Debug(ctx, "initializing module", logger.F("module", initMod.Name())) - if err := initMod.OnInit(rt); err != nil { + if err := initMod.OnInit(ctx, rt); err != nil { return errors.WithStack(err) } } diff --git a/pkg/app/server_module.go b/pkg/app/server_module.go index 03f7f25..77241fb 100644 --- a/pkg/app/server_module.go +++ b/pkg/app/server_module.go @@ -1,6 +1,8 @@ package app import ( + "context" + "github.com/dop251/goja" ) @@ -13,5 +15,5 @@ type ServerModule interface { type InitializableModule interface { ServerModule - OnInit(rt *goja.Runtime) error + OnInit(ctx context.Context, rt *goja.Runtime) error } diff --git a/pkg/http/handler.go b/pkg/http/handler.go index a521f23..d486d75 100644 --- a/pkg/http/handler.go +++ b/pkg/http/handler.go @@ -1,7 +1,8 @@ package http import ( - "io/ioutil" + "context" + "io" "net/http" "sync" @@ -40,7 +41,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.router.ServeHTTP(w, r) } -func (h *Handler) Load(bdle bundle.Bundle) error { +func (h *Handler) Load(ctx context.Context, bdle bundle.Bundle) error { h.mutex.Lock() defer h.mutex.Unlock() @@ -49,7 +50,7 @@ func (h *Handler) Load(bdle bundle.Bundle) error { return errors.Wrap(err, "could not open server main script") } - mainScript, err := ioutil.ReadAll(file) + mainScript, err := io.ReadAll(file) if err != nil { return errors.Wrap(err, "could not read server main script") } @@ -68,7 +69,7 @@ func (h *Handler) Load(bdle bundle.Bundle) error { h.server.Stop() } - if err := server.Start(); err != nil { + if err := server.Start(ctx); err != nil { return errors.WithStack(err) } diff --git a/pkg/module/app/memory/module_test.go b/pkg/module/app/memory/module_test.go index a702aa8..100a1c8 100644 --- a/pkg/module/app/memory/module_test.go +++ b/pkg/module/app/memory/module_test.go @@ -3,7 +3,7 @@ package memory import ( "context" "fmt" - "io/ioutil" + "os" "testing" "forge.cadoles.com/arcad/edge/pkg/app" @@ -41,7 +41,7 @@ func TestAppModuleWithMemoryRepository(t *testing.T) { file := "testdata/app.js" - data, err := ioutil.ReadFile(file) + data, err := os.ReadFile(file) if err != nil { t.Fatal(err) } @@ -52,7 +52,8 @@ func TestAppModuleWithMemoryRepository(t *testing.T) { defer server.Stop() - if err := server.Start(); err != nil { + ctx := context.Background() + if err := server.Start(ctx); err != nil { t.Fatalf("%+v", errors.WithStack(err)) } } diff --git a/pkg/module/auth/module_test.go b/pkg/module/auth/module_test.go index a7fb233..e127add 100644 --- a/pkg/module/auth/module_test.go +++ b/pkg/module/auth/module_test.go @@ -42,7 +42,8 @@ func TestAuthModule(t *testing.T) { t.Fatal(err) } - if err := server.Start(); err != nil { + ctx := context.Background() + if err := server.Start(ctx); err != nil { t.Fatalf("%+v", errors.WithStack(err)) } @@ -70,7 +71,7 @@ func TestAuthModule(t *testing.T) { req.Header.Add("Authorization", "Bearer "+string(rawToken)) - ctx := context.WithValue(context.Background(), edgeHTTP.ContextKeyOriginRequest, req) + ctx = context.WithValue(context.Background(), edgeHTTP.ContextKeyOriginRequest, req) if _, err := server.ExecFuncByName(ctx, "testAuth", ctx); err != nil { t.Fatalf("%+v", errors.WithStack(err)) @@ -98,7 +99,8 @@ func TestAuthAnonymousModule(t *testing.T) { t.Fatal(err) } - if err := server.Start(); err != nil { + ctx := context.Background() + if err := server.Start(ctx); err != nil { t.Fatalf("%+v", errors.WithStack(err)) } @@ -109,7 +111,7 @@ func TestAuthAnonymousModule(t *testing.T) { t.Fatalf("%+v", errors.WithStack(err)) } - ctx := context.WithValue(context.Background(), edgeHTTP.ContextKeyOriginRequest, req) + ctx = context.WithValue(context.Background(), edgeHTTP.ContextKeyOriginRequest, req) if _, err := server.ExecFuncByName(ctx, "testAuth", ctx); err != nil { t.Fatalf("%+v", errors.WithStack(err)) diff --git a/pkg/module/blob/module_test.go b/pkg/module/blob/module_test.go index aed1a23..7c89bb3 100644 --- a/pkg/module/blob/module_test.go +++ b/pkg/module/blob/module_test.go @@ -1,6 +1,7 @@ package blob import ( + "context" "os" "testing" @@ -38,7 +39,8 @@ func TestBlobModule(t *testing.T) { defer server.Stop() - if err := server.Start(); err != nil { + ctx := context.Background() + if err := server.Start(ctx); err != nil { t.Fatalf("%+v", errors.WithStack(err)) } } diff --git a/pkg/module/cast/module_test.go b/pkg/module/cast/module_test.go index dfdc041..37c298f 100644 --- a/pkg/module/cast/module_test.go +++ b/pkg/module/cast/module_test.go @@ -40,7 +40,8 @@ func TestCastModule(t *testing.T) { t.Fatal(err) } - if err := server.Start(); err != nil { + ctx := context.Background() + if err := server.Start(ctx); err != nil { t.Fatalf("%+v", errors.WithStack(err)) } @@ -74,7 +75,8 @@ func TestCastModuleRefreshDevices(t *testing.T) { t.Fatal(err) } - if err := server.Start(); err != nil { + ctx := context.Background() + if err := server.Start(ctx); err != nil { t.Fatalf("%+v", errors.WithStack(err)) } diff --git a/pkg/module/fetch/module_test.go b/pkg/module/fetch/module_test.go index 60b1737..a9e8c49 100644 --- a/pkg/module/fetch/module_test.go +++ b/pkg/module/fetch/module_test.go @@ -39,7 +39,8 @@ func TestFetchModule(t *testing.T) { defer server.Stop() - if err := server.Start(); err != nil { + ctx := context.Background() + if err := server.Start(ctx); err != nil { t.Fatalf("%+v", errors.WithStack(err)) } diff --git a/pkg/module/lifecycle.go b/pkg/module/lifecycle.go index 5bc6b6d..964433a 100644 --- a/pkg/module/lifecycle.go +++ b/pkg/module/lifecycle.go @@ -18,10 +18,10 @@ func (m *LifecycleModule) Name() string { func (m *LifecycleModule) Export(export *goja.Object) { } -func (m *LifecycleModule) OnInit(rt *goja.Runtime) (err error) { +func (m *LifecycleModule) OnInit(ctx context.Context, rt *goja.Runtime) (err error) { call, ok := goja.AssertFunction(rt.Get("onInit")) if !ok { - logger.Warn(context.Background(), "could not find onInit() function") + logger.Warn(ctx, "could not find onInit() function") return nil } @@ -30,9 +30,9 @@ func (m *LifecycleModule) OnInit(rt *goja.Runtime) (err error) { if recovered := recover(); recovered != nil { revoveredErr, ok := recovered.(error) if ok { - logger.Error(context.Background(), "recovered runtime error", logger.E(errors.WithStack(revoveredErr))) + logger.Error(ctx, "recovered runtime error", logger.E(errors.WithStack(revoveredErr))) - err = errors.WithStack(app.ErUnknownError) + err = errors.WithStack(app.ErrUnknownError) return } diff --git a/pkg/module/rpc.go b/pkg/module/rpc.go index c88741e..36694dc 100644 --- a/pkg/module/rpc.go +++ b/pkg/module/rpc.go @@ -51,8 +51,8 @@ func (m *RPCModule) Export(export *goja.Object) { } } -func (m *RPCModule) OnInit(rt *goja.Runtime) error { - go m.handleMessages() +func (m *RPCModule) OnInit(ctx context.Context, rt *goja.Runtime) error { + go m.handleMessages(ctx) return nil } @@ -92,9 +92,7 @@ func (m *RPCModule) unregister(call goja.FunctionCall, rt *goja.Runtime) goja.Va return nil } -func (m *RPCModule) handleMessages() { - ctx := context.Background() - +func (m *RPCModule) handleMessages(ctx context.Context) { clientMessages, err := m.bus.Subscribe(ctx, MessageNamespaceClient) if err != nil { panic(errors.WithStack(err)) diff --git a/pkg/module/share/module_test.go b/pkg/module/share/module_test.go index 9a70419..587d03e 100644 --- a/pkg/module/share/module_test.go +++ b/pkg/module/share/module_test.go @@ -37,7 +37,8 @@ func TestModule(t *testing.T) { t.Fatalf("%+v", errors.WithStack(err)) } - if err := server.Start(); err != nil { + ctx := context.Background() + if err := server.Start(ctx); err != nil { t.Fatalf("%+v", errors.WithStack(err)) } diff --git a/pkg/module/store/module_test.go b/pkg/module/store/module_test.go index 1e46137..080c9f2 100644 --- a/pkg/module/store/module_test.go +++ b/pkg/module/store/module_test.go @@ -31,7 +31,8 @@ func TestStoreModule(t *testing.T) { t.Fatalf("%+v", errors.WithStack(err)) } - if err := server.Start(); err != nil { + ctx := context.Background() + if err := server.Start(ctx); err != nil { t.Fatalf("%+v", errors.WithStack(err)) }