diff --git a/cmd/drone-server/config/config.go b/cmd/drone-server/config/config.go index 2b683a4e4..26c7d998c 100644 --- a/cmd/drone-server/config/config.go +++ b/cmd/drone-server/config/config.go @@ -103,6 +103,7 @@ type ( Interval time.Duration `envconfig:"DRONE_CLEANUP_INTERVAL" default:"24h"` Running time.Duration `envconfig:"DRONE_CLEANUP_DEADLINE_RUNNING" default:"24h"` Pending time.Duration `envconfig:"DRONE_CLEANUP_DEADLINE_PENDING" default:"24h"` + Buffer time.Duration `envconfig:"DRONE_CLEANUP_BUFFER" default:"30m"` } // Cron provides the cron configuration. diff --git a/cmd/drone-server/inject_service.go b/cmd/drone-server/inject_service.go index 6a3302235..25c9b8eb8 100644 --- a/cmd/drone-server/inject_service.go +++ b/cmd/drone-server/inject_service.go @@ -181,6 +181,7 @@ func provideReaper( canceler, config.Cleanup.Running, config.Cleanup.Pending, + config.Cleanup.Buffer, ) } @@ -212,7 +213,7 @@ func provideDatadog( EnableStash: config.IsStash(), EnableGogs: config.IsGogs(), EnableGitea: config.IsGitea(), - EnableGitee: config.IsGitee(), + EnableGitee: config.IsGitee(), EnableAgents: !config.Agent.Disabled, }, ) diff --git a/service/canceler/reaper/reaper.go b/service/canceler/reaper/reaper.go index 40908c9d7..9de39b11d 100644 --- a/service/canceler/reaper/reaper.go +++ b/service/canceler/reaper/reaper.go @@ -34,6 +34,10 @@ type Reaper struct { Canceler core.Canceler Pending time.Duration // Pending is the pending pipeline deadline Running time.Duration // Running is the running pipeline deadline + + // Buffer is applied when calculating whether or not the timeout + // period is exceeded. The added buffer helps prevent false positives. + Buffer time.Duration } // New returns a new Reaper. @@ -44,6 +48,7 @@ func New( canceler core.Canceler, running time.Duration, pending time.Duration, + buffer time.Duration, ) *Reaper { if running == 0 { running = time.Hour * 24 @@ -51,6 +56,10 @@ func New( if pending == 0 { pending = time.Hour * 24 } + + if buffer == 0 { + buffer = time.Minute * 30 + } return &Reaper{ Repos: repos, Builds: builds, @@ -58,6 +67,7 @@ func New( Canceler: canceler, Pending: pending, Running: running, + Buffer: buffer, } } @@ -105,7 +115,7 @@ func (r *Reaper) reap(ctx context.Context) error { // if a build is pending for longer than the maximum // pending time limit, the build is maybe cancelled. - if isExceeded(build.Created, r.Pending, buffer) { + if isExceeded(build.Created, r.Pending, r.Buffer) { logger.Traceln("reaper: cancel build: time limit exceeded") err = r.reapMaybe(ctx, build) if err != nil { @@ -134,7 +144,7 @@ func (r *Reaper) reap(ctx context.Context) error { // if a build is running for longer than the maximum // running time limit, the build is maybe cancelled. - if isExceeded(build.Started, r.Running, buffer) { + if isExceeded(build.Started, r.Running, r.Buffer) { logger.Traceln("reaper: cancel build: time limit exceeded") err = r.reapMaybe(ctx, build) @@ -188,7 +198,7 @@ func (r *Reaper) reapMaybe(ctx context.Context, build *core.Build) error { // if the build stage has exceeded the timeout by a reasonable // margin cancel the build and all build stages, else ignore. - if isExceeded(started, time.Duration(repo.Timeout)*time.Minute, buffer) { + if isExceeded(started, time.Duration(repo.Timeout)*time.Minute, r.Buffer) { // TODO trace log entry return r.Canceler.Cancel(ctx, repo, build) } diff --git a/service/canceler/reaper/reaper_test.go b/service/canceler/reaper/reaper_test.go index 0261baefb..b98ea934e 100644 --- a/service/canceler/reaper/reaper_test.go +++ b/service/canceler/reaper/reaper_test.go @@ -72,6 +72,7 @@ func TestReapPending(t *testing.T) { canceler, time.Hour*24, time.Hour*24, + time.Minute*30, ) r.reap(nocontext) @@ -139,6 +140,7 @@ func TestReapRunning(t *testing.T) { canceler, time.Hour*24, time.Hour*24, + time.Minute*30, ) r.reap(nocontext) diff --git a/service/canceler/reaper/util.go b/service/canceler/reaper/util.go index afdbc16be..5bf710f29 100644 --- a/service/canceler/reaper/util.go +++ b/service/canceler/reaper/util.go @@ -16,10 +16,6 @@ package reaper import "time" -// buffer is applied when calculating whether or not the timeout -// period is exceeded. The added buffer helps prevent false positives. -var buffer = time.Minute * 30 - // helper function returns the current time. var now = time.Now