Add durable run liveness assessment
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.cin.su/m/Assistant/contracts/work"
|
||||
)
|
||||
|
||||
type RunLivenessState string
|
||||
|
||||
const (
|
||||
RunLivenessLive RunLivenessState = "LIVE"
|
||||
RunLivenessStale RunLivenessState = "STALE"
|
||||
RunLivenessInvalid RunLivenessState = "INVALID"
|
||||
RunLivenessNotRun RunLivenessState = "NOT_RUNNING"
|
||||
)
|
||||
|
||||
type RunLiveness struct {
|
||||
State RunLivenessState
|
||||
Age time.Duration
|
||||
ObservedAt time.Time
|
||||
}
|
||||
|
||||
func AssessRunLiveness(run work.AgentRun, observedAt time.Time, heartbeatTimeout time.Duration) RunLiveness {
|
||||
result := RunLiveness{ObservedAt: observedAt}
|
||||
if observedAt.IsZero() || heartbeatTimeout <= 0 || run.UpdatedAtUnixMillis < 1 {
|
||||
result.State = RunLivenessInvalid
|
||||
return result
|
||||
}
|
||||
if run.State != work.AgentRunStarted && run.State != work.AgentRunRunning {
|
||||
result.State = RunLivenessNotRun
|
||||
return result
|
||||
}
|
||||
updated := time.UnixMilli(run.UpdatedAtUnixMillis)
|
||||
result.Age = observedAt.Sub(updated)
|
||||
if result.Age < 0 {
|
||||
result.State = RunLivenessInvalid
|
||||
return result
|
||||
}
|
||||
result.State = RunLivenessLive
|
||||
if result.Age > heartbeatTimeout {
|
||||
result.State = RunLivenessStale
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user