package probeagent import ( "encoding/json" "errors" "os" "path/filepath" "sync" "time" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/wire" ) // State is the probe's only durable bookkeeping besides the spool. type State struct { Seq uint64 `json:"seq"` LastCmd uint64 `json:"last_cmd"` Positions map[string]wire.Pos `json:"positions"` Dropped uint64 `json:"dropped"` Alive int64 `json:"alive"` // unix seconds, refreshed by the process; read by Check } type stateStore struct { mu sync.Mutex path string s State } func loadState(dir string) (*stateStore, error) { st := &stateStore{path: filepath.Join(dir, "state.json")} st.s.Positions = map[string]wire.Pos{} b, err := os.ReadFile(st.path) if errors.Is(err, os.ErrNotExist) { return st, nil } if err != nil { return nil, err } if err := json.Unmarshal(b, &st.s); err != nil { // a corrupt state must not keep the probe down: start clean, the hub's // offsets in the hello cover the gap. st.s = State{} } if st.s.Positions == nil { st.s.Positions = map[string]wire.Pos{} } return st, nil } // saveLocked writes state.json atomically. Caller holds mu. func (st *stateStore) saveLocked() error { st.s.Alive = time.Now().Unix() b, err := json.Marshal(&st.s) if err != nil { return err } tmp := st.path + ".tmp" f, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600) if err != nil { return err } if _, err := f.Write(b); err != nil { f.Close() return err } if err := f.Sync(); err != nil { f.Close() return err } if err := f.Close(); err != nil { return err } return os.Rename(tmp, st.path) } func (st *stateStore) touch() error { st.mu.Lock() defer st.mu.Unlock() return st.saveLocked() } func (st *stateStore) snapshot() State { st.mu.Lock() defer st.mu.Unlock() c := st.s c.Positions = make(map[string]wire.Pos, len(st.s.Positions)) for k, v := range st.s.Positions { c.Positions[k] = v } return c } func (st *stateStore) pos(src string) (wire.Pos, bool) { st.mu.Lock() defer st.mu.Unlock() p, ok := st.s.Positions[src] return p, ok } func (st *stateStore) lastCmd() uint64 { st.mu.Lock() defer st.mu.Unlock() return st.s.LastCmd } func (st *stateStore) setLastCmd(n uint64) error { st.mu.Lock() defer st.mu.Unlock() st.s.LastCmd = n return st.saveLocked() } func (st *stateStore) dropped() uint64 { st.mu.Lock() defer st.mu.Unlock() return st.s.Dropped } func (st *stateStore) addDropped(n uint64) { if n == 0 { return } st.mu.Lock() defer st.mu.Unlock() st.s.Dropped += n _ = st.saveLocked() } // raiseSeq makes sure new batches are numbered above what the hub already // applied (state lost on the probe, or hub ahead). func (st *stateStore) raiseSeq(n uint64) { st.mu.Lock() defer st.mu.Unlock() if n > st.s.Seq { st.s.Seq = n _ = st.saveLocked() } } func (st *stateStore) lastSeq() uint64 { st.mu.Lock() defer st.mu.Unlock() return st.s.Seq } // commit allocates the next batch seq. persist is called with it before the // positions move, so a spooled batch always has its position recorded. func (st *stateStore) commit(build func(seq uint64) (dropped uint64), pos map[string]wire.Pos) { st.mu.Lock() defer st.mu.Unlock() st.s.Seq++ st.s.Dropped += build(st.s.Seq) for k, v := range pos { st.s.Positions[k] = v } _ = st.saveLocked() } // Check is the container healthcheck: the process refreshes state.json every // few seconds, so a stale or missing file means it is not running. func Check(stateDir string) error { b, err := os.ReadFile(filepath.Join(stateDir, "state.json")) if err != nil { return errors.New("probe state unreadable") } var s State if err := json.Unmarshal(b, &s); err != nil { return errors.New("probe state corrupt") } if age := time.Since(time.Unix(s.Alive, 0)); age > aliveMax { return errors.New("probe not alive") } return nil } const aliveMax = 90 * time.Second