From a1bb3b56fd13312d03a4ea66176a5c46d415fe9b Mon Sep 17 00:00:00 2001 From: caicandong Date: Mon, 24 Jul 2023 04:28:44 +0000 Subject: [PATCH] Catch the panic and print the error (#305) refactor # 215 Catch the panic and print the error close #215 Co-authored-by: CaiCandong <1290147055@qq.com> Reviewed-on: https://gitea.com/gitea/act_runner/pulls/305 Co-authored-by: caicandong Co-committed-by: caicandong --- internal/app/poll/poller.go | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/internal/app/poll/poller.go b/internal/app/poll/poller.go index 913e464..2c7a5e8 100644 --- a/internal/app/poll/poller.go +++ b/internal/app/poll/poller.go @@ -6,6 +6,7 @@ package poll import ( "context" "errors" + "fmt" "sync" runnerv1 "code.gitea.io/actions-proto-go/runner/v1" @@ -45,19 +46,30 @@ func (p *Poller) Poll(ctx context.Context) { func (p *Poller) poll(ctx context.Context, wg *sync.WaitGroup, limiter *rate.Limiter) { defer wg.Done() for { - if err := limiter.Wait(ctx); err != nil { - if ctx.Err() != nil { - log.WithError(err).Debug("limiter wait failed") - } - return + p.pollTaskWithRateLimit(ctx, limiter) + } +} + +func (p *Poller) pollTaskWithRateLimit(ctx context.Context, limiter *rate.Limiter) { + defer func() { + if r := recover(); r != nil { + err := fmt.Errorf("panic: %v", r) + log.WithError(err).Error("panic in pollTaskWithRateLimit") } - task, ok := p.fetchTask(ctx) - if !ok { - continue - } - if err := p.runner.Run(ctx, task); err != nil { - log.WithError(err).Error("failed to run task") + }() + + if err := limiter.Wait(ctx); err != nil { + if ctx.Err() != nil { + log.WithError(err).Debug("limiter wait failed") } + return + } + task, ok := p.fetchTask(ctx) + if !ok { + return + } + if err := p.runner.Run(ctx, task); err != nil { + log.WithError(err).Error("failed to run task") } }