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

Files touched

Review this PR

C++ practice