edge/pkg/module/lifecycle.go

58 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-02-09 12:16:36 +01:00
package module
import (
"context"
"forge.cadoles.com/arcad/edge/pkg/app"
"github.com/dop251/goja"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger"
)
type LifecycleModule struct{}
2023-02-09 12:16:36 +01:00
func (m *LifecycleModule) Name() string {
return "lifecycle"
}
func (m *LifecycleModule) Export(export *goja.Object) {
}
func (m *LifecycleModule) OnInit(rt *goja.Runtime) (err error) {
call, ok := goja.AssertFunction(rt.Get("onInit"))
if !ok {
logger.Warn(context.Background(), "could not find onInit() function")
return nil
}
defer func() {
if recovered := recover(); recovered != nil {
revoveredErr, ok := recovered.(error)
if ok {
logger.Error(context.Background(), "recovered runtime error", logger.E(errors.WithStack(revoveredErr)))
2023-02-09 12:16:36 +01:00
err = errors.WithStack(app.ErUnknownError)
return
}
panic(recovered)
2023-02-09 12:16:36 +01:00
}
}()
2023-02-09 12:16:36 +01:00
call(nil)
2023-02-09 12:16:36 +01:00
return nil
}
func LifecycleModuleFactory() app.ServerModuleFactory {
2023-02-09 12:16:36 +01:00
return func(server *app.Server) app.ServerModule {
module := &LifecycleModule{}
2023-02-09 12:16:36 +01:00
return module
}
}
var _ app.InitializableModule = &LifecycleModule{}