From 396c173c53a014f6e8303931a0c100c08677647b Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 19 Apr 2023 20:47:36 +0300 Subject: rename execute to runChecks --- internal/check.go | 6 +++--- internal/execute.go | 59 --------------------------------------------------- internal/run.go | 4 ++-- internal/runchecks.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 64 deletions(-) delete mode 100644 internal/execute.go create mode 100644 internal/runchecks.go diff --git a/internal/check.go b/internal/check.go index 44e878d..8a554ee 100644 --- a/internal/check.go +++ b/internal/check.go @@ -23,7 +23,7 @@ type checkResult struct { status nagiosCode } -func (c check) execute(ctx context.Context, name string) checkResult { +func (c check) run(ctx context.Context, name string) checkResult { cmd := exec.CommandContext(ctx, c.Plugin, c.Args...) var bytes bytes.Buffer @@ -43,6 +43,6 @@ func (c check) execute(ctx context.Context, name string) checkResult { return checkResult{name, output, nagiosCode(cmd.ProcessState.ExitCode())} } -func (c namedCheck) execute(ctx context.Context) checkResult { - return c.check.execute(ctx, c.name) +func (c namedCheck) run(ctx context.Context) checkResult { + return c.check.run(ctx, c.name) } diff --git a/internal/execute.go b/internal/execute.go deleted file mode 100644 index 1029ff7..0000000 --- a/internal/execute.go +++ /dev/null @@ -1,59 +0,0 @@ -package internal - -import ( - "context" - "log" - "sync" - "time" -) - -func execute(globalCtx context.Context, state state, config config) state { - limiterCh := make(chan struct{}, config.CheckConcurrency) - inputCh := make(chan namedCheck) - outputCh := make(chan checkResult) - - go func() { - for name, check := range config.Checks { - inputCh <- namedCheck{check, name} - } - close(inputCh) - }() - - var outputWg sync.WaitGroup - outputWg.Add(1) - - go func() { - for checkResult := range outputCh { - state.update(checkResult) - } - outputWg.Done() - }() - - var inputWg sync.WaitGroup - inputWg.Add(len(config.Checks)) - - for check := range inputCh { - go func(check namedCheck) { - limiterCh <- struct{}{} - defer func() { - <-limiterCh - inputWg.Done() - }() - - ctx, cancel := context.WithTimeout(globalCtx, - time.Duration(config.CheckTimeoutS)*time.Second) - defer cancel() - - outputCh <- check.execute(ctx) - }(check) - } - - inputWg.Wait() - log.Println("All checks completed!") - close(outputCh) - - outputWg.Wait() - log.Println("All outputs collected!") - - return state -} diff --git a/internal/run.go b/internal/run.go index ef9919d..e647f7e 100644 --- a/internal/run.go +++ b/internal/run.go @@ -2,7 +2,7 @@ package internal import "context" -func Run(cts context.Context, configFile string) { +func Run(ctx context.Context, configFile string) { config, err := newConfig(configFile) if err != nil { panic(err) @@ -13,7 +13,7 @@ func Run(cts context.Context, configFile string) { notifyError(config, err) } - state = execute(ctx, state, config) + state = runChecks(ctx, state, config) if err := state.persist(); err != nil { notifyError(config, err) diff --git a/internal/runchecks.go b/internal/runchecks.go new file mode 100644 index 0000000..360a1b5 --- /dev/null +++ b/internal/runchecks.go @@ -0,0 +1,59 @@ +package internal + +import ( + "context" + "log" + "sync" + "time" +) + +func runChecks(globalCtx context.Context, state state, config config) state { + limiterCh := make(chan struct{}, config.CheckConcurrency) + inputCh := make(chan namedCheck) + outputCh := make(chan checkResult) + + go func() { + for name, check := range config.Checks { + inputCh <- namedCheck{check, name} + } + close(inputCh) + }() + + var outputWg sync.WaitGroup + outputWg.Add(1) + + go func() { + for checkResult := range outputCh { + state.update(checkResult) + } + outputWg.Done() + }() + + var inputWg sync.WaitGroup + inputWg.Add(len(config.Checks)) + + for check := range inputCh { + go func(check namedCheck) { + limiterCh <- struct{}{} + defer func() { + <-limiterCh + inputWg.Done() + }() + + ctx, cancel := context.WithTimeout(globalCtx, + time.Duration(config.CheckTimeoutS)*time.Second) + defer cancel() + + outputCh <- check.run(ctx) + }(check) + } + + inputWg.Wait() + log.Println("All checks completed!") + close(outputCh) + + outputWg.Wait() + log.Println("All outputs collected!") + + return state +} -- cgit v1.2.3