feat(app): pass context to start process

This commit is contained in:
wpetit 2023-10-19 20:05:59 +02:00
parent 4d064de164
commit efb8ba8b99
13 changed files with 46 additions and 35 deletions

View File

@ -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")
}

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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))
}
}

View File

@ -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))

View File

@ -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))
}
}

View File

@ -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))
}

View File

@ -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))
}

View File

@ -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
}

View File

@ -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))

View File

@ -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))
}

View File

@ -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))
}