Add a SQLite job store that opens, checks and locks its file

Jobs are to survive a restart in a SQLite database, jobs.db in the
state directory, through modernc.org/sqlite v1.64.0 (SQLite 3.54.1),
the latest release, which is pure Go and builds with CGO_ENABLED=0.
It requires modernc.org/libc at exactly v1.80.1, older than libc's
latest; the owner accepted that pin as the one exception to taking
dependencies at their latest stable release, and CLAUDE.md and
design.md record it here. The other modules it brings are mathutil,
memory, go-humanize, go-strftime and bigfft, at the versions it asks
for; bigfft has no tagged release.

The new package internal/pkg/jobstore opens the database in this order:

- It creates the file 0600 if it is missing, since SQLite gives its
  log the database's mode. An existing file is never opened outside
  SQLite, since closing any descriptor of it would drop the locks the
  process holds.
- The connection runs its pragmas, in this order: exclusive locking
  before the first read, so the log's index stays in memory and no
  -shm file is made; auto_vacuum NONE; WAL; synchronous NORMAL;
  temporary tables in memory; a 4 MiB journal_size_limit;
  secure_delete FAST. A connector runs them, since the driver's own
  _pragma list sorts them by name and would set the locking mode only
  after the first read, leaving a mapped -shm file on every open but
  the first.
- PRAGMA quick_check reads every page, so a damaged file fails at
  start rather than when a statement meets the page mid-run.
- A user_version newer than the binary's is refused, naming both.
- Each schema step runs in one synced transaction that also sets
  user_version. Version 1 creates jobs, deliveries, content and refs.
- It writes once, so a store that cannot be written fails at start and
  the file stays locked; a second Open of the directory, from any
  process, fails at once with SQLITE_BUSY.

Every error from Open names the file. The URI escapes the path, so a
directory with a space, "#", "?" or "%" in its name works. The store
syncs with fdatasync where the platform has it, as on Linux.

The lock test runs the second Open in a child process, the test binary
run again, so the lock tested is the file system's. A test reopens the
store three times and finds no -shm file and exclusive locking each
time. Nothing uses the store yet.

Part of stage 1d of docs/plans/job-store.md (decisions 1, 3 and 13).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
57_WolveandClaude Opus 5.5 committed 2026-12-01 16:34:51 -06:00
1 parent 60baa843ef
commit 6e47cb713e
8 files changed
+648

No files matched your search

+7
View File
@@ -16,12 +16,14 @@ require (
golang.org/x/image v0.48.0
golang.org/x/sync v0.25.0
golang.org/x/text v0.44.0
modernc.org/sqlite v1.64.0
)
require (
github.com/BurntSushi/toml v1.6.0 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.6.1 // indirect
github.com/dustin/go-humanize v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/gofiber/schema v1.10.2 // indirect
github.com/gofiber/utils/v2 v2.6.5 // indirect
@@ -32,9 +34,11 @@ require (
github.com/mattn/go-colorable v0.1.16 // indirect
github.com/mattn/go-isatty v0.0.24 // indirect
github.com/molecule-man/go-brrr v1.3.1 // indirect
github.com/ncruces/go-strftime v1.1.0 // indirect
github.com/pb33f/go-yaml v0.2.0 // indirect
github.com/pb33f/ordered-map/v2 v2.3.2 // indirect
github.com/philhofer/fwd v1.2.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/sagikazarmark/locafero v0.11.0 // indirect
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
github.com/spf13/afero v1.15.0 // indirect
@@ -54,6 +58,9 @@ require (
golang.org/x/tools v0.53.0 // indirect
golang.org/x/vuln v1.10.0 // indirect
honnef.co/go/tools v0.8.2 // indirect
modernc.org/libc v1.80.1 // indirect
modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.12.1 // indirect
)
tool (