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 <caicandong@noreply.gitea.com>
Co-committed-by: caicandong <caicandong@noreply.gitea.com>
pull/71/head
caicandong 2023-07-24 04:28:44 +00:00 committed by Jason Song
parent 1a7ec5f339
commit a1bb3b56fd
1 changed files with 23 additions and 11 deletions

View File

@ -6,6 +6,7 @@ package poll
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"sync" "sync"
runnerv1 "code.gitea.io/actions-proto-go/runner/v1" runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
@ -45,6 +46,18 @@ func (p *Poller) Poll(ctx context.Context) {
func (p *Poller) poll(ctx context.Context, wg *sync.WaitGroup, limiter *rate.Limiter) { func (p *Poller) poll(ctx context.Context, wg *sync.WaitGroup, limiter *rate.Limiter) {
defer wg.Done() defer wg.Done()
for { for {
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")
}
}()
if err := limiter.Wait(ctx); err != nil { if err := limiter.Wait(ctx); err != nil {
if ctx.Err() != nil { if ctx.Err() != nil {
log.WithError(err).Debug("limiter wait failed") log.WithError(err).Debug("limiter wait failed")
@ -53,12 +66,11 @@ func (p *Poller) poll(ctx context.Context, wg *sync.WaitGroup, limiter *rate.Lim
} }
task, ok := p.fetchTask(ctx) task, ok := p.fetchTask(ctx)
if !ok { if !ok {
continue return
} }
if err := p.runner.Run(ctx, task); err != nil { if err := p.runner.Run(ctx, task); err != nil {
log.WithError(err).Error("failed to run task") log.WithError(err).Error("failed to run task")
} }
}
} }
func (p *Poller) fetchTask(ctx context.Context) (*runnerv1.Task, bool) { func (p *Poller) fetchTask(ctx context.Context) (*runnerv1.Task, bool) {