Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/consumers.go
T
jschoubben 4b47e88369 RN25: runner_health reads the consumer facts mesh-sdk's ledger writes
The runner's own writer of its consumer's oldest unacknowledged delivery is
replaced by mesh-sdk's consumer ledger: each message the hub hands a handler is
noted delivered, and applied when the handler acknowledges or ends it; one it
asks again stays unacknowledged. The key form is the same, a key per consumer
by its name in the state consumers. A runner that heard no event yet is healthy.

Builds: design 47 G §76, §468, §495; lead answers A-166, A-176

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 05:42:33 +01:00

60 lines
2.4 KiB
Go

package pipeline
import (
"encoding/json"
"fmt"
"sort"
"strings"
"time"
"git.novox.be/novox/mesh-sdk/go/health"
"git.novox.be/novox/mesh-sdk/go/process"
)
// StateConsumers is the runner's old state section of its durable consumers' facts, a key per consumer, written by
// mesh-sdk's consumer ledger (design 47 G §468, §76, §495; A-164, A-166, A-176).
const StateConsumers = process.ConsumersState
// BehindAfter is how long a consumer's oldest unacknowledged message may wait before the runner's health check names it
// behind (§76).
const BehindAfter = 5 * time.Minute
// Health is the runner's health as its tool runner_health answers it (§468, §76, §495; A-164): healthy when its state
// pipelines answers, as show reads it, and each durable consumer's oldest unacknowledged message waits less than
// BehindAfter; otherwise unhealthy naming each consumer behind and how long.
func Health(pipelines, consumers Kept, now time.Time) health.Answer {
if _, err := pipelines.Keys(); err != nil {
return health.Unhealthy(fmt.Sprintf("The runner does not answer show: its state pipelines cannot be listed (%v).", err))
}
keys, err := consumers.Keys()
if err != nil {
return health.Unhealthy(fmt.Sprintf("The ages of the runner's consumers cannot be read from its state %s (%v).", StateConsumers, err))
}
if len(keys) == 0 {
return health.Healthy(fmt.Sprintf("The runner answers show and has heard no event yet: its state %s holds no consumer.", StateConsumers))
}
sort.Strings(keys)
var behind []string
for _, k := range keys {
raw, err := consumers.Value(k)
if err != nil {
return health.Unhealthy(fmt.Sprintf("The age of the consumer %s cannot be read (%v).", k, err))
}
var a process.ConsumerFacts
if raw != nil {
if err := json.Unmarshal(raw, &a); err != nil {
return health.Unhealthy(fmt.Sprintf("The age of the consumer %s does not read (%v).", k, err))
}
}
if !a.OldestUnacknowledged.IsZero() {
if age := now.Sub(a.OldestUnacknowledged); age >= BehindAfter {
behind = append(behind, fmt.Sprintf("%s (its oldest unacknowledged message waits %s)", k, age.Round(time.Second)))
}
}
}
if len(behind) > 0 {
return health.Unhealthy(fmt.Sprintf("The runner's consumers behind by %s or more: %s.", BehindAfter, strings.Join(behind, ", ")))
}
return health.Healthy(fmt.Sprintf("The runner answers show, and each of its %d consumers is less than %s behind.", len(keys), BehindAfter))
}