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"
|
|
|
|
)
|
|
|
|
|
2023-04-24 12:16:30 +02:00
|
|
|
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) {
|
|
|
|
}
|
|
|
|
|
2023-10-19 20:05:59 +02:00
|
|
|
func (m *LifecycleModule) OnInit(ctx context.Context, rt *goja.Runtime) (err error) {
|
2023-04-24 12:16:30 +02:00
|
|
|
call, ok := goja.AssertFunction(rt.Get("onInit"))
|
|
|
|
if !ok {
|
2023-10-19 20:05:59 +02:00
|
|
|
logger.Warn(ctx, "could not find onInit() function")
|
2023-04-24 12:16:30 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if recovered := recover(); recovered != nil {
|
|
|
|
revoveredErr, ok := recovered.(error)
|
|
|
|
if ok {
|
2023-10-19 21:47:09 +02:00
|
|
|
logger.Error(ctx, "recovered runtime error", logger.CapturedE(errors.WithStack(revoveredErr)))
|
2023-02-09 12:16:36 +01:00
|
|
|
|
2023-10-19 20:05:59 +02:00
|
|
|
err = errors.WithStack(app.ErrUnknownError)
|
2023-04-24 12:16:30 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(recovered)
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
2023-04-24 12:16:30 +02:00
|
|
|
}()
|
2023-02-09 12:16:36 +01:00
|
|
|
|
2023-04-24 12:16:30 +02:00
|
|
|
call(nil)
|
2023-02-09 12:16:36 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-24 14:40:28 +01:00
|
|
|
func LifecycleModuleFactory() app.ServerModuleFactory {
|
2023-02-09 12:16:36 +01:00
|
|
|
return func(server *app.Server) app.ServerModule {
|
2023-04-24 12:16:30 +02:00
|
|
|
module := &LifecycleModule{}
|
2023-02-09 12:16:36 +01:00
|
|
|
|
|
|
|
return module
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ app.InitializableModule = &LifecycleModule{}
|