Skip to content

Commit

Permalink
Add timestamp for last work done by agent (#3844)
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten committed Jul 1, 2024
1 parent ba858d6 commit 918985c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cmd/server/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3967,6 +3967,10 @@ const docTemplate = `{
"last_contact": {
"type": "integer"
},
"last_work": {
"description": "last time the agent did something, this value is used to determine if the agent is still doing work used by the autoscaler",
"type": "integer"
},
"name": {
"type": "string"
},
Expand Down
32 changes: 30 additions & 2 deletions server/grpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ func (s *RPC) Wait(c context.Context, workflowID string) error {

// Extend extends the lease for the workflow with the given ID.
func (s *RPC) Extend(c context.Context, workflowID string) error {
agent, err := s.getAgentFromContext(c)
if err != nil {
return err
}

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

return s.queue.Extend(c, workflowID)
}

Expand Down Expand Up @@ -226,7 +237,8 @@ func (s *RPC) Init(c context.Context, strWorkflowID string, state rpc.WorkflowSt
}
s.updateForgeStatus(c, repo, currentPipeline, workflow)

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

// Done marks the workflow with the given ID as done.
Expand Down Expand Up @@ -315,7 +327,12 @@ func (s *RPC) Done(c context.Context, strWorkflowID string, state rpc.WorkflowSt
s.pipelineTime.WithLabelValues(repo.FullName, currentPipeline.Branch, string(workflow.State), workflow.Name).Set(float64(workflow.Finished - workflow.Started))
}

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

// Log writes a log entry to the database and publishes it to the pubsub.
Expand All @@ -332,13 +349,24 @@ func (s *RPC) Log(c context.Context, rpcLogEntry *rpc.LogEntry) error {
Data: rpcLogEntry.Data,
Type: model.LogEntryType(rpcLogEntry.Type),
}

// make sure writes to pubsub are non blocking (https://github.com/woodpecker-ci/woodpecker/blob/c919f32e0b6432a95e1a6d3d0ad662f591adf73f/server/logging/log.go#L9)
go func() {
// write line to listening web clients
if err := s.logger.Write(c, logEntry.StepID, logEntry); err != nil {
log.Error().Err(err).Msgf("rpc server could not write to logger")
}
}()

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

return server.Config.Services.LogStore.LogAppend(logEntry)
}

Expand Down
1 change: 1 addition & 0 deletions server/model/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Agent struct {
OwnerID int64 `json:"owner_id" xorm:"'owner_id'"`
Token string `json:"token" xorm:"token"`
LastContact int64 `json:"last_contact" xorm:"last_contact"`
LastWork int64 `json:"last_work" xorm:"last_work"` // last time the agent did something, this value is used to determine if the agent is still doing work used by the autoscaler
Platform string `json:"platform" xorm:"VARCHAR(100) 'platform'"`
Backend string `json:"backend" xorm:"VARCHAR(100) 'backend'"`
Capacity int32 `json:"capacity" xorm:"capacity"`
Expand Down
1 change: 1 addition & 0 deletions woodpecker-go/woodpecker/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ type (
OwnerID int64 `json:"owner_id"`
Token string `json:"token"`
LastContact int64 `json:"last_contact"`
LastWork int64 `json:"last_work"`
Platform string `json:"platform"`
Backend string `json:"backend"`
Capacity int32 `json:"capacity"`
Expand Down

0 comments on commit 918985c

Please sign in to comment.