diff --git a/server/grpc/rpc.go b/server/grpc/rpc.go index ca43ea3d6..bdd498b13 100644 --- a/server/grpc/rpc.go +++ b/server/grpc/rpc.go @@ -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 } @@ -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. @@ -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. @@ -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 } @@ -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 +}