Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only update agent.LastWork if not done recently #4031

Merged
merged 2 commits into from
Aug 14, 2024
Merged
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
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()) {
6543 marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

agent.LastWork = time.Now().Unix()
if err := s.store.AgentUpdate(agent); err != nil {
return err
}

return nil
}