feat(conga,dev): worker and scheduler reports, summer dev runs serve

- queue:work and schedule:run print a startup summary and a shutdown
  report on a terminal, log them as records otherwise, and return nil
  after a clean stop
- schedule:run --once ends with 'Ran N due entries (M failed)'
- summer dev runs bin/<app> serve with the forwarded global options,
  reports through Output, stops the old server before starting the new
  one and prints a dev report on Ctrl+C (exit 0)
- lagoon.DatabaseLabel describes a DSN without its secrets
- Long help and examples for queue:work, schedule:run, dev, docs:serve
This commit is contained in:
Jakub Zych
2026-10-21 13:04:27 +02:00
parent c38dc1fe48
commit 168d65cd1b
15 changed files with 319 additions and 81 deletions

View File

@@ -14,23 +14,44 @@ import (
"github.com/fsnotify/fsnotify"
"git.golem15.com/golem15/summercms/internal/build"
"git.golem15.com/golem15/summercms/modules/bonfire"
)
const defaultDebounce = 200 * time.Millisecond
// Options customizes the watch loop. Tests inject Build and Start.
type Options struct {
Build func(ctx context.Context, appDir string, out io.Writer) error
Start func(ctx context.Context, binPath string, stdout, stderr io.Writer) (*exec.Cmd, error)
Build func(ctx context.Context, appDir string, out io.Writer) error
// Start runs binPath with args ("serve" followed by the forwarded
// global options).
Start func(ctx context.Context, binPath string, args []string, stdout, stderr io.Writer) (*exec.Cmd, error)
Debounce time.Duration
}
// Watch rebuilds the app on source changes and restarts the child binary.
func Watch(ctx context.Context, appDir string, out io.Writer) error {
return watch(ctx, appDir, out, Options{})
// Watch builds the app, runs bin/<binary> serve with args (the global
// options summer dev was given, such as -v) and rebuilds and restarts it on
// source changes. When ctx ends it stops the child with SIGTERM (serve
// prints its own shutdown report), prints the dev report (rebuilds, failed
// builds, restarts, uptime) and returns nil.
func Watch(ctx context.Context, appDir string, out bonfire.Output, args []string) error {
return watch(ctx, appDir, out, args, Options{})
}
func watch(ctx context.Context, appDir string, out io.Writer, opts Options) error {
// devReport counts what a watch session did.
type devReport struct {
rebuilds, failed, restarts int
started time.Time
}
func (r devReport) details() []bonfire.Detail {
return []bonfire.Detail{
{Label: "Rebuilds", Value: fmt.Sprintf("%d (%d failed)", r.rebuilds+r.failed, r.failed), Warn: r.failed > 0},
{Label: "Restarts", Value: fmt.Sprint(r.restarts)},
{Label: "Uptime", Value: time.Since(r.started).Round(time.Second).String()},
}
}
func watch(ctx context.Context, appDir string, out bonfire.Output, args []string, opts Options) error {
if appDir == "" {
return fmt.Errorf("dev: app directory is empty")
}
@@ -40,7 +61,7 @@ func watch(ctx context.Context, appDir string, out io.Writer, opts Options) erro
}
appDir = abs
if out == nil {
out = os.Stdout
out = bonfire.NewOutput(os.Stdin, os.Stdout, os.Stderr)
}
if opts.Build == nil {
opts.Build = build.App
@@ -57,6 +78,14 @@ func watch(ctx context.Context, appDir string, out io.Writer, opts Options) erro
return err
}
binPath := filepath.Join(appDir, "bin", m.Binary)
childArgs := append([]string{"serve"}, args...)
// On a terminal the child gets the process's own stdout and stderr, so
// serve sees a terminal and prints its styled summary and report.
childOut, childErr := io.Writer(out), io.Writer(out)
if bonfire.IsTerminal(out) {
childOut, childErr = os.Stdout, os.Stderr
}
report := devReport{started: time.Now()}
watcher, err := fsnotify.NewWatcher()
if err != nil {
@@ -70,26 +99,35 @@ func watch(ctx context.Context, appDir string, out io.Writer, opts Options) erro
var current *child
defer func() { current.stop() }()
out.Info(fmt.Sprintf("Watching %s; running bin/%s %s", appDir, m.Binary, strings.Join(childArgs, " ")))
rebuild := func() {
if ctx.Err() != nil {
return
}
start := time.Now()
if err := opts.Build(ctx, appDir, out); err != nil {
fmt.Fprintf(out, "build error: %v\n", err)
report.failed++
out.Error(fmt.Sprintf("Build failed: %v", err))
return
}
fmt.Fprintf(out, "rebuild: %s\n", time.Since(start).Round(time.Millisecond))
report.rebuilds++
out.Success(fmt.Sprintf("Rebuilt in %s", time.Since(start).Round(time.Millisecond)))
if ctx.Err() != nil {
return
}
next, err := startChild(ctx, opts, binPath, out)
restart := current != nil
current.stop()
current = nil
next, err := startChild(ctx, opts, binPath, childArgs, childOut, childErr)
if err != nil {
fmt.Fprintf(out, "start error: %v\n", err)
out.Error(fmt.Sprintf("Start failed: %v", err))
return
}
current.stop()
current = next
if restart {
report.restarts++
out.Info("Restarted bin/" + m.Binary)
}
}
rebuild()
@@ -145,9 +183,10 @@ func watch(ctx context.Context, appDir string, out io.Writer, opts Options) erro
case <-ctx.Done():
current.stop()
current = nil
return ctx.Err()
bonfire.Details(out, "Dev report", report.details())
return nil
case err := <-watchErrs:
fmt.Fprintf(out, "watch error: %v\n", err)
out.Warning(fmt.Sprintf("Watch error: %v", err))
case <-events:
if timer == nil {
timer = time.NewTimer(opts.Debounce)
@@ -172,8 +211,8 @@ type child struct {
wait <-chan error
}
func startChild(ctx context.Context, opts Options, binPath string, out io.Writer) (*child, error) {
cmd, err := opts.Start(ctx, binPath, out, out)
func startChild(ctx context.Context, opts Options, binPath string, args []string, stdout, stderr io.Writer) (*child, error) {
cmd, err := opts.Start(ctx, binPath, args, stdout, stderr)
if err != nil {
return nil, err
}
@@ -198,8 +237,10 @@ func (c *child) stop() {
}
}
func startBin(_ context.Context, binPath string, stdout, stderr io.Writer) (*exec.Cmd, error) {
cmd := exec.Command(binPath)
// startBin runs binPath with args, which watch builds as "serve" followed
// by the forwarded global options.
func startBin(_ context.Context, binPath string, args []string, stdout, stderr io.Writer) (*exec.Cmd, error) {
cmd := exec.Command(binPath, args...)
cmd.Stdout = stdout
cmd.Stderr = stderr
cmd.Stdin = os.Stdin

View File

@@ -16,6 +16,7 @@ import (
"time"
"git.golem15.com/golem15/summercms/internal/build"
"git.golem15.com/golem15/summercms/modules/bonfire"
)
type safeBuffer struct {
@@ -35,6 +36,11 @@ func (s *safeBuffer) String() string {
return s.b.String()
}
// testOutput is a non-terminal bonfire console writing to w.
func testOutput(w io.Writer) bonfire.Output {
return bonfire.NewOutput(strings.NewReader(""), w, w)
}
func TestWatchRebuildsOnSourceEdit(t *testing.T) {
dir := t.TempDir()
writeWatchApp(t, dir)
@@ -53,7 +59,10 @@ func TestWatchRebuildsOnSourceEdit(t *testing.T) {
fmt.Fprintln(w, "built hello in 1ms")
return nil
},
Start: func(ctx context.Context, binPath string, stdout, stderr io.Writer) (*exec.Cmd, error) {
Start: func(ctx context.Context, binPath string, args []string, stdout, stderr io.Writer) (*exec.Cmd, error) {
if got := strings.Join(args, " "); got != "serve -v --env staging" {
return nil, fmt.Errorf("start args = %q, want serve and the forwarded globals", got)
}
starts.Add(1)
cmd := exec.Command("sleep", "30")
if err := cmd.Start(); err != nil {
@@ -64,7 +73,7 @@ func TestWatchRebuildsOnSourceEdit(t *testing.T) {
}
done := make(chan error, 1)
go func() { done <- watch(ctx, dir, &out, opts) }()
go func() { done <- watch(ctx, dir, testOutput(&out), []string{"-v", "--env", "staging"}, opts) }()
waitUntil(t, "initial start", func() bool { return starts.Load() == 1 })
if builds.Load() != 1 {
@@ -91,15 +100,18 @@ func TestWatchRebuildsOnSourceEdit(t *testing.T) {
}
got := out.String()
if strings.Count(got, "rebuild:") != 2 {
if strings.Count(got, "Rebuilt in ") != 2 {
t.Fatalf("want 2 rebuild latency lines, got %q", got)
}
cancel()
select {
case err := <-done:
if err != nil && err != context.Canceled {
t.Fatalf("watch exit: %v", err)
if err != nil {
t.Fatalf("watch returned %v after Ctrl+C, want nil (exit 0)", err)
}
if !strings.Contains(out.String(), "Dev report") || !strings.Contains(out.String(), "Restarts 1") {
t.Fatalf("missing dev report:\n%s", out.String())
}
case <-time.After(3 * time.Second):
t.Fatal("watch did not exit on cancel")
@@ -127,7 +139,7 @@ func TestWatchKeepsChildOnBuildFailure(t *testing.T) {
}
return nil
},
Start: func(ctx context.Context, binPath string, stdout, stderr io.Writer) (*exec.Cmd, error) {
Start: func(ctx context.Context, binPath string, args []string, stdout, stderr io.Writer) (*exec.Cmd, error) {
starts.Add(1)
cmd := exec.Command("sleep", "30")
if err := cmd.Start(); err != nil {
@@ -139,7 +151,7 @@ func TestWatchKeepsChildOnBuildFailure(t *testing.T) {
}
done := make(chan error, 1)
go func() { done <- watch(ctx, dir, &out, opts) }()
go func() { done <- watch(ctx, dir, testOutput(&out), nil, opts) }()
waitUntil(t, "initial start", func() bool { return starts.Load() == 1 })
if err := os.WriteFile(filepath.Join(dir, "plugin.go"), []byte("package p\n// bad\n"), 0o644); err != nil {
@@ -192,7 +204,7 @@ func TestWatchDebouncesRapidEdits(t *testing.T) {
builds.Add(1)
return nil
},
Start: func(ctx context.Context, binPath string, stdout, stderr io.Writer) (*exec.Cmd, error) {
Start: func(ctx context.Context, binPath string, args []string, stdout, stderr io.Writer) (*exec.Cmd, error) {
cmd := exec.Command("sleep", "30")
if err := cmd.Start(); err != nil {
return nil, err
@@ -201,7 +213,7 @@ func TestWatchDebouncesRapidEdits(t *testing.T) {
},
}
done := make(chan error, 1)
go func() { done <- watch(ctx, dir, &out, opts) }()
go func() { done <- watch(ctx, dir, testOutput(&out), nil, opts) }()
waitUntil(t, "initial build", func() bool { return builds.Load() == 1 })
for i := 0; i < 5; i++ {
@@ -243,7 +255,7 @@ func TestWatchIgnoresBinAndTmp(t *testing.T) {
builds.Add(1)
return nil
},
Start: func(ctx context.Context, binPath string, stdout, stderr io.Writer) (*exec.Cmd, error) {
Start: func(ctx context.Context, binPath string, args []string, stdout, stderr io.Writer) (*exec.Cmd, error) {
cmd := exec.Command("sleep", "30")
if err := cmd.Start(); err != nil {
return nil, err
@@ -252,7 +264,7 @@ func TestWatchIgnoresBinAndTmp(t *testing.T) {
},
}
done := make(chan error, 1)
go func() { done <- watch(ctx, dir, &out, opts) }()
go func() { done <- watch(ctx, dir, testOutput(&out), nil, opts) }()
waitUntil(t, "initial build", func() bool { return builds.Load() == 1 })
if err := os.WriteFile(filepath.Join(dir, "bin", "hello.go"), []byte("package main\n"), 0o644); err != nil {
@@ -290,7 +302,7 @@ func TestWatchHelloWorkspaceRebuildLatency(t *testing.T) {
}
return nil
},
Start: func(ctx context.Context, binPath string, stdout, stderr io.Writer) (*exec.Cmd, error) {
Start: func(ctx context.Context, binPath string, args []string, stdout, stderr io.Writer) (*exec.Cmd, error) {
starts.Add(1)
cmd := exec.Command("sleep", "30")
if err := cmd.Start(); err != nil {
@@ -301,7 +313,7 @@ func TestWatchHelloWorkspaceRebuildLatency(t *testing.T) {
}
done := make(chan error, 1)
go func() { done <- watch(ctx, dir, &out, opts) }()
go func() { done <- watch(ctx, dir, testOutput(&out), nil, opts) }()
waitUntilTimeout(t, 60*time.Second, "initial real rebuild", func() bool { return starts.Load() == 1 })
if builds.Load() != 1 {
t.Fatalf("initial builds = %d", builds.Load())
@@ -320,7 +332,7 @@ func TestWatchHelloWorkspaceRebuildLatency(t *testing.T) {
}
waitUntilTimeout(t, 60*time.Second, "rebuild after source edit", func() bool { return starts.Load() == 2 })
got := out.String()
if strings.Count(got, "rebuild:") < 2 {
if strings.Count(got, "Rebuilt in ") < 2 {
t.Fatalf("want rebuild latency lines after restart, got %q", got)
}
if !rebuildLinePresent(got) {
@@ -352,7 +364,7 @@ func TestWatchHelloWorkspaceRebuildLatency(t *testing.T) {
func rebuildLinePresent(s string) bool {
for _, line := range strings.Split(s, "\n") {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "rebuild:") && len(strings.TrimSpace(strings.TrimPrefix(line, "rebuild:"))) > 0 {
if _, d, ok := strings.Cut(line, "Rebuilt in "); ok && strings.TrimSpace(d) != "" {
return true
}
}