mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-16 21:03:43 +00:00
fix: honor zero and empty values for fetch backoff and idle cleanup (#1418)
`fetch_interval_max: 0` is documented to disable polling backoff but became 5s. Zeros for `workdir_cleanup_age` and `idle_cleanup_interval` were ignored when they came from a YAML merge key or alias, so cleanup still ran, while an empty or null value turned cleanup off. These defaults are now seeded before the file is read, as `cache` already does, so a written 0 means off and an empty value keeps the default. _Written by Claude (Opus 5.5)._ Reviewed-on: https://gitea.com/gitea/runner/pulls/1418 Reviewed-by: bircni <[email protected]> Co-authored-by: silverwind <[email protected]>
This commit is contained in:
1 parent
891f3fde38
commit
1f2ea1b83a
2 files changed
+14
-35
No files matched your search
@@ -271,8 +271,11 @@ type Config struct {
|
|||||||
// If file is not empty, it will be used to load the configuration.
|
// If file is not empty, it will be used to load the configuration.
|
||||||
func LoadDefault(file string) (*Config, error) {
|
func LoadDefault(file string) (*Config, error) {
|
||||||
// Seeded before the file is read, so a written 0 can mean off.
|
// Seeded before the file is read, so a written 0 can mean off.
|
||||||
cfg := &Config{Cache: DefaultCache(), Log: Log{Job: LogJob{Retention: 7 * 24 * time.Hour, MaxSize: 1024 * 1024 * 1024}}}
|
cfg := &Config{
|
||||||
var definedRunnerKeys map[string]bool
|
Runner: Runner{FetchIntervalMax: 5 * time.Second, WorkdirCleanupAge: 24 * time.Hour, IdleCleanupInterval: 10 * time.Minute},
|
||||||
|
Cache: DefaultCache(),
|
||||||
|
Log: Log{Job: LogJob{Retention: 7 * 24 * time.Hour, MaxSize: 1024 * 1024 * 1024}},
|
||||||
|
}
|
||||||
if file != "" {
|
if file != "" {
|
||||||
content, err := os.ReadFile(file)
|
content, err := os.ReadFile(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -289,7 +292,6 @@ func LoadDefault(file string) (*Config, error) {
|
|||||||
return nil, fmt.Errorf("parse config file %q: %w", file, err)
|
return nil, fmt.Errorf("parse config file %q: %w", file, err)
|
||||||
}
|
}
|
||||||
warnUnknownKeys(file, &document)
|
warnUnknownKeys(file, &document)
|
||||||
definedRunnerKeys = definedRunnerConfigKeys(&document)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Runner.EnvFile != "" {
|
if cfg.Runner.EnvFile != "" {
|
||||||
@@ -410,15 +412,6 @@ func LoadDefault(file string) (*Config, error) {
|
|||||||
if cfg.Runner.FetchInterval <= 0 {
|
if cfg.Runner.FetchInterval <= 0 {
|
||||||
cfg.Runner.FetchInterval = 2 * time.Second
|
cfg.Runner.FetchInterval = 2 * time.Second
|
||||||
}
|
}
|
||||||
if cfg.Runner.FetchIntervalMax <= 0 {
|
|
||||||
cfg.Runner.FetchIntervalMax = 5 * time.Second
|
|
||||||
}
|
|
||||||
if cfg.Runner.WorkdirCleanupAge == 0 && !definedRunnerKeys["workdir_cleanup_age"] {
|
|
||||||
cfg.Runner.WorkdirCleanupAge = 24 * time.Hour
|
|
||||||
}
|
|
||||||
if cfg.Runner.IdleCleanupInterval == 0 && !definedRunnerKeys["idle_cleanup_interval"] {
|
|
||||||
cfg.Runner.IdleCleanupInterval = 10 * time.Minute
|
|
||||||
}
|
|
||||||
if cfg.Runner.LogReportInterval <= 0 {
|
if cfg.Runner.LogReportInterval <= 0 {
|
||||||
cfg.Runner.LogReportInterval = 5 * time.Second
|
cfg.Runner.LogReportInterval = 5 * time.Second
|
||||||
}
|
}
|
||||||
@@ -459,8 +452,10 @@ func LoadDefault(file string) (*Config, error) {
|
|||||||
cfg.Runner.FetchTimeout = RequestTimeout
|
cfg.Runner.FetchTimeout = RequestTimeout
|
||||||
}
|
}
|
||||||
if cfg.Runner.FetchIntervalMax < cfg.Runner.FetchInterval {
|
if cfg.Runner.FetchIntervalMax < cfg.Runner.FetchInterval {
|
||||||
|
if cfg.Runner.FetchIntervalMax != 0 {
|
||||||
logging.Warnf("fetch_interval_max (%v) is less than fetch_interval (%v), setting fetch_interval_max to fetch_interval",
|
logging.Warnf("fetch_interval_max (%v) is less than fetch_interval (%v), setting fetch_interval_max to fetch_interval",
|
||||||
cfg.Runner.FetchIntervalMax, cfg.Runner.FetchInterval)
|
cfg.Runner.FetchIntervalMax, cfg.Runner.FetchInterval)
|
||||||
|
}
|
||||||
cfg.Runner.FetchIntervalMax = cfg.Runner.FetchInterval
|
cfg.Runner.FetchIntervalMax = cfg.Runner.FetchInterval
|
||||||
}
|
}
|
||||||
if cfg.Runner.LogReportMaxLatency >= cfg.Runner.LogReportInterval {
|
if cfg.Runner.LogReportMaxLatency >= cfg.Runner.LogReportInterval {
|
||||||
@@ -550,28 +545,6 @@ func warnUnknownKeys(file string, document *yaml.Node) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func definedRunnerConfigKeys(root *yaml.Node) map[string]bool {
|
|
||||||
defined := map[string]bool{}
|
|
||||||
if len(root.Content) == 0 {
|
|
||||||
return defined
|
|
||||||
}
|
|
||||||
|
|
||||||
doc := root.Content[0]
|
|
||||||
for i := 0; i+1 < len(doc.Content); i += 2 {
|
|
||||||
key := doc.Content[i]
|
|
||||||
value := doc.Content[i+1]
|
|
||||||
if key.Value != "runner" || value.Kind != yaml.MappingNode {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for j := 0; j+1 < len(value.Content); j += 2 {
|
|
||||||
defined[value.Content[j].Value] = true
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
return defined
|
|
||||||
}
|
|
||||||
|
|
||||||
func validateExtraHeaders(headers map[string]string) error {
|
func validateExtraHeaders(headers map[string]string) error {
|
||||||
seen := make(map[string]string, len(headers))
|
seen := make(map[string]string, len(headers))
|
||||||
for name, value := range headers {
|
for name, value := range headers {
|
||||||
|
|||||||
@@ -255,6 +255,7 @@ metrics:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadDefault_WorkdirCleanup(t *testing.T) {
|
func TestLoadDefault_WorkdirCleanup(t *testing.T) {
|
||||||
|
t.Setenv("EMPTY", "")
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
name, yaml string
|
name, yaml string
|
||||||
age, interval time.Duration
|
age, interval time.Duration
|
||||||
@@ -263,6 +264,8 @@ func TestLoadDefault_WorkdirCleanup(t *testing.T) {
|
|||||||
{"configured interval", "\nrunner:\n idle_cleanup_interval: 45m\n", 24 * time.Hour, 45 * time.Minute},
|
{"configured interval", "\nrunner:\n idle_cleanup_interval: 45m\n", 24 * time.Hour, 45 * time.Minute},
|
||||||
{"disabled", "\nrunner:\n workdir_cleanup_age: 0s\n idle_cleanup_interval: 0s\n", 0, 0},
|
{"disabled", "\nrunner:\n workdir_cleanup_age: 0s\n idle_cleanup_interval: 0s\n", 0, 0},
|
||||||
{"negative", "\nrunner:\n workdir_cleanup_age: -1s\n idle_cleanup_interval: -1s\n", -time.Second, -time.Second},
|
{"negative", "\nrunner:\n workdir_cleanup_age: -1s\n idle_cleanup_interval: -1s\n", -time.Second, -time.Second},
|
||||||
|
{"disabled through a merge key", "\nx-base: &base\n workdir_cleanup_age: 0s\n idle_cleanup_interval: 0s\nrunner:\n <<: *base\n", 0, 0},
|
||||||
|
{"empty keeps the default", "\nrunner:\n workdir_cleanup_age: ${EMPTY}\n idle_cleanup_interval:\n", 24 * time.Hour, 10 * time.Minute},
|
||||||
} {
|
} {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
cfg, err := LoadDefault(write(t, tc.yaml))
|
cfg, err := LoadDefault(write(t, tc.yaml))
|
||||||
@@ -441,11 +444,14 @@ func TestLoadDefault_ShippedConfigsChangeNothing(t *testing.T) {
|
|||||||
assert.Empty(t, hook.AllRecords())
|
assert.Empty(t, hook.AllRecords())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadDefault_ClampsFetchTimeout(t *testing.T) {
|
func TestLoadDefault_ClampsFetchTimeoutAndZeroFetchIntervalMaxDisablesBackoff(t *testing.T) {
|
||||||
cfg, err := LoadDefault(write(t, `
|
cfg, err := LoadDefault(write(t, `
|
||||||
runner:
|
runner:
|
||||||
fetch_timeout: 120s
|
fetch_timeout: 120s
|
||||||
|
fetch_interval: 1s
|
||||||
|
fetch_interval_max: 0s
|
||||||
`))
|
`))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, RequestTimeout, cfg.Runner.FetchTimeout)
|
assert.Equal(t, RequestTimeout, cfg.Runner.FetchTimeout)
|
||||||
|
assert.Equal(t, time.Second, cfg.Runner.FetchIntervalMax)
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user