Only update agent.LastWork if not done recently (#4031)

This commit is contained in:
Anbraten 2024-08-14 21:53:35 +02:00 committed by GitHub
parent bcecbbd398
commit b8c1d68eb1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -98,8 +98,7 @@ func (s *RPC) Extend(c context.Context, workflowID string) error {
return err return err
} }
agent.LastWork = time.Now().Unix() err = s.updateAgentLastWork(agent)
err = s.store.AgentUpdate(agent)
if err != nil { if err != nil {
return err return err
} }
@ -237,8 +236,7 @@ func (s *RPC) Init(c context.Context, strWorkflowID string, state rpc.WorkflowSt
} }
s.updateForgeStatus(c, repo, currentPipeline, workflow) s.updateForgeStatus(c, repo, currentPipeline, workflow)
agent.LastWork = time.Now().Unix() return s.updateAgentLastWork(agent)
return s.store.AgentUpdate(agent)
} }
// Done marks the workflow with the given ID as done. // Done marks the workflow with the given ID as done.
@ -331,8 +329,7 @@ func (s *RPC) Done(c context.Context, strWorkflowID string, state rpc.WorkflowSt
if err != nil { if err != nil {
return err return err
} }
agent.LastWork = time.Now().Unix() return s.updateAgentLastWork(agent)
return s.store.AgentUpdate(agent)
} }
// Log writes a log entry to the database and publishes it to the pubsub. // Log writes a log entry to the database and publishes it to the pubsub.
@ -362,8 +359,9 @@ func (s *RPC) Log(c context.Context, rpcLogEntry *rpc.LogEntry) error {
if err != nil { if err != nil {
return err return err
} }
agent.LastWork = time.Now().Unix()
if err := s.store.AgentUpdate(agent); err != nil { err = s.updateAgentLastWork(agent)
if err != nil {
return err return err
} }
@ -510,3 +508,17 @@ func (s *RPC) getHostnameFromContext(ctx context.Context) (string, error) {
} }
return "", errors.New("no hostname in metadata") return "", errors.New("no hostname in metadata")
} }
func (s *RPC) updateAgentLastWork(agent *model.Agent) error {
// only update agent.LastWork if not done recently
if time.Unix(agent.LastWork, 0).Add(1 * time.Minute).Before(time.Now()) {
return nil
}
agent.LastWork = time.Now().Unix()
if err := s.store.AgentUpdate(agent); err != nil {
return err
}
return nil
}