#pragma once #include "db.hpp" #include "models.hpp" #include #include #include #include class UploadLock; class DirectUploadRepo { public: explicit DirectUploadRepo(PgPool& pool) : pool_(pool) {} DirectUpload insert(const DirectUpload& in); std::optional find_by_id(const std::string& id); // Mark completed and link attachment_id. Returns nullopt if not pending // (or already finished / wrong owner handled by caller). std::optional mark_completed(const std::string& id, const std::string& attachment_id); std::optional mark_aborted(const std::string& id); // pending → expired where expires_at < now; returns expired rows. std::vector expire_stale(); // Staging keys of still-pending sessions (orphan GC must keep these). std::vector pending_s3_keys(); // One PG transaction. Callback may call S3 while the row lock is held. // Commit explicitly via UploadLock::commit(); otherwise the work rolls back. void with_lock(const std::function& fn); private: PgPool& pool_; }; // Row lock for a single complete(). Not safe to use after with_lock returns. class UploadLock { public: std::optional lock(const std::string& id); std::optional find_attachment(const std::string& id); // Inserts attachment and its job in this transaction; job_id equals attachment id. // ON CONFLICT (id) DO NOTHING. inserted=false and nullopt → row already exists. std::optional insert_queued(const Attachment& in, bool& inserted); void mark_completed(const std::string& id, const std::string& attachment_id); void mark_expired(const std::string& id); void commit(); private: friend class DirectUploadRepo; explicit UploadLock(pqxx::work& tx) : tx_(tx) {} pqxx::work& tx_; bool committed_ = false; };