Files
mtusi2026lab5/src/job_admin_main.cpp
T
2026-11-16 09:28:38 +03:00

111 lines
6.5 KiB
C++

#include "db.hpp"
#include "outbox.hpp"
#include "s3_client.hpp"
#include <sodium.h>
#include <iostream>
#include <regex>
#include <nlohmann/json.hpp>
namespace {
void verify_source(S3Client& s3, const std::string& key, std::size_t expected,
const std::string& checksum) {
crypto_hash_sha256_state state;
crypto_hash_sha256_init(&state);
std::size_t received = 0;
s3.get_object(key, [&](const char* data, std::size_t n) {
if (received > expected || n > expected - received) throw std::runtime_error("source mismatch");
crypto_hash_sha256_update(&state, reinterpret_cast<const unsigned char*>(data), n);
received += n;
});
unsigned char digest[crypto_hash_sha256_BYTES];
crypto_hash_sha256_final(&state, digest);
char hex[crypto_hash_sha256_BYTES * 2 + 1];
sodium_bin2hex(hex, sizeof(hex), digest, sizeof(digest));
if (received != expected || checksum != hex) throw std::runtime_error("source checksum mismatch");
}
}
int main(int argc, char** argv) {
const bool replay = argc == 7 && std::string(argv[1]) == "replay" &&
std::string(argv[3]) == "--actor" && std::string(argv[5]) == "--reason";
const bool upgrade = argc == 9 && std::string(argv[1]) == "upgrade" &&
std::string(argv[3]) == "--version" && std::string(argv[5]) == "--actor" &&
std::string(argv[7]) == "--reason";
if (!replay && !upgrade) {
std::cerr << "usage: job_admin replay JOB_UUID --actor NAME --reason REASON\n"
" job_admin upgrade ATTACHMENT_UUID --version 2 --actor NAME --reason REASON\n";
return 2;
}
const std::string id = argv[2], actor = argv[replay ? 4 : 6], reason = argv[replay ? 6 : 8];
const std::regex uuid("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$");
if (!std::regex_match(id, uuid) || actor.empty() || actor.size() > 100 || reason.empty() ||
reason.size() > 500 || actor.find_first_not_of(" \t\r\n") == std::string::npos ||
reason.find_first_not_of(" \t\r\n") == std::string::npos) return 2;
if (upgrade && std::string(argv[4]) != "1" && std::string(argv[4]) != "2") return 2;
try {
if (sodium_init() < 0) return 1;
auto conn = open_pg_connection(read_secret_file("DATABASE_URL_FILE"), true);
S3Client s3(S3Client::from_env());
pqxx::work tx(*conn);
if (upgrade) {
const int version = std::stoi(argv[4]);
tx.exec("SELECT pg_advisory_xact_lock(hashtextextended('pipeline:' || $1::uuid::text,0))", pqxx::params{id});
auto rows = tx.exec("SELECT pipeline_version,s3_key,size_bytes,sha256 FROM attachments "
"WHERE id=$1::uuid FOR UPDATE", pqxx::params{id});
if (rows.empty() || version < rows[0]["pipeline_version"].as<int>()) return 1;
if (version == rows[0]["pipeline_version"].as<int>()) {
const auto jobs = tx.exec("SELECT id FROM jobs WHERE attachment_id=$1::uuid AND pipeline_version=$2",
pqxx::params{id, version});
if (jobs.empty()) return 1;
std::cout << nlohmann::json{{"job_id", jobs[0][0].as<std::string>()},
{"pipeline_version", version}, {"created", false}}.dump() << '\n';
return 0;
}
const auto& row = rows[0];
verify_source(s3, row["s3_key"].as<std::string>(), row["size_bytes"].as<std::size_t>(), row["sha256"].as<std::string>());
const auto job = tx.exec("INSERT INTO jobs(id,attachment_id,pipeline_version,source_key) "
"VALUES(gen_random_uuid(),$1::uuid,$2,$3) RETURNING id",
pqxx::params{id, version, row["s3_key"].as<std::string>()})[0][0].as<std::string>();
tx.exec("UPDATE attachments SET pipeline_version=$2, "
"status=CASE WHEN published_version IS NULL THEN 'queued' ELSE status END, "
"error_code=CASE WHEN published_version IS NULL THEN NULL ELSE error_code END, "
"updated_at=CASE WHEN published_version IS NULL THEN now() ELSE updated_at END WHERE id=$1::uuid",
pqxx::params{id, version});
tx.exec("INSERT INTO pipeline_requests(attachment_id,pipeline_version,job_id,actor,reason) "
"VALUES($1::uuid,$2,$3::uuid,$4,$5)", pqxx::params{id, version, job, actor, reason});
enqueue_attachment_job(tx, job);
tx.commit();
std::cout << nlohmann::json{{"job_id", job}, {"pipeline_version", version}, {"created", true}}.dump() << '\n';
return 0;
}
if (!tx.exec("SELECT pg_try_advisory_xact_lock(hashtextextended($1::uuid::text,0))",
pqxx::params{id})[0][0].as<bool>()) return 1;
const auto rows = tx.exec(
"SELECT j.attachment_id,j.status,j.retries,j.source_key,j.pipeline_version, "
"a.pipeline_version AS desired_version,a.staging_cleaned_at,a.size_bytes,a.sha256 "
"FROM jobs j JOIN attachments a ON a.id=j.attachment_id "
"WHERE j.id=$1::uuid FOR UPDATE OF j,a", pqxx::params{id});
if (rows.empty() || rows[0]["status"].as<std::string>() != "failed" ||
rows[0]["pipeline_version"].as<int>() != rows[0]["desired_version"].as<int>()) return 1;
const auto& row = rows[0];
const std::string source = row["source_key"].as<std::string>();
if (source.rfind("staging/",0) == 0 && !row["staging_cleaned_at"].is_null()) return 1;
verify_source(s3, source, row["size_bytes"].as<std::size_t>(), row["sha256"].as<std::string>());
tx.exec("INSERT INTO job_replays(job_id,actor,reason,previous_error_code,retries_before) "
"SELECT id,$2,$3,error_code,retries FROM jobs WHERE id=$1::uuid", pqxx::params{id, actor, reason});
tx.exec("UPDATE jobs SET status='queued',retry_base=retries,error_code=NULL, "
"next_attempt_at=now(),updated_at=now() WHERE id=$1::uuid", pqxx::params{id});
tx.exec("UPDATE attachments SET status='queued',s3_key=$2,error_code=NULL,variants='[]'::jsonb,updated_at=now() "
"WHERE id=$1::uuid AND published_version IS NULL",
pqxx::params{row["attachment_id"].as<std::string>(), source});
enqueue_attachment_job(tx, id);
tx.commit();
std::cout << "replayed job=" << id << '\n';
return 0;
} catch (const std::exception&) {
std::cerr << "operator action failed; transaction was not confirmed\n";
return 1;
}
}