Exit MonitorEvaluation once stream is closed

As we pass the context to the Nomad API event stream, they close
the event stream once the passed context is cancelled. We use this
to exit our receive loop on the event stream once the stream is closed,
instead of having to check the context manually.
This commit is contained in:
sirkrypt0
2021-05-27 11:35:37 +02:00
committed by Tobias Kantusch
parent a891d72c4f
commit 6084b00e23
2 changed files with 26 additions and 29 deletions

View File

@@ -66,17 +66,12 @@ func (a *ApiClient) LoadRunners(jobId string) (runnerIds []string, err error) {
}
func (a *ApiClient) MonitorEvaluation(evalID string, ctx context.Context) error {
var events *nomadApi.Events
stream, err := a.EvaluationStream(evalID, ctx)
if err != nil {
return err
}
for {
select {
case events = <-stream:
case <-ctx.Done():
return nil
}
// If ctx is cancelled, the stream will be closed by Nomad and we exit the for loop.
for events := range stream {
if events.IsHeartbeat() {
continue
}
@@ -97,6 +92,7 @@ func (a *ApiClient) MonitorEvaluation(evalID string, ctx context.Context) error
}
}
}
return nil
}
// checkEvaluation checks whether the given evaluation failed.