beads/cmd/bd/ping.go
Dustin Brown 9ffe6a7fc7
feat: support compact/gc/ping/dolt clean-databases in proxied-server mode (#5003)
* /{cmd,internal}: wip, missing compact

* /cmd/bd: add compact support

* /cmd/bd: add tests

* address Copilot review feedback on proxied maintenance commands

- gc: track Dolt GC failure; report "GC completed with errors", set
  JSON success=false, and exit non-zero instead of always printing "GC complete"
- gc: scalarCount handles []byte/uint64/float64 in addition to string/int64
- proxied_maintenance: use HandleErrorRespectJSON so --json callers get JSON errors
- clean-databases: fail loudly on rows.Scan error instead of silently skipping
- test: check fmt.Sscanf error in proxiedCommitCount

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(proxied): readonly guard + JSON parity for compact --dolt

Maintainer follow-ups to the proxied maintenance commands:

- runCompactDoltProxiedServer: enforce CheckReadonly("compact") on the
  non-dry-run path, matching the embedded dispatch in compact.go and the
  other proxied handlers (gc, admin compact).
- runCompactDoltProxiedServer: route the Dolt GC failure through
  HandleErrorRespectJSON so --json callers get a structured error,
  consistent with the rest of this PR's proxied paths.
- runCompactProxiedServer: emit remote_refs_pruned / tags_anchoring as
  arrays in --json output, matching the embedded compact_dolt.go schema
  instead of emitting counts under the same keys.

Agent-Signature: claude-fable-5-high on behalf of maphew

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: matt wilkie <maphew@gmail.com>
2026-07-23 22:42:02 -07:00

103 lines
2.5 KiB
Go

package main
import (
"fmt"
"os"
"time"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/metrics"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/ui"
)
var pingCmd = &cobra.Command{
Use: "ping",
GroupID: "maint",
Short: "Check database connectivity",
Long: `Lightweight health check that confirms bd can reach its database.
Steps:
1. Resolve the .beads workspace
2. Open the store (embedded or server)
3. Run a trivial query (issue count)
4. Report timing
Exit 0 on success, exit 1 on failure.
Examples:
bd ping # Quick connectivity check
bd ping --json # Structured output for automation`,
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
if usesProxiedServer() {
return runPingProxiedServer(rootCtx)
}
evt := metrics.NewCommandEvent("ping")
defer func() {
if c := metrics.Global(); c != nil {
c.CloseEventAndAdd(evt)
}
}()
start := time.Now()
beadsDir := beads.FindBeadsDir()
if beadsDir == "" {
return pingFail(start, "no .beads directory found")
}
resolveMs := time.Since(start).Milliseconds()
st := getStore()
if st == nil {
return pingFail(start, "store not initialized")
}
if lm, ok := storage.UnwrapStore(st).(storage.LifecycleManager); ok && lm.IsClosed() {
return pingFail(start, "store is closed")
}
storeMs := time.Since(start).Milliseconds()
filter := types.IssueFilter{Limit: 1}
_, err := st.SearchIssues(rootCtx, "", filter)
if err != nil {
return pingFail(start, fmt.Sprintf("query failed: %v", err))
}
totalMs := time.Since(start).Milliseconds()
queryMs := totalMs - storeMs
if jsonOutput {
return outputJSON(map[string]interface{}{
"status": "ok",
"resolve_ms": resolveMs,
"store_ms": storeMs - resolveMs,
"query_ms": queryMs,
"total_ms": totalMs,
})
}
fmt.Fprintf(os.Stdout, "%s bd ping: ok (%dms)\n", ui.RenderPass("✓"), totalMs)
return nil
},
}
func pingFail(start time.Time, reason string) error {
totalMs := time.Since(start).Milliseconds()
if jsonOutput {
if jerr := outputJSON(map[string]interface{}{
"status": "error",
"error": reason,
"total_ms": totalMs,
}); jerr != nil {
return jerr
}
return SilentExit()
}
return HandleError("bd ping: %s (%dms)", reason, totalMs)
}
func init() {
rootCmd.AddCommand(pingCmd)
}