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
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ jobs:
tags: shoppa:smoke
build-args: VERSION=${{ env.VERSION }}
cache-from: type=gha,scope=app
# Run the runtime stage fresh, so each release takes Alpine's latest security fixes.
no-cache-filters: runtime
cache-to: ${{ github.event_name == 'pull_request' && 'type=gha,mode=max,scope=app' || '' }}
- name: Build the worker for the smoke test
uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7
Expand All @@ -103,6 +105,8 @@ jobs:
load: true
tags: shoppa-worker:smoke
cache-from: type=gha,scope=worker
# Run the runtime stage fresh, so each release takes Debian's latest security fixes.
no-cache-filters: runtime
cache-to: ${{ github.event_name == 'pull_request' && 'type=gha,mode=max,scope=worker' || '' }}
- name: Smoke test
run: |
Expand Down
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH GOARM=${TARGETVARIANT#v} \
go build -trimpath -ldflags="-s -w -X github.com/selfhostit/shoppa/app.Version=$VERSION" -o /shoppa .

# --- runtime ---
FROM alpine:3.24
RUN apk add --no-cache ca-certificates tzdata \
FROM alpine:3.24 AS runtime
# Take Alpine's security fixes now rather than waiting for the base image to be rebuilt.
RUN apk upgrade --no-cache \
&& apk add --no-cache ca-certificates tzdata \
&& adduser -D -u 1000 app \
&& mkdir -p /app/pb_data && chown app:app /app/pb_data
WORKDIR /app
Expand Down
45 changes: 41 additions & 4 deletions backend/jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,53 @@ 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 {
// checked before serving, so it never outlives the app; only a run goes
// to the background, so a slow job doesn't hold up the start
// checked before serving; only the run goes to the background, so a
// slow job doesn't hold up the start
if due(se.App, name, schedule, time.Now()) {
go Run(se.App, name, fn)
go runs.do(func() { Run(se.App, name, fn) })
}
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)
}
}
6 changes: 5 additions & 1 deletion workers/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ RUN uv export --locked --no-dev --no-emit-project -o requirements.txt \
&& uv build --wheel -o dist

# --- runtime ---
FROM python:3.15-slim
FROM python:3.15-slim AS runtime
ENV PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1
# Take Debian's security fixes now rather than waiting for the base image to be rebuilt.
RUN apt-get update \
&& apt-get upgrade -y --no-install-recommends \
&& rm -rf /var/lib/apt/lists/*
COPY --from=build /src/requirements.txt /src/dist/ /tmp/build/
RUN pip install --no-cache-dir -r /tmp/build/requirements.txt \
&& pip install --no-cache-dir --no-deps /tmp/build/*.whl \
Expand Down
Loading