2023-04-04 08:32:04 -05:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package poll
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2023-07-23 23:28:44 -05:00
|
|
|
"fmt"
|
2023-04-04 08:32:04 -05:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
|
|
|
|
"github.com/bufbuild/connect-go"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/time/rate"
|
|
|
|
|
|
|
|
"gitea.com/gitea/act_runner/internal/app/run"
|
|
|
|
"gitea.com/gitea/act_runner/internal/pkg/client"
|
|
|
|
"gitea.com/gitea/act_runner/internal/pkg/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Poller struct {
|
2023-04-05 21:57:36 -05:00
|
|
|
client client.Client
|
|
|
|
runner *run.Runner
|
|
|
|
cfg *config.Config
|
2023-04-04 08:32:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(cfg *config.Config, client client.Client, runner *run.Runner) *Poller {
|
|
|
|
return &Poller{
|
2023-04-05 21:57:36 -05:00
|
|
|
client: client,
|
|
|
|
runner: runner,
|
|
|
|
cfg: cfg,
|
2023-04-04 08:32:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Poller) Poll(ctx context.Context) {
|
2023-04-05 21:57:36 -05:00
|
|
|
limiter := rate.NewLimiter(rate.Every(p.cfg.Runner.FetchInterval), 1)
|
2023-04-04 08:32:04 -05:00
|
|
|
wg := &sync.WaitGroup{}
|
2023-04-05 21:57:36 -05:00
|
|
|
for i := 0; i < p.cfg.Runner.Capacity; i++ {
|
2023-04-04 08:32:04 -05:00
|
|
|
wg.Add(1)
|
|
|
|
go p.poll(ctx, wg, limiter)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Poller) poll(ctx context.Context, wg *sync.WaitGroup, limiter *rate.Limiter) {
|
|
|
|
defer wg.Done()
|
|
|
|
for {
|
2023-07-23 23:28:44 -05:00
|
|
|
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")
|
2023-04-04 08:32:04 -05:00
|
|
|
}
|
2023-07-23 23:28:44 -05:00
|
|
|
}()
|
|
|
|
|
|
|
|
if err := limiter.Wait(ctx); err != nil {
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
log.WithError(err).Debug("limiter wait failed")
|
2023-04-04 08:32:04 -05:00
|
|
|
}
|
2023-07-23 23:28:44 -05:00
|
|
|
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")
|
2023-04-04 08:32:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Poller) fetchTask(ctx context.Context) (*runnerv1.Task, bool) {
|
2023-04-05 21:57:36 -05:00
|
|
|
reqCtx, cancel := context.WithTimeout(ctx, p.cfg.Runner.FetchTimeout)
|
2023-04-04 08:32:04 -05:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
resp, err := p.client.FetchTask(reqCtx, connect.NewRequest(&runnerv1.FetchTaskRequest{}))
|
|
|
|
if errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("failed to fetch task")
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2023-04-06 08:51:46 -05:00
|
|
|
if resp == nil || resp.Msg == nil || resp.Msg.Task == nil {
|
2023-04-04 08:32:04 -05:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return resp.Msg.Task, true
|
|
|
|
}
|