feat(lifecycle): execute onInit func asynchronously
arcad/edge/pipeline/head This commit looks good Details

This commit is contained in:
wpetit 2023-10-22 10:47:44 +02:00
parent 0cfb132b65
commit 22a3326be9
1 changed files with 4 additions and 22 deletions

View File

@ -2,7 +2,6 @@ package module
import ( import (
"context" "context"
"time"
"forge.cadoles.com/arcad/edge/pkg/app" "forge.cadoles.com/arcad/edge/pkg/app"
"github.com/dop251/goja" "github.com/dop251/goja"
@ -20,33 +19,16 @@ func (m *LifecycleModule) Export(export *goja.Object) {
} }
func (m *LifecycleModule) OnInit(ctx context.Context, rt *goja.Runtime) (err error) { func (m *LifecycleModule) OnInit(ctx context.Context, rt *goja.Runtime) (err error) {
call, ok := goja.AssertFunction(rt.Get("onInit")) _, ok := goja.AssertFunction(rt.Get("onInit"))
if !ok { if !ok {
logger.Warn(ctx, "could not find onInit() function") logger.Warn(ctx, "could not find onInit() function")
return nil return nil
} }
defer func() { if _, err := rt.RunString("setTimeout(onInit, 0)"); err != nil {
if recovered := recover(); recovered != nil { return errors.WithStack(err)
revoveredErr, ok := recovered.(error) }
if ok {
logger.Error(ctx, "recovered runtime error", logger.CapturedE(errors.WithStack(revoveredErr)))
err = errors.WithStack(app.ErrUnknownError)
return
}
panic(recovered)
}
}()
logger.Debug(ctx, "executing app onInit() function")
start := time.Now()
call(nil)
duration := time.Since(start)
logger.Debug(ctx, "executed app onInit() function", logger.F("duration", duration.String()))
return nil return nil
} }