diff --git a/backend/jobs/jobs.go b/backend/jobs/jobs.go index 44e15cd..dba188a 100644 --- a/backend/jobs/jobs.go +++ b/backend/jobs/jobs.go @@ -39,12 +39,50 @@ func MustAdd(app core.App, name, cronExpr string, fn Func) { if err != nil { panic(fmt.Sprintf("job %q: %v", name, err)) } - app.Cron().MustAdd(name, cronExpr, func() { Run(app, name, fn) }) + runs := &background{} + app.Cron().MustAdd(name, cronExpr, func() { + runs.do(func() { Run(app, name, fn) }) + }) app.OnServe().BindFunc(func(se *core.ServeEvent) error { - go catchUp(se.App, name, schedule, fn, time.Now()) + now := time.Now() + go runs.do(func() { catchUp(se.App, name, schedule, fn, now) }) return se.Next() }) + + // the app waits for a running job before it closes its database, so a run + // never writes to a closed app + app.OnTerminate().BindFunc(func(e *core.TerminateEvent) error { + runs.stop() + return e.Next() + }) +} + +// background tracks one job's runs so the app can wait for them when it stops. +// Once stopped, new runs are skipped. +type background struct { + mu sync.Mutex + wg sync.WaitGroup + stopped bool +} + +func (b *background) do(f func()) { + b.mu.Lock() + if b.stopped { + b.mu.Unlock() + return + } + b.wg.Add(1) + b.mu.Unlock() + defer b.wg.Done() + f() +} + +func (b *background) stop() { + b.mu.Lock() + b.stopped = true + b.mu.Unlock() + b.wg.Wait() } // Run runs a job now and records the result. A run is skipped while the diff --git a/backend/jobs/jobs_test.go b/backend/jobs/jobs_test.go index b25e2e6..7b4debb 100644 --- a/backend/jobs/jobs_test.go +++ b/backend/jobs/jobs_test.go @@ -191,3 +191,39 @@ func TestSignInIsRateLimited(t *testing.T) { t.Errorf("rate limits = %+v", r) } } + +func TestShutdownWaitsForARunningJob(t *testing.T) { + a := newApp(t) + col, _ := a.FindCollectionByNameOrId(Collection) + rec := core.NewRecord(col) + rec.Set("name", "slow") + rec.Set("last_run", time.Now().Add(-time.Hour)) + if err := a.Save(rec); err != nil { + t.Fatal(err) + } + started, release := make(chan struct{}), make(chan struct{}) + MustAdd(a, "slow", "* * * * *", func(core.App) error { + close(started) + <-release + return nil + }) + if err := a.OnServe().Trigger(&core.ServeEvent{App: a}, func(*core.ServeEvent) error { return nil }); err != nil { + t.Fatal(err) + } + <-started + + stopped := make(chan time.Time) + go a.OnTerminate().Trigger(&core.TerminateEvent{App: a}, func(*core.TerminateEvent) error { + stopped <- lastRun(t, a, "slow").GetDateTime("last_run").Time() + return nil + }) + select { + case <-stopped: + t.Fatal("the app stopped while the job was still running") + case <-time.After(100 * time.Millisecond): + } + close(release) + if got := <-stopped; time.Since(got) > time.Minute { + t.Errorf("last_run = %v, want the run that just finished", got) + } +} diff --git a/workers/Dockerfile b/workers/Dockerfile index cb56115..524a5f1 100644 --- a/workers/Dockerfile +++ b/workers/Dockerfile @@ -4,8 +4,11 @@ # --- build: resolve the locked dependencies and build the kit's wheel --- # Base images and uv are pinned by digest; Dependabot moves the digests weekly. +# uv only runs on the build machine; its image has no armv7 build, so it is taken +# for the build platform instead of each target one. +FROM --platform=$BUILDPLATFORM ghcr.io/astral-sh/uv:0.11.32@sha256:df4cae8f3a96d175e2e5f992e597550000edbe78fdc2594d5cd8de1a217f504c AS uv FROM --platform=$BUILDPLATFORM python:3.15-slim@sha256:65a65fc7b30b06cb6df4930fd4650697cb23f4c2698ab81fc87e4b031bc36928 AS build -COPY --from=ghcr.io/astral-sh/uv:0.11.32@sha256:df4cae8f3a96d175e2e5f992e597550000edbe78fdc2594d5cd8de1a217f504c /uv /usr/local/bin/uv +COPY --from=uv /uv /usr/local/bin/uv WORKDIR /src COPY pyproject.toml uv.lock README.md ./ COPY src/ src/