mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-12 22:10:25 +00:00
fix: allow Actions runner to recover tasks lost during fetching from intermittent errors (#11401)
Probably fixes (or improves, at least) https://code.forgejo.org/forgejo/runner/issues/1391, paired with the runner implementation https://code.forgejo.org/forgejo/runner/pulls/1393. When the FetchTask() API is invoked to create a task, unpreventable environmental errors may occur; for example, network disconnects and timeouts. It's possible that these errors occur after the server-side has assigned a task to the runner during the API call, in which case the error would cause that task to be lost between the two systems -- the server will think it's assigned to the runner, and the runner never received it. This can cause jobs to appear stuck at "Set up job". The solution implemented here is idempotency in the FetchTask() API call, which means that the "same" FetchTask() API call is expected to return the same values. Specifically, the runner creates a unique identifier which is transmitted to the server as a header `x-runner-request-key` with each FetchTask() invocation which defines the sameness of the call, and the runner retains the value until the API call receives a successful response. The server implementation returns the same tasks back if a second (or Nth) call is received with the same `x-runner-request-key` header. In order to accomplish this is records the `x-runner-request-key` value that is used with each request that assigns tasks. As a complication, the Forgejo server is unable to return the same `${{ secrets.forgejo_token }}` for the task because the server stores that value in a one-way hash in the database. To resolve this, the server regenerates the token when retrieving tasks for a second time. ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests for Go changes (can be removed for JavaScript changes) - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I ran... - [x] `make pr-go` before pushing ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [x] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - [ ] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change. *The decision if the pull request will be shown in the release notes is up to the mergers / release team.* The content of the `release-notes/<pull request number>.md` file will serve as the basis for the release notes. If the file does not exist, the title of the pull request will be used instead. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11401 Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org> Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net> Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
This commit is contained in:
parent
5486bfa535
commit
0ae6235386
9 changed files with 337 additions and 11 deletions
|
|
@ -0,0 +1,36 @@
|
|||
-
|
||||
id: 100
|
||||
attempt: 3
|
||||
runner_id: 12345678
|
||||
status: 5 # StatusWaiting
|
||||
repo_id: 4
|
||||
owner_id: 1
|
||||
commit_sha: c2d72f548424103f01ee1dc02889c1e2bff816b0
|
||||
is_fork_pull_request: false
|
||||
token_hash: a1
|
||||
token_salt: eeeeeeee
|
||||
token_last_eight: eeeeeeee
|
||||
log_filename: artifact-test2/2f/47.log
|
||||
log_in_storage: true
|
||||
log_length: 707
|
||||
log_size: 90179
|
||||
log_expired: false
|
||||
runner_request_key: 0a7e017d-4201-4b34-8cf4-de0f431893a4
|
||||
-
|
||||
id: 101
|
||||
attempt: 3
|
||||
runner_id: 12345678
|
||||
status: 5 # StatusWaiting
|
||||
repo_id: 4
|
||||
owner_id: 1
|
||||
commit_sha: c2d72f548424103f01ee1dc02889c1e2bff816b0
|
||||
is_fork_pull_request: false
|
||||
token_hash: a2
|
||||
token_salt: eeeeeeee
|
||||
token_last_eight: eeeeeeee
|
||||
log_filename: artifact-test2/2f/47.log
|
||||
log_in_storage: true
|
||||
log_length: 707
|
||||
log_size: 90179
|
||||
log_expired: false
|
||||
runner_request_key: 0a7e017d-4201-4b34-8cf4-de0f431893a4
|
||||
|
|
@ -29,7 +29,7 @@ type ActionTask struct {
|
|||
Job *ActionRunJob `xorm:"-"`
|
||||
Steps []*ActionTaskStep `xorm:"-"`
|
||||
Attempt int64
|
||||
RunnerID int64 `xorm:"index"`
|
||||
RunnerID int64 `xorm:"index index(request_key)"`
|
||||
Status Status `xorm:"index"`
|
||||
Started timeutil.TimeStamp `xorm:"index"`
|
||||
Stopped timeutil.TimeStamp `xorm:"index(stopped_log_expired)"`
|
||||
|
|
@ -51,6 +51,15 @@ type ActionTask struct {
|
|||
LogIndexes LogIndexes `xorm:"LONGBLOB"` // line number to offset
|
||||
LogExpired bool `xorm:"index(stopped_log_expired)"` // files that are too old will be deleted
|
||||
|
||||
// When the FetchTask() API is invoked to create a task, unpreventable environmental errors may occur; for example,
|
||||
// network disconnects and timeouts. If that API call has a unique identifier associated with it, it is stored in
|
||||
// RunnerRequestKey. This allows the API call to be implemented idempotently using this state: if one API call
|
||||
// assigns a task to a runner and a second API call is received from the same runner with the same request key, the
|
||||
// existing assigned tasks can be returned.
|
||||
//
|
||||
// Indexed for an efficient search on runner_id=? AND runner_request_key=?.
|
||||
RunnerRequestKey string `xorm:"index(request_key)"`
|
||||
|
||||
Created timeutil.TimeStamp `xorm:"created"`
|
||||
Updated timeutil.TimeStamp `xorm:"updated index"`
|
||||
}
|
||||
|
|
@ -147,6 +156,11 @@ func (task *ActionTask) GenerateToken() {
|
|||
task.Token, task.TokenSalt, task.TokenHash, task.TokenLastEight = generateSaltedToken()
|
||||
}
|
||||
|
||||
// After using GenerateToken, UpdateToken can be used to update the database record affecting the same columns.
|
||||
func (task *ActionTask) UpdateToken(ctx context.Context) error {
|
||||
return UpdateTask(ctx, task, "token_hash", "token_salt", "token_last_eight")
|
||||
}
|
||||
|
||||
// Retrieve all the attempts from the same job as the target `ActionTask`. Limited fields are queried to avoid loading
|
||||
// the LogIndexes blob when not needed.
|
||||
func (task *ActionTask) GetAllAttempts(ctx context.Context) ([]*ActionTask, error) {
|
||||
|
|
@ -242,6 +256,15 @@ func GetRunningTaskByToken(ctx context.Context, token string) (*ActionTask, erro
|
|||
return nil, errNotExist
|
||||
}
|
||||
|
||||
func GetTasksByRunnerRequestKey(ctx context.Context, runner *ActionRunner, requestKey string) ([]*ActionTask, error) {
|
||||
var tasks []*ActionTask
|
||||
err := db.GetEngine(ctx).Where("runner_id = ? AND runner_request_key = ?", runner.ID, requestKey).Find(&tasks)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tasks, nil
|
||||
}
|
||||
|
||||
func getConcurrencyCondition() builder.Cond {
|
||||
concurrencyCond := builder.NewCond()
|
||||
|
||||
|
|
@ -324,7 +347,7 @@ func GetAvailableJobsForRunner(e db.Engine, runner *ActionRunner) ([]*ActionRunJ
|
|||
return jobs, nil
|
||||
}
|
||||
|
||||
func CreateTaskForRunner(ctx context.Context, runner *ActionRunner) (*ActionTask, bool, error) {
|
||||
func CreateTaskForRunner(ctx context.Context, runner *ActionRunner, requestKey *string) (*ActionTask, bool, error) {
|
||||
ctx, committer, err := db.TxContext(ctx)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
|
|
@ -370,6 +393,9 @@ func CreateTaskForRunner(ctx context.Context, runner *ActionRunner) (*ActionTask
|
|||
CommitSHA: job.CommitSHA,
|
||||
IsForkPullRequest: job.IsForkPullRequest,
|
||||
}
|
||||
if requestKey != nil {
|
||||
task.RunnerRequestKey = *requestKey
|
||||
}
|
||||
task.GenerateToken()
|
||||
|
||||
var workflowJob *jobparser.Job
|
||||
|
|
|
|||
|
|
@ -76,3 +76,26 @@ func TestActionTask_CreatePlaceholderTask(t *testing.T) {
|
|||
}
|
||||
assert.Equal(t, map[string]string{"output1": "value1", "output2": "value2"}, finalOutputs)
|
||||
}
|
||||
|
||||
func TestActionTask_GetTasksByRunnerRequestKey(t *testing.T) {
|
||||
defer unittest.OverrideFixtures("models/actions/TestActionTask_GetTasksByRunnerRequestKey")()
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
runner := unittest.AssertExistsAndLoadBean(t, &ActionRunner{ID: 12345678})
|
||||
|
||||
// not matching runner_request_key
|
||||
tasks, err := GetTasksByRunnerRequestKey(t.Context(), runner, "22288392-2c70-4125-bb01-c7da79fa280c")
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, tasks)
|
||||
|
||||
// matching both runner_id and runner_request_key
|
||||
tasks, err = GetTasksByRunnerRequestKey(t.Context(), runner, "0a7e017d-4201-4b34-8cf4-de0f431893a4")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, tasks, 2)
|
||||
|
||||
// not matching runner_id
|
||||
runner = unittest.AssertExistsAndLoadBean(t, &ActionRunner{ID: 10000001})
|
||||
tasks, err = GetTasksByRunnerRequestKey(t.Context(), runner, "0a7e017d-4201-4b34-8cf4-de0f431893a4")
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, tasks)
|
||||
}
|
||||
|
|
|
|||
24
models/forgejo_migrations/v15b_add-runner_request_key.go
Normal file
24
models/forgejo_migrations/v15b_add-runner_request_key.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package forgejo_migrations
|
||||
|
||||
import (
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registerMigration(&Migration{
|
||||
Description: "add runner_request_key to action_task",
|
||||
Upgrade: addActionTaskRunnerRequestKey,
|
||||
})
|
||||
}
|
||||
|
||||
func addActionTaskRunnerRequestKey(x *xorm.Engine) error {
|
||||
type ActionTask struct {
|
||||
RunnerID int64 `xorm:"index index(request_key)"`
|
||||
RunnerRequestKey string `xorm:"index(request_key)"`
|
||||
}
|
||||
_, err := x.SyncWithOptions(xorm.SyncOptions{IgnoreDropIndices: true}, new(ActionTask))
|
||||
return err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue