fix endless loop (#306)

fix endless loop in  poll
relate #305

Co-authored-by: CaiCandong <1290147055@qq.com>
Reviewed-on: https://gitea.com/gitea/act_runner/pulls/306
Co-authored-by: caicandong <caicandong@noreply.gitea.com>
Co-committed-by: caicandong <caicandong@noreply.gitea.com>
This commit is contained in:
caicandong 2023-07-24 07:07:53 +00:00 committed by Jason Song
parent 8f88e4f15a
commit 49a2fcc138

View File

@ -46,18 +46,6 @@ 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")
@ -66,8 +54,20 @@ func (p *Poller) pollTaskWithRateLimit(ctx context.Context, limiter *rate.Limite
} }
task, ok := p.fetchTask(ctx) task, ok := p.fetchTask(ctx)
if !ok { if !ok {
return continue
} }
p.runTaskWithRecover(ctx, task)
}
}
func (p *Poller) runTaskWithRecover(ctx context.Context, task *runnerv1.Task) {
defer func() {
if r := recover(); r != nil {
err := fmt.Errorf("panic: %v", r)
log.WithError(err).Error("panic in runTaskWithRecover")
}
}()
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")
} }