Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions backend/jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions backend/jobs/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
5 changes: 4 additions & 1 deletion workers/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
Loading