2023-04-04 14:32:04 +01:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package poll
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2023-07-24 05:28:44 +01:00
|
|
|
"fmt"
|
2023-04-04 14:32:04 +01:00
|
|
|
"sync"
|
2023-07-25 04:25:50 +01:00
|
|
|
"sync/atomic"
|
2023-04-04 14:32:04 +01:00
|
|
|
|
|
|
|
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-07-25 04:25:50 +01:00
|
|
|
client client.Client
|
|
|
|
runner *run.Runner
|
|
|
|
cfg *config.Config
|
|
|
|
tasksVersion atomic.Int64 // tasksVersion used to store the version of the last task fetched from the Gitea.
|
2024-05-27 08:38:55 +01:00
|
|
|
|
|
|
|
pollingCtx context.Context
|
|
|
|
shutdownPolling context.CancelFunc
|
|
|
|
|
|
|
|
jobsCtx context.Context
|
|
|
|
shutdownJobs context.CancelFunc
|
|
|
|
|
|
|
|
done chan struct{}
|
2023-04-04 14:32:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(cfg *config.Config, client client.Client, runner *run.Runner) *Poller {
|
2024-05-27 08:38:55 +01:00
|
|
|
pollingCtx, shutdownPolling := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
jobsCtx, shutdownJobs := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
2023-04-04 14:32:04 +01:00
|
|
|
return &Poller{
|
2023-04-06 03:57:36 +01:00
|
|
|
client: client,
|
|
|
|
runner: runner,
|
|
|
|
cfg: cfg,
|
2024-05-27 08:38:55 +01:00
|
|
|
|
|
|
|
pollingCtx: pollingCtx,
|
|
|
|
shutdownPolling: shutdownPolling,
|
|
|
|
|
|
|
|
jobsCtx: jobsCtx,
|
|
|
|
shutdownJobs: shutdownJobs,
|
|
|
|
|
|
|
|
done: done,
|
2023-04-04 14:32:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-27 08:38:55 +01:00
|
|
|
func (p *Poller) Poll() {
|
2023-04-06 03:57:36 +01:00
|
|
|
limiter := rate.NewLimiter(rate.Every(p.cfg.Runner.FetchInterval), 1)
|
2023-04-04 14:32:04 +01:00
|
|
|
wg := &sync.WaitGroup{}
|
2023-04-06 03:57:36 +01:00
|
|
|
for i := 0; i < p.cfg.Runner.Capacity; i++ {
|
2023-04-04 14:32:04 +01:00
|
|
|
wg.Add(1)
|
2024-05-27 08:38:55 +01:00
|
|
|
go p.poll(wg, limiter)
|
2023-04-04 14:32:04 +01:00
|
|
|
}
|
|
|
|
wg.Wait()
|
2024-05-27 08:38:55 +01:00
|
|
|
|
|
|
|
// signal that we shutdown
|
|
|
|
close(p.done)
|
2023-04-04 14:32:04 +01:00
|
|
|
}
|
|
|
|
|
2024-05-27 08:38:55 +01:00
|
|
|
func (p *Poller) Shutdown(ctx context.Context) error {
|
|
|
|
p.shutdownPolling()
|
|
|
|
|
|
|
|
select {
|
|
|
|
// graceful shutdown completed succesfully
|
|
|
|
case <-p.done:
|
|
|
|
return nil
|
|
|
|
|
|
|
|
// our timeout for shutting down ran out
|
|
|
|
case <-ctx.Done():
|
|
|
|
// when both the timeout fires and the graceful shutdown
|
|
|
|
// completed succsfully, this branch of the select may
|
|
|
|
// fire. Do a non-blocking check here against the graceful
|
|
|
|
// shutdown status to avoid sending an error if we don't need to.
|
|
|
|
_, ok := <-p.done
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// force a shutdown of all running jobs
|
|
|
|
p.shutdownJobs()
|
|
|
|
|
|
|
|
// wait for running jobs to report their status to Gitea
|
|
|
|
_, _ = <-p.done
|
|
|
|
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Poller) poll(wg *sync.WaitGroup, limiter *rate.Limiter) {
|
2023-04-04 14:32:04 +01:00
|
|
|
defer wg.Done()
|
|
|
|
for {
|
2024-05-27 08:38:55 +01:00
|
|
|
if err := limiter.Wait(p.pollingCtx); err != nil {
|
|
|
|
if p.pollingCtx.Err() != nil {
|
2023-07-24 08:07:53 +01:00
|
|
|
log.WithError(err).Debug("limiter wait failed")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2024-05-27 08:38:55 +01:00
|
|
|
task, ok := p.fetchTask(p.pollingCtx)
|
2023-07-24 08:07:53 +01:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2024-05-27 08:38:55 +01:00
|
|
|
|
|
|
|
p.runTaskWithRecover(p.jobsCtx, task)
|
2023-07-24 05:28:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-24 08:07:53 +01:00
|
|
|
func (p *Poller) runTaskWithRecover(ctx context.Context, task *runnerv1.Task) {
|
2023-07-24 05:28:44 +01:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err := fmt.Errorf("panic: %v", r)
|
2023-07-24 08:07:53 +01:00
|
|
|
log.WithError(err).Error("panic in runTaskWithRecover")
|
2023-04-04 14:32:04 +01:00
|
|
|
}
|
2023-07-24 05:28:44 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
if err := p.runner.Run(ctx, task); err != nil {
|
|
|
|
log.WithError(err).Error("failed to run task")
|
2023-04-04 14:32:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Poller) fetchTask(ctx context.Context) (*runnerv1.Task, bool) {
|
2023-04-06 03:57:36 +01:00
|
|
|
reqCtx, cancel := context.WithTimeout(ctx, p.cfg.Runner.FetchTimeout)
|
2023-04-04 14:32:04 +01:00
|
|
|
defer cancel()
|
|
|
|
|
2023-07-25 04:25:50 +01: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 14:32:04 +01:00
|
|
|
if errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("failed to fetch task")
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2023-07-25 04:25:50 +01:00
|
|
|
if resp == nil || resp.Msg == nil {
|
2023-04-04 14:32:04 +01:00
|
|
|
return nil, false
|
|
|
|
}
|
2023-07-25 04:25:50 +01: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 14:32:04 +01:00
|
|
|
return resp.Msg.Task, true
|
|
|
|
}
|