Small cleanup on one-time x hooks

This commit is contained in:
Tim Hockin 2022-01-19 11:37:22 -08:00
parent 9eed6946b7
commit c143bfd31a
2 changed files with 25 additions and 33 deletions

View File

@ -544,10 +544,6 @@ func main() {
// Startup webhooks goroutine // Startup webhooks goroutine
var webhookRunner *hook.HookRunner var webhookRunner *hook.HookRunner
if *flWebhookURL != "" { if *flWebhookURL != "" {
var webhookChannel chan bool
if *flOneTime {
webhookChannel = make(chan bool, 1)
}
webhook := hook.NewWebhook( webhook := hook.NewWebhook(
*flWebhookURL, *flWebhookURL,
*flWebhookMethod, *flWebhookMethod,
@ -560,7 +556,7 @@ func main() {
*flWebhookBackoff, *flWebhookBackoff,
hook.NewHookData(), hook.NewHookData(),
log, log,
webhookChannel, *flOneTime,
) )
go webhookRunner.Run(context.Background()) go webhookRunner.Run(context.Background())
} }
@ -568,10 +564,6 @@ func main() {
// Startup exechooks goroutine // Startup exechooks goroutine
var exechookRunner *hook.HookRunner var exechookRunner *hook.HookRunner
if *flExechookCommand != "" { if *flExechookCommand != "" {
var exechookChannel chan bool
if *flOneTime {
exechookChannel = make(chan bool, 1)
}
exechook := hook.NewExechook( exechook := hook.NewExechook(
cmd.NewRunner(log), cmd.NewRunner(log),
*flExechookCommand, *flExechookCommand,
@ -585,7 +577,7 @@ func main() {
*flExechookBackoff, *flExechookBackoff,
hook.NewHookData(), hook.NewHookData(),
log, log,
exechookChannel, *flOneTime,
) )
go exechookRunner.Run(context.Background()) go exechookRunner.Run(context.Background())
} }
@ -639,14 +631,12 @@ func main() {
// least one value before getting closed // least one value before getting closed
exitCode := 0 // is 0 if all hooks succeed, else is 1 exitCode := 0 // is 0 if all hooks succeed, else is 1
if exechookRunner != nil { if exechookRunner != nil {
if err = exechookRunner.WaitForCompletion(); err != nil { if err := exechookRunner.WaitForCompletion(); err != nil {
log.Error(err, "exechook completed with error")
exitCode = 1 exitCode = 1
} }
} }
if webhookRunner != nil { if webhookRunner != nil {
if err = webhookRunner.WaitForCompletion(); err != nil { if err := webhookRunner.WaitForCompletion(); err != nil {
log.Error(err, "webhook completed with error")
exitCode = 1 exitCode = 1
} }
} }

View File

@ -84,8 +84,12 @@ func (d *hookData) send(newHash string) {
} }
// NewHookRunner returns a new HookRunner // NewHookRunner returns a new HookRunner
func NewHookRunner(hook Hook, backoff time.Duration, data *hookData, log *logging.Logger, hasSucceededOnce chan bool) *HookRunner { func NewHookRunner(hook Hook, backoff time.Duration, data *hookData, log *logging.Logger, oneTime bool) *HookRunner {
return &HookRunner{hook: hook, backoff: backoff, data: data, logger: log, hasCompletedOnce: hasSucceededOnce} hr := &HookRunner{hook: hook, backoff: backoff, data: data, logger: log}
if oneTime {
hr.oneTimeResult = make(chan bool, 1)
}
return hr
} }
// HookRunner struct // HookRunner struct
@ -98,11 +102,9 @@ type HookRunner struct {
data *hookData data *hookData
// Logger // Logger
logger *logging.Logger logger *logging.Logger
// hasCompletedOnce is used to send true if and only if first run executed // Used to send a status result when running in one-time mode.
// successfully and false otherwise to some receiver. Should be // Should be initialised to a buffered channel of size 1.
// initialised to a buffered channel of size 1. oneTimeResult chan bool
// Is only meant for use within sendCompletedOnceMessageIfApplicable.
hasCompletedOnce chan bool
} }
// Send sends hash to hookdata // Send sends hash to hookdata
@ -131,27 +133,27 @@ func (r *HookRunner) Run(ctx context.Context) {
r.logger.Error(err, "hook failed") r.logger.Error(err, "hook failed")
updateHookRunCountMetric(r.hook.Name(), "error") updateHookRunCountMetric(r.hook.Name(), "error")
// don't want to sleep unnecessarily terminating anyways // don't want to sleep unnecessarily terminating anyways
r.sendCompletedOnceMessageIfApplicable(false) r.sendOneTimeResultAndTerminate(false)
time.Sleep(r.backoff) time.Sleep(r.backoff)
} else { } else {
updateHookRunCountMetric(r.hook.Name(), "success") updateHookRunCountMetric(r.hook.Name(), "success")
lastHash = hash lastHash = hash
r.sendCompletedOnceMessageIfApplicable(true) r.sendOneTimeResultAndTerminate(true)
break break
} }
} }
} }
} }
// If hasCompletedOnce is nil, does nothing. Otherwise, forwards the caller // If oneTimeResult is nil, does nothing. Otherwise, forwards the caller
// provided success status (as a boolean) of HookRunner to receivers of // provided success status (as a boolean) of HookRunner to receivers of
// hasCompletedOnce, closes said chanel, and terminates this goroutine. // oneTimeResult, closes said chanel, and terminates this goroutine.
// Using this function to write to hasCompletedOnce ensures it is only ever // Using this function to write to oneTimeResult ensures it is only ever
// written to once. // written to once.
func (r *HookRunner) sendCompletedOnceMessageIfApplicable(completedSuccessfully bool) { func (r *HookRunner) sendOneTimeResultAndTerminate(completedSuccessfully bool) {
if r.hasCompletedOnce != nil { if r.oneTimeResult != nil {
r.hasCompletedOnce <- completedSuccessfully r.oneTimeResult <- completedSuccessfully
close(r.hasCompletedOnce) close(r.oneTimeResult)
runtime.Goexit() runtime.Goexit()
} }
} }
@ -159,15 +161,15 @@ func (r *HookRunner) sendCompletedOnceMessageIfApplicable(completedSuccessfully
// WaitForCompletion waits for HookRunner to send completion message to // WaitForCompletion waits for HookRunner to send completion message to
// calling thread and returns either true if HookRunner executed successfully // calling thread and returns either true if HookRunner executed successfully
// and some error otherwise. // and some error otherwise.
// Assumes that r.hasCompletedOnce is not nil, but if it is, returns an error // Assumes that r.oneTimeResult is not nil, but if it is, returns an error
func (r *HookRunner) WaitForCompletion() error { func (r *HookRunner) WaitForCompletion() error {
// Make sure function should be called // Make sure function should be called
if r.hasCompletedOnce == nil { if r.oneTimeResult == nil {
return fmt.Errorf("HookRunner.WaitForCompletion called on async runner") return fmt.Errorf("HookRunner.WaitForCompletion called on async runner")
} }
// Perform wait on HookRunner // Perform wait on HookRunner
hookRunnerFinishedSuccessfully := <-r.hasCompletedOnce hookRunnerFinishedSuccessfully := <-r.oneTimeResult
if !hookRunnerFinishedSuccessfully { if !hookRunnerFinishedSuccessfully {
return fmt.Errorf("hook completed with error") return fmt.Errorf("hook completed with error")
} }