forgejo/services/cron/tasks_actions.go
Earl Warren 238ecfdeb8 fix: garbage collect lingering actions logs (#10009)
If, for any reason (e.g. server crash), a task is recorded as done in the database but the logs are still in the database instead of being in storage, they need to be collected.

The log_in_storage field is only set to true after the logs have been transfered to storage and can be relied upon to reflect which tasks have lingering logs.

A cron job collects lingering logs every day, 3000 at a time, sleeping one second between them. In normal circumstances there will be only a few of them, even on a large instance, and there is no need to collect them as quickly as possible.

When there are a lot of them for some reason, garbage collection must happen at a rate that is not too hard on storage I/O.

Refs https://codeberg.org/forgejo/forgejo/issues/9999

---

Note on backports: the v11 backport is done manually because of minor conflicts. https://codeberg.org/forgejo/forgejo/pulls/10024

## 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

- 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 added test coverage for JavaScript changes...
  - [ ] in `web_src/js/*.test.js` if it can be unit tested.
  - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)).

### 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

- [ ] I do not want this change to show in the release notes.
- [x] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.

<!--start release-notes-assistant-->

## Release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- Bug fixes
  - [PR](https://codeberg.org/forgejo/forgejo/pulls/10009): <!--number 10009 --><!--line 0 --><!--description Z2FyYmFnZSBjb2xsZWN0IGxpbmdlcmluZyBhY3Rpb25zIGxvZ3M=-->garbage collect lingering actions logs<!--description-->
<!--end release-notes-assistant-->

Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10009
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: Earl Warren <contact@earl-warren.org>
Co-committed-by: Earl Warren <contact@earl-warren.org>
2025-11-18 18:59:01 +01:00

119 lines
3.2 KiB
Go

// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package cron
import (
"context"
"time"
user_model "forgejo.org/models/user"
"forgejo.org/modules/setting"
actions_service "forgejo.org/services/actions"
)
func initActionsTasks() {
if !setting.Actions.Enabled {
return
}
registerStopZombieTasks()
registerStopEndlessTasks()
registerCancelAbandonedJobs()
registerTransferLingeringLogs()
registerScheduleTasks()
registerActionsCleanup()
registerOfflineRunnersCleanup()
registerCleanupActionUser()
}
func registerStopZombieTasks() {
RegisterTaskFatal("stop_zombie_tasks", &BaseConfig{
Enabled: true,
RunAtStart: true,
Schedule: "@every 5m",
}, func(ctx context.Context, _ *user_model.User, cfg Config) error {
return actions_service.StopZombieTasks(ctx)
})
}
func registerStopEndlessTasks() {
RegisterTaskFatal("stop_endless_tasks", &BaseConfig{
Enabled: true,
RunAtStart: true,
Schedule: "@every 30m",
}, func(ctx context.Context, _ *user_model.User, cfg Config) error {
return actions_service.StopEndlessTasks(ctx)
})
}
func registerCancelAbandonedJobs() {
RegisterTaskFatal("cancel_abandoned_jobs", &BaseConfig{
Enabled: true,
RunAtStart: true,
Schedule: "@every 6h",
}, func(ctx context.Context, _ *user_model.User, cfg Config) error {
return actions_service.CancelAbandonedJobs(ctx)
})
}
func registerTransferLingeringLogs() {
RegisterTaskFatal("transfer_lingering_logs", &BaseConfig{
Enabled: true,
RunAtStart: true,
Schedule: "@midnight",
}, func(ctx context.Context, _ *user_model.User, cfg Config) error {
return actions_service.TransferLingeringLogs(ctx)
})
}
// registerScheduleTasks registers a scheduled task that runs every minute to start any due schedule tasks.
func registerScheduleTasks() {
// Register the task with a unique name, enabled status, and schedule for every minute.
RegisterTaskFatal("start_schedule_tasks", &BaseConfig{
Enabled: true,
RunAtStart: false,
Schedule: "@every 1m",
}, func(ctx context.Context, _ *user_model.User, cfg Config) error {
// Call the function to start schedule tasks and pass the context.
return actions_service.StartScheduleTasks(ctx)
})
}
func registerActionsCleanup() {
RegisterTaskFatal("cleanup_actions", &BaseConfig{
Enabled: true,
RunAtStart: false,
Schedule: "@midnight",
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
return actions_service.Cleanup(ctx)
})
}
func registerOfflineRunnersCleanup() {
RegisterTaskFatal("cleanup_offline_runners", &CleanupOfflineRunnersConfig{
BaseConfig: BaseConfig{
Enabled: false,
RunAtStart: false,
Schedule: "@midnight",
},
GlobalScopeOnly: true,
OlderThan: time.Hour * 24,
}, func(ctx context.Context, _ *user_model.User, cfg Config) error {
c := cfg.(*CleanupOfflineRunnersConfig)
return actions_service.CleanupOfflineRunners(
ctx,
c.OlderThan,
c.GlobalScopeOnly,
)
})
}
func registerCleanupActionUser() {
RegisterTaskFatal("actions_action_user", &BaseConfig{
Enabled: true,
RunAtStart: true,
Schedule: "@weekly",
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
return actions_service.CleanupActionUser(ctx)
})
}