mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-09-03 06:14:54 +00:00
fix(agent): handle context cancellation (#5323)
Signed-off-by: ivaltryek <meet.vasani86@gmail.com> Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
This commit is contained in:
@@ -267,6 +267,11 @@ func run(ctx context.Context, c *cli.Command, backends []types.Backend) error {
|
|||||||
err := client.ReportHealth(grpcCtx)
|
err := client.ReportHealth(grpcCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg("failed to report health")
|
log.Err(err).Msg("failed to report health")
|
||||||
|
// Check if the error is due to context cancellation
|
||||||
|
if grpcCtx.Err() != nil || agentCtx.Err() != nil {
|
||||||
|
log.Debug().Msg("terminating health reporting due to context cancellation")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
@@ -291,8 +296,18 @@ func run(ctx context.Context, c *cli.Command, backends []types.Backend) error {
|
|||||||
|
|
||||||
log.Debug().Msg("polling new steps")
|
log.Debug().Msg("polling new steps")
|
||||||
if err := runner.Run(agentCtx, shutdownCtx); err != nil {
|
if err := runner.Run(agentCtx, shutdownCtx); err != nil {
|
||||||
log.Error().Err(err).Msg("runner done with error")
|
log.Error().Err(err).Msg("runner error, retrying...")
|
||||||
return err
|
// Check if context is canceled
|
||||||
|
if agentCtx.Err() != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// Wait a bit before retrying to avoid hammering the server
|
||||||
|
select {
|
||||||
|
case <-agentCtx.Done():
|
||||||
|
return nil
|
||||||
|
case <-time.After(time.Second * 5):
|
||||||
|
// Continue to next iteration
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user