Run CSV exports on background workers
C++ · C++ · advanced · modification
Large CSV exports currently run on the request thread, so `POST /api/exports` holds the request open (and a pooled connection) until the file is written. This PR moves them to the background. `src/jobs/db_workers.hpp` and `src/jobs/db_workers.cpp` (new) add `jobs::DbWorkers`, a small worker pool whose jobs receive a `db::Connection&`: a worker takes a connection from the pool when it picks up work, keeps it while there are queued jobs, and hands it back before going idle. `src/main.cpp` creates the workers next to the pool, has the export route queue the export and answer 202, and shuts the workers down after the server stops. `tests/db_workers_test.cpp` (new) drives the workers with a fake pool that counts the connections it hands out and gets back.
Reporting service in C++20, standard library only apart from the existing libpq wrapper, built with CMake (build files unchanged: `src/**/*.cpp` go into the service, every `tests/*_test.cpp` plus `tests/check_main.cpp` into the test runner) by Apple clang 17 locally and GCC 13 in CI with `-Wall -Wextra -Wconversion` treated as errors. Threading: `http::Server` (unchanged) runs route handlers on several threads, so `submit` can be called concurrently; `server.run()` returns after SIGTERM once in-flight requests have finished, and no handler runs after that, so nothing calls `submit` once `shutdown` has started. `main` is the only caller of `shutdown`. Existing and unchanged: `db::Pool` (`src/db/pool.hpp`) is a thread-safe interface; `acquire()` blocks until a connection is free and does not throw, and `release(std::unique_ptr<Connection>)` resets the session (`DISCARD ALL`, one round trip to the database) before the connection goes back to the pool. `db::PgPool` implements it; `cfg.db_pool_size` is 16 and `cfg.export_workers` is 1 to 4. The codebase rule is that no database or network call is made while holding a mutex. `reports::write_export` (`src/reports/export.hpp`) is `noexcept`: it runs the export's query on the given connection and writes `<export_dir>/<export_id>.csv`, and on failure logs and writes no file (the UI then shows the export as failed after its polling timeout). Jobs passed to `submit` do not throw. Allocation and thread-creation failure are treated as fatal across the service and are out of scope. `tests/check.hpp` is the repo's tiny test helper: `TEST(name) { ... }` registers a test, `CHECK(expr)` and `CHECK_EQ(a, b)` record a failure with file and line without stopping, and `tests/check_main.cpp` runs every registered test and exits non-zero if any check failed.
Requirements
- `POST /api/exports` (existing) answers `202 Accepted` as soon as the export spec is parsed and the export is queued, instead of `200` after the CSV is written. An invalid spec still gets `400`. The export itself runs in the background and writes its CSV to `export_dir` as before (the UI already polls for the file).
- Add `jobs::DbWorkers`: `cfg.export_workers` worker threads that run queued jobs, each job with a database connection from the shared pool. `submit` is thread-safe and returns at once. A queued job starts as soon as a worker is free: a job must never stay queued while a worker sits idle.
- A worker keeps its connection while it runs queued jobs back to back and gives it back to the pool before it goes idle, so idle workers hold no connection (the pool is shared with the rest of the service).
- `shutdown()` runs every job queued before it was called, returns every connection to the pool and joins the workers. `main` calls it after `server.run()` returns; the destructor calls it too.
Files touched
- src/jobs/db_workers.hpp
- src/jobs/db_workers.cpp
- src/main.cpp
- tests/db_workers_test.cpp