3
0

Add ability to disable script runner calls

With script runner disabled, all critical scripts would fail
This commit is contained in:
Denis Arh 2019-07-31 16:46:10 +02:00
parent ecc73e10fc
commit 94e2acfc17
2 changed files with 23 additions and 2 deletions

View File

@ -49,13 +49,18 @@ type (
)
// @todo move to opt so all services can use it
func ScriptRunner(c options.ScriptRunnerOpt) (*scriptRunner, error) {
var svc = &scriptRunner{
func ScriptRunner(c options.ScriptRunnerOpt) (svc *scriptRunner, err error) {
svc = &scriptRunner{
c: c,
logger: DefaultLogger.Named("script-runner"),
jwtEncoder: auth.DefaultJwtHandler,
}
if !c.Enabled {
// Do not connect when script runner is not enabled
return
}
return svc, svc.connect()
}
@ -128,6 +133,20 @@ func (svc scriptRunner) Record(ctx context.Context, s Runnable, ns *types.Namesp
return nil, errors.New("module not provided")
}
if !svc.c.Enabled {
if s.IsCritical() {
// Oh dear, we are in quite a pickle:
// Script runner is disabled but we have critical script to run
return nil, errors.New("script runner disabled")
}
// Log this
svc.logger.Debug("executing script", zap.Any("record", r))
// and pretend like nothing happened
return r, nil
}
svc.logger.Debug("executing script", zap.Any("record", r))
ctx, cancelFn := context.WithTimeout(ctx, time.Second*5)

View File

@ -6,6 +6,7 @@ import (
type (
ScriptRunnerOpt struct {
Enabled bool `env:"SCRIPT_RUNNER_ENABLED"`
Addr string `env:"SCRIPT_RUNNER_ADDR"`
MaxBackoffDelay time.Duration `env:"SCRIPT_RUNNER_MAX_BACKOFF_DELAY"`
Log bool `env:"SCRIPT_RUNNER_LOG"`
@ -14,6 +15,7 @@ type (
func ScriptRunner(pfix string) (o *ScriptRunnerOpt) {
o = &ScriptRunnerOpt{
Enabled: false,
Addr: "corredor:80",
MaxBackoffDelay: time.Minute,
Log: false,