2023-04-04 13:32:04 +00:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package poll
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2023-07-24 04:28:44 +00:00
|
|
|
"fmt"
|
2023-04-04 13:32:04 +00:00
|
|
|
"sync"
|
2023-07-25 03:25:50 +00:00
|
|
|
"sync/atomic"
|
2023-04-04 13:32:04 +00:00
|
|
|
|
|
|
|
runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
|
2024-07-31 04:02:21 +00:00
|
|
|
"connectrpc.com/connect"
|
2023-04-04 13:32:04 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2024-06-06 09:40:31 +00:00
|
|
|
const PollerID = "PollerID"
|
|
|
|
|
|
|
|
type Poller interface {
|
|
|
|
Poll()
|
|
|
|
Shutdown(ctx context.Context) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type poller struct {
|
2023-07-25 03:25:50 +00:00
|
|
|
client client.Client
|
2024-06-06 09:40:31 +00:00
|
|
|
runner run.RunnerInterface
|
2023-07-25 03:25:50 +00:00
|
|
|
cfg *config.Config
|
|
|
|
tasksVersion atomic.Int64 // tasksVersion used to store the version of the last task fetched from the Gitea.
|
2024-06-06 09:40:31 +00:00
|
|
|
|
|
|
|
pollingCtx context.Context
|
|
|
|
shutdownPolling context.CancelFunc
|
|
|
|
|
|
|
|
jobsCtx context.Context
|
|
|
|
shutdownJobs context.CancelFunc
|
|
|
|
|
|
|
|
done chan any
|
2023-04-04 13:32:04 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 09:40:31 +00:00
|
|
|
func New(cfg *config.Config, client client.Client, runner run.RunnerInterface) Poller {
|
|
|
|
return (&poller{}).init(cfg, client, runner)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *poller) init(cfg *config.Config, client client.Client, runner run.RunnerInterface) Poller {
|
|
|
|
pollingCtx, shutdownPolling := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
jobsCtx, shutdownJobs := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
done := make(chan any)
|
|
|
|
|
|
|
|
p.client = client
|
|
|
|
p.runner = runner
|
|
|
|
p.cfg = cfg
|
|
|
|
|
|
|
|
p.pollingCtx = pollingCtx
|
|
|
|
p.shutdownPolling = shutdownPolling
|
|
|
|
|
|
|
|
p.jobsCtx = jobsCtx
|
|
|
|
p.shutdownJobs = shutdownJobs
|
|
|
|
p.done = done
|
|
|
|
|
|
|
|
return p
|
2023-04-04 13:32:04 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 09:40:31 +00:00
|
|
|
func (p *poller) Poll() {
|
2023-04-06 02:57:36 +00:00
|
|
|
limiter := rate.NewLimiter(rate.Every(p.cfg.Runner.FetchInterval), 1)
|
2023-04-04 13:32:04 +00:00
|
|
|
wg := &sync.WaitGroup{}
|
2023-04-06 02:57:36 +00:00
|
|
|
for i := 0; i < p.cfg.Runner.Capacity; i++ {
|
2023-04-04 13:32:04 +00:00
|
|
|
wg.Add(1)
|
2024-06-06 09:40:31 +00:00
|
|
|
go p.poll(i, wg, limiter)
|
2023-04-04 13:32:04 +00:00
|
|
|
}
|
|
|
|
wg.Wait()
|
2024-06-06 09:40:31 +00:00
|
|
|
|
|
|
|
// signal the poller is finished
|
|
|
|
close(p.done)
|
2023-04-04 13:32:04 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 09:40:31 +00:00
|
|
|
func (p *poller) Shutdown(ctx context.Context) error {
|
|
|
|
p.shutdownPolling()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-p.done:
|
|
|
|
log.Trace("all jobs are complete")
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Trace("forcing the jobs to shutdown")
|
|
|
|
p.shutdownJobs()
|
|
|
|
<-p.done
|
|
|
|
log.Trace("all jobs have been shutdown")
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *poller) poll(id int, wg *sync.WaitGroup, limiter *rate.Limiter) {
|
|
|
|
log.Infof("[poller %d] launched", id)
|
2023-04-04 13:32:04 +00:00
|
|
|
defer wg.Done()
|
|
|
|
for {
|
2024-06-06 09:40:31 +00:00
|
|
|
if err := limiter.Wait(p.pollingCtx); err != nil {
|
|
|
|
log.Infof("[poller %d] shutdown", id)
|
2023-07-24 07:07:53 +00:00
|
|
|
return
|
|
|
|
}
|
2024-06-06 09:40:31 +00:00
|
|
|
task, ok := p.fetchTask(p.pollingCtx)
|
2023-07-24 07:07:53 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2024-06-06 09:40:31 +00:00
|
|
|
p.runTaskWithRecover(p.jobsCtx, task)
|
2023-07-24 04:28:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-06 09:40:31 +00:00
|
|
|
func (p *poller) runTaskWithRecover(ctx context.Context, task *runnerv1.Task) {
|
2023-07-24 04:28:44 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err := fmt.Errorf("panic: %v", r)
|
2023-07-24 07:07:53 +00:00
|
|
|
log.WithError(err).Error("panic in runTaskWithRecover")
|
2023-04-04 13:32:04 +00:00
|
|
|
}
|
2023-07-24 04:28:44 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
if err := p.runner.Run(ctx, task); err != nil {
|
|
|
|
log.WithError(err).Error("failed to run task")
|
2023-04-04 13:32:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-06 09:40:31 +00:00
|
|
|
func (p *poller) fetchTask(ctx context.Context) (*runnerv1.Task, bool) {
|
2023-04-06 02:57:36 +00:00
|
|
|
reqCtx, cancel := context.WithTimeout(ctx, p.cfg.Runner.FetchTimeout)
|
2023-04-04 13:32:04 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2023-07-25 03:25:50 +00:00
|
|
|
// Load the version value that was in the cache when the request was sent.
|
|
|
|
v := p.tasksVersion.Load()
|
|
|
|
resp, err := p.client.FetchTask(reqCtx, connect.NewRequest(&runnerv1.FetchTaskRequest{
|
|
|
|
TasksVersion: v,
|
|
|
|
}))
|
2023-04-04 13:32:04 +00:00
|
|
|
if errors.Is(err, context.DeadlineExceeded) {
|
2024-06-06 09:40:31 +00:00
|
|
|
log.Trace("deadline exceeded")
|
2023-04-04 13:32:04 +00:00
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2024-06-06 09:40:31 +00:00
|
|
|
if errors.Is(err, context.Canceled) {
|
|
|
|
log.WithError(err).Debugf("shutdown, fetch task canceled")
|
|
|
|
} else {
|
|
|
|
log.WithError(err).Error("failed to fetch task")
|
|
|
|
}
|
2023-04-04 13:32:04 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2023-07-25 03:25:50 +00:00
|
|
|
if resp == nil || resp.Msg == nil {
|
2023-04-04 13:32:04 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
2023-07-25 03:25:50 +00:00
|
|
|
|
|
|
|
if resp.Msg.TasksVersion > v {
|
|
|
|
p.tasksVersion.CompareAndSwap(v, resp.Msg.TasksVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Msg.Task == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// got a task, set `tasksVersion` to zero to focre query db in next request.
|
|
|
|
p.tasksVersion.CompareAndSwap(resp.Msg.TasksVersion, 0)
|
|
|
|
|
2023-04-04 13:32:04 +00:00
|
|
|
return resp.Msg.Task, true
|
|
|
|
}
|