Commit Graph
3 Commits
Author SHA1 Message Date
57_WolveandClaude Opus 5.5 5ae323b570 Keep unique jobs' references in the SQLite job store
The queue answers a repeated ntfy message or digest with its earlier
job, by the source and reference of unique jobs. For that check to
outlive a restart, the store keeps those references too.

Add, for a unique job, stores its source and reference with its job
ID in refs, in the insert's commit, and drops those of the source
beyond the newest 512, in the order they were kept, as the queue
forgets them. Job IDs do not give that order: one minted after the
clock was set back, as on a host whose clock does not run while it is
off, sorts before those kept, and pruning by ID would drop the new
reference and keep the oldest. A reference the store still holds for
an earlier job is dropped and kept again for the new one, as the
newest of its source; a plain insert would fail and stop the daemon.
Pruning statuses leaves refs alone. Load returns the references by
source, the first kept first, and reads a job as unique while its
reference is kept.

A test runs the same calls against this store and the memory store
and finds the same records, references, content and refusals. Another
keeps 512 references and then one whose job ID sorts before them all,
and finds that one kept and the first gone.

Part of stage 1d of docs/plans/job-store.md (decision 10).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 16:37:45 -06:00
57_WolveandClaude Opus 5.5 f6323aa854 Keep print jobs in the SQLite job store
The job store now implements printqueue.Store on its one connection,
so the queue can keep jobs on disk. A mutex serialises every call,
since a transaction and the synchronous setting belong to the
connection.

- Add inserts the job, its deliveries and its content in one
  transaction.
- Sending updates the delivery's row alone, at synchronous FULL, so
  the mark is on disk before the first byte goes to the printer.
- Finish updates the delivery's row and drops exactly the jobs,
  deliveries and contents the queue names, in one transaction, synced
  when the queue asks, as it does for a failure.
- Load turns every delivery in sending into uncertain and reads the
  jobs and deliveries, in one commit. It reads each content's length,
  not the content, and refuses a delivery whose job is gone or whose
  state this binary does not know; its errors name the file.
- Job reads one job's content.
- Idle runs a passive checkpoint while the queue has nothing to do,
  once 64 commits, about 20 jobs, have gathered since the last. The
  queue is idle after nearly every job when receipts come one at a
  time, and a checkpoint syncs the log and the database, and the next
  commit the log's header: checkpointing each time would turn a job's
  one sync into four.

A synced commit sets synchronous FULL, commits, and returns to NORMAL
even when the commit failed. Times are stored as Unix nanoseconds, so
they read back to the nanosecond in UTC. Errors name the operation and
the job's or delivery's sequence number, never a value.

A test hook runs inside each commit and reads PRAGMA synchronous and
total_changes(), so the tests see which commits are synced, the schema
step included, and that Sending, and a Finish that drops nothing,
change one row. With automatic checkpoints off, a job of 2 KB appended
8.6 WAL frames on the workstation; the test holds it to at most 16.
Unique jobs' references are kept in the next commit. Nothing uses the
store yet.

Part of stage 1d of docs/plans/job-store.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 16:34:51 -06:00
57_WolveandClaude Opus 5.5 6e47cb713e 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>
2026-12-01 16:34:51 -06:00