mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-01-10 17:45:36 +00:00
Fix agent last work update throttling (#4124)
This commit is contained in:
parent
276b279b7f
commit
f2060db5c0
2 changed files with 52 additions and 2 deletions
|
@ -525,8 +525,8 @@ func (s *RPC) getHostnameFromContext(ctx context.Context) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *RPC) updateAgentLastWork(agent *model.Agent) error {
|
func (s *RPC) updateAgentLastWork(agent *model.Agent) error {
|
||||||
// only update agent.LastWork if not done recently
|
// only update agent.LastWork if not recently updated
|
||||||
if time.Unix(agent.LastWork, 0).Add(updateAgentLastWorkDelay).Before(time.Now()) {
|
if time.Unix(agent.LastWork, 0).Add(updateAgentLastWorkDelay).After(time.Now()) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,9 +17,11 @@ package grpc
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/franela/goblin"
|
"github.com/franela/goblin"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
"google.golang.org/grpc/metadata"
|
"google.golang.org/grpc/metadata"
|
||||||
|
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
||||||
|
@ -107,3 +109,51 @@ func TestRegisterAgent(t *testing.T) {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUpdateAgentLastWork(t *testing.T) {
|
||||||
|
t.Run("When last work was never updated it should update last work timestamp", func(t *testing.T) {
|
||||||
|
agent := model.Agent{
|
||||||
|
LastWork: 0,
|
||||||
|
}
|
||||||
|
store := mocks_store.NewStore(t)
|
||||||
|
rpc := RPC{
|
||||||
|
store: store,
|
||||||
|
}
|
||||||
|
store.On("AgentUpdate", mock.Anything).Once().Return(nil)
|
||||||
|
|
||||||
|
err := rpc.updateAgentLastWork(&agent)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.NotZero(t, agent.LastWork)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("When last work was updated over a minute ago it should update last work timestamp", func(t *testing.T) {
|
||||||
|
lastWork := time.Now().Add(-time.Hour).Unix()
|
||||||
|
agent := model.Agent{
|
||||||
|
LastWork: lastWork,
|
||||||
|
}
|
||||||
|
store := mocks_store.NewStore(t)
|
||||||
|
rpc := RPC{
|
||||||
|
store: store,
|
||||||
|
}
|
||||||
|
store.On("AgentUpdate", mock.Anything).Once().Return(nil)
|
||||||
|
|
||||||
|
err := rpc.updateAgentLastWork(&agent)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.NotEqual(t, lastWork, agent.LastWork)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("When last work was updated in the last minute it should not update last work timestamp again", func(t *testing.T) {
|
||||||
|
lastWork := time.Now().Add(-time.Second * 30).Unix()
|
||||||
|
agent := model.Agent{
|
||||||
|
LastWork: lastWork,
|
||||||
|
}
|
||||||
|
rpc := RPC{}
|
||||||
|
|
||||||
|
err := rpc.updateAgentLastWork(&agent)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, lastWork, agent.LastWork)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue