Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion engine/autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func (a *Autoscaler) calcAgents(ctx context.Context) (float64, error) {
maxUp := float64(a.config.MaxAgents - availablePoolAgents)
maxDown := float64(availablePoolAgents - a.config.MinAgents)

reqPoolAgents := math.Ceil(reqAgents - (availableAgents + float64(availablePoolAgents)))
reqPoolAgents := math.Ceil(reqAgents - availableAgents)
reqPoolAgents = math.Max(reqPoolAgents, -maxDown)
reqPoolAgents = math.Min(reqPoolAgents, maxUp)

Expand Down
22 changes: 22 additions & 0 deletions engine/autoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,28 @@ func Test_calcAgents(t *testing.T) {
value, _ := autoscaler.calcAgents(t.Context())
assert.Equal(t, float64(0), value)
})

t.Run("should create new agents when pool is fully busy with a backlog", func(t *testing.T) {
// All 3 existing pool agents are busy (workers=0, running=3) and 2 more
// workflows are queued. Regression test: calcAgents previously
// double-subtracted availablePoolAgents, which made it return a
// negative value here (wanting to drain agents) instead of scaling up.
autoscaler := Autoscaler{client: &MockClient{
workers: 0,
running: 3,
pending: 2,
}, config: &config.Config{
WorkflowsPerAgent: 1,
MaxAgents: 10,
}, agents: []*woodpecker.Agent{
{Name: "pool-1-agent-1111"},
{Name: "pool-1-agent-2222"},
{Name: "pool-1-agent-3333"},
}}

value, _ := autoscaler.calcAgents(t.Context())
assert.Equal(t, float64(2), value)
})
}

func Test_getQueueInfo(t *testing.T) {
Expand Down