Skip to content

asyncio/aiohttp servers, uploads to the website, history settings and a startup cache - #16

Merged
shuckc merged 8 commits into
masterfrom
sales-upload
Nov 24, 2026
Merged

shuckc merged 8 commits into
masterfrom
sales-upload

Conversation

@shuckc

@shuckc shuckc commented Nov 24, 2026

Copy link
Copy Markdown
Member

Eight commits, in the order they build on each other.

Servers on asyncio and aiohttp

  • The LPD receiver and the web server run on one asyncio loop (aiohttp for HTTP, asyncio streams for LPD), replacing asyncore. Handlers still run one at a time on the loop, so the job list needs no locking.
  • Mail is sent from the event loop with smtplib in a worker thread.

Uploads to the website

  • webupload.py turns a parsed invoice, credit note or statement into the website's JSON and cuts the account PDF into one PDF per document (a document can span pages). uploader.py queues them, posts each as multipart with the API token in a worker thread, retries network failures and 5xx, and records the outcome in upload.pickle.
  • Job page: an Upload to website button (when [Web] base_url and api_key are set) and an "Uploaded from this job" table.
  • Settings > Web Uploads shows the website and the last three characters of the key; a new Uploads page lists recent uploads; [Web] auto_types (invoice, crednote, statement) uploads documents as they arrive.
  • Needs the website's ingest endpoints (abfw_www, already merged).

History settings

  • Sent emails, printed documents and uploads each keep their own result_count ([Gmail], [Printing], [Web]).

Faster startup

  • The parser's colouring is no longer stored on every job; the debug page parses its one job again. PDFs are made from parsed alone.
  • ~/printerface/derived.pickle caches each loaded job's parsed pages and PDF names (keyed by job file name, with its size and mtime and a parser stamp). 1,089 jobs start in 0.84 s instead of 8 s here; on merlot 306 jobs came up in about 5 s warm. The cache is only an optimisation: anything not matching is worked out again. A stationery change does not redraw by itself; Settings > Stationery counts the older documents and has a button.
  • Job pickles that appear in the job directory without arriving over LPD are taken in like new arrivals, including auto-print and auto-upload. Copy in only what should be acted on.

Testing

  • python -m unittest discover --pattern='*.py' with PRINTERFACE_HOME pointing at a scratch directory: 156 tests, 1 skipped (the corpus test, which needs WEBUPLOAD_CORPUS). Run on this branch as pushed.
  • Replayed 1,089 stored jobs through the new LPD server with no mismatches, and uploaded 13 real documents to a local copy of the website, twice (created, then replaced).
  • Running on printertest at the previous tip of this branch.

New dependencies: aiohttp, pypdf.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DKcyxWnp9B7ZMfow73wHmP

shuckc and others added 8 commits November 24, 2026 09:32
asyncore and asynchat are gone from python 3.12, and the hand-written HTTP server was the
largest part of what depended on them. lpdserver.py is now an asyncio stream protocol and
httpserver.py serves the same path handlers (and the static files) with aiohttp. Both run
on one event loop, as before, so handlers still see a single thread and the job list needs
no locking; main.py starts them with asyncio.run, writes the print history every ten
seconds and on SIGTERM as well as Ctrl+C.

LPD behaviour is unchanged, including senders that close the connection to mark the end
of the data (even by reset), the trailing null on a sized data file and ignoring unknown
commands. 1,089 production jobs replayed through it with tools/replay_corpus.py arrive
byte for byte. Directory listings from the static file server are gone.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DKcyxWnp9B7ZMfow73wHmP
The mail queue is an asyncio.Queue worked by a task on the loop, and the blocking smtplib
call runs through asyncio.to_thread. The results list and its pickle are now only touched
on the loop, so there is no thread sharing them. Queueing no longer blocks the web server
if the queue is full: it raises, and the send form already reports "unable to queue
email". Shutdown waits up to 30 seconds for queued mail to go.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DKcyxWnp9B7ZMfow73wHmP
webupload.py turns a parsed job into the website's JSON, one document per
invoice, credit note or statement, and cuts the account PDF into a PDF per
document (a document can span pages). uploader.py queues them on the event
loop, posts each as multipart with the API token in a worker thread, retries
network failures and 5xx, and records the outcome in upload.pickle. The job
page gets an "Upload to website" button when [Web] base_url and api_key are
set, an "Uploaded from this job" table, and the home page counts failures.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DKcyxWnp9B7ZMfow73wHmP
…ings

The sent emails, printed documents and uploads each remember their own number
of records, set under [Gmail], [Printing] and [Web] result_count, so they can
be sized independently. Printing defaults to 200 and uploads to 120.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DKcyxWnp9B7ZMfow73wHmP
Settings -> Web Uploads shows the configured website (as a link) and the last
three characters of the API key, the automatic upload kinds, and the queue
and recent failures. A new Uploads page lists recent uploads. [Web]
auto_types (invoice, crednote, statement) queues documents as they arrive,
through the same queue as the Upload button. The "Uploaded from this job"
table links with docnum / account.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DKcyxWnp9B7ZMfow73wHmP
The fields the parser found are only used to colour in the debug page, and are
about twice the size of the parsed pages, so they are no longer stored on the
job: debug() parses again for the one job it shows. The PDFs are made from the
parsed pages alone, so whether to format now depends on `parsed`. Every
extractor adds its fields and its parsed page together, so this gives the same
answer as before (checked on all 1,089 stored jobs).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DKcyxWnp9B7ZMfow73wHmP
~/printerface/derived.pickle holds, for each job file in the loaded history, its
parsed pages and the names of its PDFs, with the size and modification time of
the job file and a stamp of the parser code. At startup a job whose entry is
still good takes them from the cache, and its PDFs are only drawn again if
they have gone from disk: a thousand jobs start in under a second, against
about eight. A job file that has changed, or a different parser, means that
job is parsed again. The file is rewritten, atomically, when something
changed, at most every three minutes and at shutdown, and holds only the jobs
still loaded.

A change to the stationery does not redraw anything by itself. Settings >
Stationery counts the documents drawn under older stationery (stationary.py,
the [Printing] company details, the logos) and redraws them on request.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DKcyxWnp9B7ZMfow73wHmP
Every poll of the control loop looks at the two newest month directories for
job files that are not in the history: copied in from another instance, say.
Once a file has been still for two seconds it is loaded (through the derived
cache), put into the history in file name order, and run through auto-print
and auto-upload exactly as a job that arrived over LPD is. A file older than
everything in a full history is ignored, one that will not load is tried again
only if its size changes, and a job that arrived over LPD is never found again.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DKcyxWnp9B7ZMfow73wHmP
@shuckc
shuckc merged commit 8283a52 into master Nov 24, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant