archie-core-8cda.6.8's carrier. The service now records channel lifecycle and has nowhere to put it: /api/channels answered with nothing because webui.Server.Channels had no production source, and reload answered 501 for the same reason. This adds the surface the register decided on, following the split archie-core-ymut established for config snapshots -- a pair of administrative RPCs whose writer is one process and whose reader is the UI -- and wires both ends. Wire and store: PutChannelStatus / ListChannelStatus, with ChannelStatus carrying id, name, state, detail, configured, reload_supported and observed_at. The store holds one row per channel and replaces the reported set, so a channel the reporter no longer mentions is deleted rather than left standing as a stale "running"; an empty report clears the table, because hosting no channels is a fact worth reporting. A row without an id is refused: it could never be replaced or removed by a later report. Writer: the service publishes on every lifecycle transition through a coalescing signal -- a burst of transitions is one write, since a reader only wants the latest state of every channel -- plus a start-up report and a final report after shutdown, so a stopped service leaves the store honest. A failed write is logged and retried on the next signal; the dashboard losing channel state is worth a warning and never worth failing a channel start over. Reader: webui.Server.Channels becomes a source rather than a concrete manager, and webui.RemoteChannelStatus reads the store the way RemoteConfigView reads the config snapshot. A store it cannot reach reports nothing rather than a stale "running", and a state the reader does not recognise is carried through rather than dropped. The contract's own index gains the group in the same change that adds the RPCs -- a surface the grouped table does not list is one nobody finds -- and the count moves to 51. The register records why the surface is the State Store rather than a resource kind, and why reload gets its own surface rather than a field here: reading status must not be able to retrigger an action, a transient request is not distinguishable from state once anything polls it, and it would invert the writer/reader relationship the precedent establishes. Verified: task check exit 0. New coverage: the store's replace-the-set semantics and its refusal of an id-less row, the reader's mapping including an unknown state, and the publisher's start-up, transition and final reports.
107 lines
3.5 KiB
Go
107 lines
3.5 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
const channelStatusSchema = `
|
|
CREATE TABLE IF NOT EXISTS channel_status (
|
|
id TEXT PRIMARY KEY,
|
|
name TEXT NOT NULL DEFAULT '',
|
|
state TEXT NOT NULL DEFAULT '',
|
|
detail TEXT NOT NULL DEFAULT '',
|
|
configured INTEGER NOT NULL DEFAULT 0,
|
|
reload_supported INTEGER NOT NULL DEFAULT 0,
|
|
observed_at TEXT NOT NULL
|
|
);
|
|
`
|
|
|
|
// PutChannelStatus records the reporting process's whole set, in one
|
|
// transaction: rows it no longer reports are deleted, so a channel that stopped
|
|
// or was removed does not linger as a stale "running" the dashboard would show
|
|
// indefinitely.
|
|
func (s *Store) PutChannelStatus(ctx context.Context, channels []ChannelStatus) error {
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("store: put channel status: begin: %w", err)
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
reported := make([]any, 0, len(channels)+1)
|
|
placeholders := make([]byte, 0, len(channels)*2)
|
|
for index, channel := range channels {
|
|
if channel.ID == "" {
|
|
return fmt.Errorf("store: put channel status: a channel needs an id")
|
|
}
|
|
if index > 0 {
|
|
placeholders = append(placeholders, ',')
|
|
}
|
|
placeholders = append(placeholders, '?')
|
|
reported = append(reported, channel.ID)
|
|
|
|
observed := channel.ObservedAt
|
|
if observed.IsZero() {
|
|
observed = time.Now()
|
|
}
|
|
if _, err := tx.ExecContext(ctx, `
|
|
INSERT INTO channel_status (id, name, state, detail, configured, reload_supported, observed_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT(id) DO UPDATE SET
|
|
name=excluded.name,
|
|
state=excluded.state,
|
|
detail=excluded.detail,
|
|
configured=excluded.configured,
|
|
reload_supported=excluded.reload_supported,
|
|
observed_at=excluded.observed_at`,
|
|
channel.ID, channel.Name, channel.State, channel.Detail,
|
|
channel.Configured, channel.ReloadSupported, observed.UTC().Format(configSnapshotTimeLayout)); err != nil {
|
|
return fmt.Errorf("store: put channel status %s: %w", channel.ID, err)
|
|
}
|
|
}
|
|
|
|
// An empty report is a real report -- the process hosts no channels -- and it
|
|
// clears the table rather than leaving the previous set standing.
|
|
query := `DELETE FROM channel_status`
|
|
if len(reported) > 0 {
|
|
query += ` WHERE id NOT IN (` + string(placeholders) + `)`
|
|
}
|
|
if _, err := tx.ExecContext(ctx, query, reported...); err != nil {
|
|
return fmt.Errorf("store: put channel status: prune: %w", err)
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
// ChannelStatus returns every reported channel, ordered by id so a reader and a
|
|
// test see the same order twice.
|
|
func (s *Store) ChannelStatus(ctx context.Context) ([]ChannelStatus, error) {
|
|
rows, err := s.db.QueryContext(ctx, `
|
|
SELECT id, name, state, detail, configured, reload_supported, observed_at
|
|
FROM channel_status ORDER BY id`)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: list channel status: %w", err)
|
|
}
|
|
defer func() { _ = rows.Close() }()
|
|
|
|
var channels []ChannelStatus
|
|
for rows.Next() {
|
|
var (
|
|
channel ChannelStatus
|
|
observed string
|
|
)
|
|
if err := rows.Scan(&channel.ID, &channel.Name, &channel.State, &channel.Detail,
|
|
&channel.Configured, &channel.ReloadSupported, &observed); err != nil {
|
|
return nil, fmt.Errorf("store: scan channel status: %w", err)
|
|
}
|
|
if at, err := time.Parse(configSnapshotTimeLayout, observed); err == nil {
|
|
channel.ObservedAt = at
|
|
}
|
|
channels = append(channels, channel)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("store: list channel status: %w", err)
|
|
}
|
|
return channels, nil
|
|
}
|