2022-10-06 12:23:59 +00:00
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/dop251/goja"
|
|
|
|
"github.com/dop251/goja_nodejs/require"
|
|
|
|
"github.com/zitadel/logging"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxPrepareTimeout = 5 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
type Option func(*runConfig)
|
|
|
|
|
|
|
|
func WithAllowedToFail() Option {
|
|
|
|
return func(c *runConfig) {
|
|
|
|
c.allowedToFail = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type runConfig struct {
|
|
|
|
allowedToFail bool
|
2023-02-15 01:52:11 +00:00
|
|
|
functionTimeout,
|
|
|
|
scriptTimeout time.Duration
|
|
|
|
modules map[string]require.ModuleLoader
|
|
|
|
logger *logger
|
|
|
|
instanceID string
|
|
|
|
vm *goja.Runtime
|
|
|
|
ctxParam *ctxConfig
|
|
|
|
apiParam *apiConfig
|
2022-10-06 12:23:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newRunConfig(ctx context.Context, opts ...Option) *runConfig {
|
|
|
|
deadline, ok := ctx.Deadline()
|
|
|
|
if !ok {
|
|
|
|
logging.Warn("no timeout set on action run")
|
|
|
|
}
|
|
|
|
|
|
|
|
vm := goja.New()
|
|
|
|
vm.SetFieldNameMapper(goja.UncapFieldNameMapper())
|
|
|
|
|
|
|
|
config := &runConfig{
|
2023-02-15 01:52:11 +00:00
|
|
|
functionTimeout: time.Until(deadline),
|
|
|
|
scriptTimeout: maxPrepareTimeout,
|
|
|
|
modules: map[string]require.ModuleLoader{},
|
|
|
|
vm: vm,
|
2022-10-06 12:23:59 +00:00
|
|
|
ctxParam: &ctxConfig{
|
|
|
|
FieldConfig: FieldConfig{
|
|
|
|
Runtime: vm,
|
|
|
|
fields: fields{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
apiParam: &apiConfig{
|
|
|
|
FieldConfig: FieldConfig{
|
|
|
|
Runtime: vm,
|
|
|
|
fields: fields{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(config)
|
|
|
|
}
|
|
|
|
|
2023-02-15 01:52:11 +00:00
|
|
|
if config.scriptTimeout > config.functionTimeout {
|
|
|
|
config.scriptTimeout = config.functionTimeout
|
2022-10-06 12:23:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2023-02-15 01:52:11 +00:00
|
|
|
func (c *runConfig) StartFunction() *time.Timer {
|
2022-10-06 12:23:59 +00:00
|
|
|
c.vm.ClearInterrupt()
|
2023-02-15 01:52:11 +00:00
|
|
|
return time.AfterFunc(c.functionTimeout, func() {
|
2022-10-06 12:23:59 +00:00
|
|
|
c.vm.Interrupt(ErrHalt)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-15 01:52:11 +00:00
|
|
|
func (c *runConfig) StartScript() *time.Timer {
|
2022-10-06 12:23:59 +00:00
|
|
|
c.vm.ClearInterrupt()
|
2023-02-15 01:52:11 +00:00
|
|
|
return time.AfterFunc(c.scriptTimeout, func() {
|
2022-10-06 12:23:59 +00:00
|
|
|
c.vm.Interrupt(ErrHalt)
|
|
|
|
})
|
|
|
|
}
|
2023-02-15 01:52:11 +00:00
|
|
|
|
|
|
|
func (c *runConfig) cutTimeouts(remainingSeconds *uint64) {
|
|
|
|
if remainingSeconds == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
remainingDur := time.Duration(*remainingSeconds) * time.Second
|
|
|
|
if c.functionTimeout > remainingDur {
|
|
|
|
c.functionTimeout = remainingDur
|
|
|
|
}
|
|
|
|
if c.scriptTimeout > remainingDur {
|
|
|
|
c.scriptTimeout = remainingDur
|
|
|
|
}
|
|
|
|
}
|