diff --git a/internal/handler/notetaker.go b/internal/handler/notetaker.go index 2ef1d90a..2f7f68b4 100644 --- a/internal/handler/notetaker.go +++ b/internal/handler/notetaker.go @@ -52,19 +52,27 @@ func (h *Handler) deepgramKey(ctx context.Context) string { return key } -// enqueueJob inserts a background job for the worker. The jobs table has a UNIQUE(type, payload) -// index for dedup, so re-queuing the same (type, payload) — e.g. re-summarising a booking on a new -// recording or a manual regenerate — would otherwise fail the constraint. Instead, re-queue it: -// reset an existing matching job back to pending so the worker runs it again. (Notetaker-only; -// reminders/webhooks enqueue via their own paths.) +// enqueueJob inserts a background job for the worker. The jobs table dedups LIVE jobs only +// (the partial unique index ux_jobs_type_payload_live, migration 00065), so: +// - a finished (done/failed) job never conflicts - re-queuing the same (type, payload), +// e.g. re-summarising a booking on a new recording or a manual regenerate, inserts a +// fresh run; +// - a pending duplicate is pulled forward to run now; +// - a running duplicate is left alone, rather than reset to pending and run twice. +// +// The ON CONFLICT target must repeat the index's WHERE: SQLite only uses a partial index as +// an upsert target when the predicate matches, and rejected the bare (type, payload) target +// outright ("ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint") from +// 00065 on - every notetaker enqueue failed. (Notetaker-only; reminders/webhooks enqueue via +// their own paths.) func (h *Handler) enqueueJob(ctx context.Context, typ string, payload any) error { b, _ := json.Marshal(payload) _, err := h.db.ExecContext(ctx, ` INSERT INTO jobs (id, type, payload, run_at, status, attempts, max_attempts) VALUES (?, ?, ?, datetime('now'), 'pending', 0, 3) - ON CONFLICT(type, payload) DO UPDATE SET - status = 'pending', run_at = datetime('now'), attempts = 0, - last_error = NULL, locked_until = NULL`, + ON CONFLICT(type, payload) WHERE status IN ('pending', 'running') DO UPDATE SET + run_at = datetime('now'), attempts = 0, last_error = NULL, locked_until = NULL + WHERE jobs.status = 'pending'`, uid.New(), typ, string(b)) return err } diff --git a/internal/handler/notetaker_enqueue_internal_test.go b/internal/handler/notetaker_enqueue_internal_test.go new file mode 100644 index 00000000..06bc9d41 --- /dev/null +++ b/internal/handler/notetaker_enqueue_internal_test.go @@ -0,0 +1,70 @@ +package handler + +import ( + "log/slog" + "testing" + + "github.com/calnode/calnode/internal/db" +) + +// Migration 00065 replaced the full UNIQUE(type, payload) index with a partial one over +// live jobs. SQLite only uses a partial index as an upsert target when the ON CONFLICT +// clause repeats its WHERE, so enqueueJob must keep matching it - and re-enqueueing a +// finished job must start a fresh run rather than conflict with history. +func TestEnqueueJob_upsertsLiveJobsAndRequeuesFinishedOnes(t *testing.T) { + database, err := db.Open("sqlite://:memory:") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { database.Close() }) + if err := db.Migrate(database); err != nil { + t.Fatal(err) + } + h := New(database, slog.Default()) + payload := map[string]string{"recording_id": "rec-1"} + + if err := h.enqueueJob(t.Context(), "notetaker.transcribe", payload); err != nil { + t.Fatalf("first enqueue: %v", err) + } + if err := h.enqueueJob(t.Context(), "notetaker.transcribe", payload); err != nil { + t.Fatalf("re-enqueue of a pending job: %v", err) + } + var n int + if err := database.QueryRow(`SELECT COUNT(*) FROM jobs`).Scan(&n); err != nil { + t.Fatal(err) + } + if n != 1 { + t.Fatalf("pending duplicate created %d jobs, want 1", n) + } + + if _, err := database.Exec(`UPDATE jobs SET status = 'done'`); err != nil { + t.Fatal(err) + } + if err := h.enqueueJob(t.Context(), "notetaker.transcribe", payload); err != nil { + t.Fatalf("re-enqueue after completion: %v", err) + } + if err := database.QueryRow(`SELECT COUNT(*) FROM jobs WHERE status = 'pending'`).Scan(&n); err != nil { + t.Fatal(err) + } + if n != 1 { + t.Fatalf("pending jobs after re-run = %d, want 1", n) + } + + // A running job keeps running: resetting it to pending would run it twice. + if _, err := database.Exec(`UPDATE jobs SET status = 'running', attempts = 1 WHERE status = 'pending'`); err != nil { + t.Fatal(err) + } + if err := h.enqueueJob(t.Context(), "notetaker.transcribe", payload); err != nil { + t.Fatalf("enqueue while running: %v", err) + } + var running, live int + if err := database.QueryRow(`SELECT COUNT(*) FROM jobs WHERE status = 'running' AND attempts = 1`).Scan(&running); err != nil { + t.Fatal(err) + } + if err := database.QueryRow(`SELECT COUNT(*) FROM jobs WHERE status IN ('pending', 'running')`).Scan(&live); err != nil { + t.Fatal(err) + } + if running != 1 || live != 1 { + t.Fatalf("running = %d, live = %d; want the running job untouched and no duplicate", running, live) + } +}