mirror of
https://github.com/go-gitea/gitea
synced 2025-12-07 13:28:25 +00:00
feat: update runner status
This commit is contained in:
@@ -38,22 +38,10 @@ type Service struct {
|
||||
|
||||
// UpdateRunner update runner status or other data.
|
||||
func (s *Service) UpdateRunner(
|
||||
ctx context.Context,
|
||||
req *connect.Request[runnerv1.UpdateRunnerRequest],
|
||||
_ context.Context,
|
||||
_ *connect.Request[runnerv1.UpdateRunnerRequest],
|
||||
) (*connect.Response[runnerv1.UpdateRunnerResponse], error) {
|
||||
runner := GetRunner(ctx)
|
||||
|
||||
// check status
|
||||
if runner.Status == req.Msg.Status {
|
||||
return connect.NewResponse(&runnerv1.UpdateRunnerResponse{}), nil
|
||||
}
|
||||
|
||||
// update status
|
||||
runner.Status = req.Msg.Status
|
||||
if err := bots_model.UpdateRunner(ctx, runner, "status"); err != nil {
|
||||
return nil, connect.NewError(connect.CodeInternal, err)
|
||||
}
|
||||
|
||||
// FIXME: we don't need it any longer
|
||||
return connect.NewResponse(&runnerv1.UpdateRunnerResponse{}), nil
|
||||
}
|
||||
|
||||
@@ -81,7 +69,6 @@ func (s *Service) Register(
|
||||
Name: req.Msg.Name,
|
||||
OwnerID: runnerToken.OwnerID,
|
||||
RepoID: runnerToken.RepoID,
|
||||
Status: runnerv1.RunnerStatus_RUNNER_STATUS_OFFLINE,
|
||||
AgentLabels: req.Msg.AgentLabels,
|
||||
CustomLabels: req.Msg.CustomLabels,
|
||||
}
|
||||
@@ -108,7 +95,6 @@ func (s *Service) Register(
|
||||
Name: runner.Name,
|
||||
AgentLabels: runner.AgentLabels,
|
||||
CustomLabels: runner.CustomLabels,
|
||||
Status: runner.Status,
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -14,21 +14,20 @@ import (
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
|
||||
"github.com/bufbuild/connect-go"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
const (
|
||||
runnerOnlineTimeDeltaSecs = 30
|
||||
uuidHeaderKey = "x-runner-uuid"
|
||||
tokenHeaderKey = "x-runner-token"
|
||||
uuidHeaderKey = "x-runner-uuid"
|
||||
tokenHeaderKey = "x-runner-token"
|
||||
)
|
||||
|
||||
var WithRunner = connect.WithInterceptors(connect.UnaryInterceptorFunc(func(unaryFunc connect.UnaryFunc) connect.UnaryFunc {
|
||||
return func(ctx context.Context, request connect.AnyRequest) (connect.AnyResponse, error) {
|
||||
if methodName(request) == "Register" {
|
||||
methodName := getMethodName(request)
|
||||
if methodName == "Register" {
|
||||
return unaryFunc(ctx, request)
|
||||
}
|
||||
uuid := request.Header().Get(uuidHeaderKey)
|
||||
@@ -44,19 +43,14 @@ var WithRunner = connect.WithInterceptors(connect.UnaryInterceptorFunc(func(unar
|
||||
return nil, status.Error(codes.Unauthenticated, "unregistered runner")
|
||||
}
|
||||
|
||||
// update runner online status
|
||||
if runner.Status == runnerv1.RunnerStatus_RUNNER_STATUS_OFFLINE {
|
||||
runner.LastOnline = timeutil.TimeStampNow()
|
||||
runner.Status = runnerv1.RunnerStatus_RUNNER_STATUS_ACTIVE
|
||||
if err := bots_model.UpdateRunner(ctx, runner, "last_online", "status"); err != nil {
|
||||
log.Error("can't update runner status: %v", err)
|
||||
}
|
||||
cols := []string{"last_online"}
|
||||
runner.LastOnline = timeutil.TimeStampNow()
|
||||
if methodName == "UpdateTask" || methodName == "UpdateLog" {
|
||||
runner.LastActive = timeutil.TimeStampNow()
|
||||
cols = append(cols, "last_active")
|
||||
}
|
||||
if timeutil.TimeStampNow()-runner.LastOnline >= runnerOnlineTimeDeltaSecs {
|
||||
runner.LastOnline = timeutil.TimeStampNow()
|
||||
if err := bots_model.UpdateRunner(ctx, runner, "last_online"); err != nil {
|
||||
log.Error("can't update runner last_online: %v", err)
|
||||
}
|
||||
if err := bots_model.UpdateRunner(ctx, runner, cols...); err != nil {
|
||||
log.Error("can't update runner status: %v", err)
|
||||
}
|
||||
|
||||
ctx = context.WithValue(ctx, runnerCtxKey{}, runner)
|
||||
@@ -64,7 +58,7 @@ var WithRunner = connect.WithInterceptors(connect.UnaryInterceptorFunc(func(unar
|
||||
}
|
||||
}))
|
||||
|
||||
func methodName(req connect.AnyRequest) string {
|
||||
func getMethodName(req connect.AnyRequest) string {
|
||||
splits := strings.Split(req.Spec().Procedure, "/")
|
||||
if len(splits) > 0 {
|
||||
return splits[len(splits)-1]
|
||||
|
||||
Reference in New Issue
Block a user