internal/jobs keeps named locks as leases in the database (schema 31: locks, instances), taken with one conditional UPDATE on SQLite or Postgres, renewed while the work runs and lapsing if its copy stops. Each background loop's turn, sending an email, audits, discovery runs and imports run under a lock; OAuth sign-ins under way are kept in the database. Copies check in, and work marked running is recovered at start only when no other copy is alive. Settings -> About lists the copies and work under way. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
170 lines
4.2 KiB
Go
170 lines
4.2 KiB
Go
package jobs
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"log/slog"
|
|
"path/filepath"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"supermarket/internal/db"
|
|
)
|
|
|
|
func setup(t *testing.T) *db.DB {
|
|
t.Helper()
|
|
d, err := db.Open(filepath.Join(t.TempDir(), "app.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { d.Close() })
|
|
return d
|
|
}
|
|
|
|
var quiet = slog.New(slog.NewTextHandler(io.Discard, nil))
|
|
|
|
func runner(t *testing.T, d *db.DB) *Runner {
|
|
t.Helper()
|
|
r, err := New(d, "test", quiet)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return r
|
|
}
|
|
|
|
// Two copies of the app: only one does a job at a time.
|
|
func TestOneAtATime(t *testing.T) {
|
|
d := setup(t)
|
|
a, b := runner(t, d), runner(t, d)
|
|
ctx := context.Background()
|
|
wctx, release, err := a.TryLock(ctx, "job:seo")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, err := b.TryLock(ctx, "job:seo"); !errors.Is(err, ErrBusy) {
|
|
t.Fatalf("the other copy took it: %v", err)
|
|
}
|
|
if _, _, err := a.TryLock(ctx, "job:seo"); !errors.Is(err, ErrBusy) { // not re-entrant, even in one copy
|
|
t.Fatalf("taken twice in one copy: %v", err)
|
|
}
|
|
if ran, _ := b.Do(ctx, "job:seo", func(context.Context) {}); ran {
|
|
t.Fatal("Do ran while busy")
|
|
}
|
|
held, _ := b.Locks()
|
|
if len(held) != 1 || held[0].Name != "job:seo" || held[0].Instance != a.ID() {
|
|
t.Fatalf("held: %+v", held)
|
|
}
|
|
release()
|
|
if wctx.Err() == nil {
|
|
t.Error("the work's context goes on after release")
|
|
}
|
|
if ran, err := b.Do(ctx, "job:seo", func(context.Context) {}); !ran || err != nil {
|
|
t.Fatalf("free again: %v %v", ran, err)
|
|
}
|
|
|
|
// Many goroutines, two copies: never two at once.
|
|
var inside, most, runs int32
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 20; i++ {
|
|
wg.Add(1)
|
|
r := a
|
|
if i%2 == 1 {
|
|
r = b
|
|
}
|
|
go func() {
|
|
defer wg.Done()
|
|
_, release, err := r.Lock(ctx, "outreach:send")
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
n := atomic.AddInt32(&inside, 1)
|
|
for {
|
|
m := atomic.LoadInt32(&most)
|
|
if n <= m || atomic.CompareAndSwapInt32(&most, m, n) {
|
|
break
|
|
}
|
|
}
|
|
time.Sleep(2 * time.Millisecond)
|
|
atomic.AddInt32(&inside, -1)
|
|
atomic.AddInt32(&runs, 1)
|
|
release()
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
if most != 1 || runs != 20 {
|
|
t.Errorf("at most %d at once, %d runs", most, runs)
|
|
}
|
|
}
|
|
|
|
// A copy that stops holding a lock: once its lease lapses, another takes it.
|
|
func TestLapsedLease(t *testing.T) {
|
|
d := setup(t)
|
|
a, b := runner(t, d), runner(t, d)
|
|
clock := time.Now()
|
|
a.now = func() time.Time { return clock }
|
|
b.now = func() time.Time { return clock }
|
|
if _, _, err := a.TryLock(context.Background(), "job:metrics"); err != nil { // never released: a crashed
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, err := b.TryLock(context.Background(), "job:metrics"); !errors.Is(err, ErrBusy) {
|
|
t.Fatal("taken before the lease lapsed")
|
|
}
|
|
clock = clock.Add(Lease + time.Second)
|
|
if _, release, err := b.TryLock(context.Background(), "job:metrics"); err != nil {
|
|
t.Fatalf("not taken after it lapsed: %v", err)
|
|
} else {
|
|
release()
|
|
}
|
|
}
|
|
|
|
// Lock gives up when its context ends.
|
|
func TestLockWaits(t *testing.T) {
|
|
d := setup(t)
|
|
a := runner(t, d)
|
|
_, release, _ := a.TryLock(context.Background(), "x")
|
|
defer release()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond)
|
|
defer cancel()
|
|
if _, _, err := a.Lock(ctx, "x"); !errors.Is(err, ErrBusy) {
|
|
t.Fatalf("lock: %v", err)
|
|
}
|
|
}
|
|
|
|
// Copies see each other; one that stops checking in is gone.
|
|
func TestInstances(t *testing.T) {
|
|
d := setup(t)
|
|
a := runner(t, d)
|
|
if alone, err := a.Alone(); !alone || err != nil {
|
|
t.Fatalf("alone: %v %v", alone, err)
|
|
}
|
|
b := runner(t, d)
|
|
if alone, _ := a.Alone(); alone {
|
|
t.Fatal("doesn't see the other copy")
|
|
}
|
|
list, _ := a.Instances()
|
|
if len(list) != 2 || !list[0].This || list[1].ID != b.ID() || list[1].Version != "test" {
|
|
t.Fatalf("instances: %+v", list)
|
|
}
|
|
a.now = func() time.Time { return time.Now().Add(Gone + time.Minute) } // b hasn't checked in since
|
|
if alone, _ := a.Alone(); !alone {
|
|
t.Error("a stopped copy still counts")
|
|
}
|
|
|
|
// Stopping: the check-in and the copy's locks go.
|
|
if _, _, err := b.TryLock(context.Background(), "job:seo"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b.Stop()
|
|
a.now = time.Now
|
|
if held, _ := a.Locks(); len(held) != 0 {
|
|
t.Errorf("still held: %+v", held)
|
|
}
|
|
if list, _ := a.Instances(); len(list) != 1 {
|
|
t.Errorf("instances: %+v", list)
|
|
}
|
|
}
|