This commit is contained in:
+18
-14
@@ -152,32 +152,36 @@ func summarize(v any) string {
|
||||
return "ok"
|
||||
}
|
||||
|
||||
// printOnce runs a single query under a timeout and prints the result as JSON.
|
||||
func printOnce(ctx context.Context, timeout time.Duration, q query) error {
|
||||
// callOnce runs a single query under a fresh timeout derived from ctx.
|
||||
func callOnce(ctx context.Context, timeout time.Duration, q query) (any, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
v, err := q(ctx)
|
||||
return q(ctx)
|
||||
}
|
||||
|
||||
// printOnce runs a single query and prints the result as JSON.
|
||||
func printOnce(ctx context.Context, timeout time.Duration, q query) error {
|
||||
v, err := callOnce(ctx, timeout, q)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return printJSON(v)
|
||||
}
|
||||
|
||||
// pollLoop prints a fresh result immediately and then once per interval until
|
||||
// the context is cancelled (Ctrl-C). Per-poll errors are logged, not fatal.
|
||||
// pollLoop ranges over client.Poll — the same interval engine the typed Poll*
|
||||
// methods use — printing each result as JSON until the context is cancelled
|
||||
// (Ctrl-C). Per-poll errors are logged, not fatal.
|
||||
func pollLoop(ctx context.Context, cfg config, q query) error {
|
||||
tick := time.NewTicker(cfg.interval)
|
||||
defer tick.Stop()
|
||||
for {
|
||||
if err := printOnce(ctx, cfg.timeout, q); err != nil && ctx.Err() == nil {
|
||||
fetch := func(ctx context.Context) (any, error) { return callOnce(ctx, cfg.timeout, q) }
|
||||
for v, err := range client.Poll(ctx, cfg.interval, fetch) {
|
||||
switch {
|
||||
case err != nil && ctx.Err() == nil:
|
||||
fmt.Fprintln(os.Stderr, "wingbits:", err)
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
case <-tick.C:
|
||||
case err == nil:
|
||||
printJSON(v)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func printJSON(v any) error {
|
||||
|
||||
Reference in New Issue
Block a user