Skip to content

Commit

Permalink
Only update agent.LastWork if not done recently (#4031)
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten authored Aug 14, 2024
1 parent bcecbbd commit b8c1d68
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions server/grpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ func (s *RPC) Extend(c context.Context, workflowID string) error {
return err
}

agent.LastWork = time.Now().Unix()
err = s.store.AgentUpdate(agent)
err = s.updateAgentLastWork(agent)
if err != nil {
return err
}
Expand Down Expand Up @@ -237,8 +236,7 @@ func (s *RPC) Init(c context.Context, strWorkflowID string, state rpc.WorkflowSt
}
s.updateForgeStatus(c, repo, currentPipeline, workflow)

agent.LastWork = time.Now().Unix()
return s.store.AgentUpdate(agent)
return s.updateAgentLastWork(agent)
}

// Done marks the workflow with the given ID as done.
Expand Down Expand Up @@ -331,8 +329,7 @@ func (s *RPC) Done(c context.Context, strWorkflowID string, state rpc.WorkflowSt
if err != nil {
return err
}
agent.LastWork = time.Now().Unix()
return s.store.AgentUpdate(agent)
return s.updateAgentLastWork(agent)
}

// Log writes a log entry to the database and publishes it to the pubsub.
Expand Down Expand Up @@ -362,8 +359,9 @@ func (s *RPC) Log(c context.Context, rpcLogEntry *rpc.LogEntry) error {
if err != nil {
return err
}
agent.LastWork = time.Now().Unix()
if err := s.store.AgentUpdate(agent); err != nil {

err = s.updateAgentLastWork(agent)
if err != nil {
return err
}

Expand Down Expand Up @@ -510,3 +508,17 @@ func (s *RPC) getHostnameFromContext(ctx context.Context) (string, error) {
}
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
}

0 comments on commit b8c1d68

Please sign in to comment.