Files
moloapp/src/supervisor/tasks/task_envelope.cpp
T
retoor 3c4334cec9 feat: implement MoloApp plans 01-15 with integration
Full C++23 framework tree: runtime, observability, security,
data, app, realtime, supervisor, clients, CLI, example plus
enterprise tenancy, audit, operability, governance,
supplychain. Postgres via docker compose for tests.
2026-10-23 18:20:04 +02:00

151 lines
5.0 KiB
C++

#include "supervisor/tasks/task_envelope.hpp"
#include <chrono>
#include <iomanip>
#include <random>
#include <sstream>
#include "supervisor/common/clock.hpp"
#include "supervisor/common/json.hpp"
namespace molo::tasks {
std::string to_string(TaskState state) {
switch (state) {
case TaskState::Queued: return "queued";
case TaskState::Leased: return "leased";
case TaskState::Running: return "running";
case TaskState::Succeeded: return "succeeded";
case TaskState::FailedRetryable: return "failed-retryable";
case TaskState::FailedPermanent: return "failed-permanent";
case TaskState::DeadLettered: return "dead-lettered";
case TaskState::Cancelled: return "cancelled";
}
return "queued";
}
bool parse_state(const std::string& text, TaskState& out) {
if (text == "queued") {
out = TaskState::Queued;
} else if (text == "leased") {
out = TaskState::Leased;
} else if (text == "running") {
out = TaskState::Running;
} else if (text == "succeeded") {
out = TaskState::Succeeded;
} else if (text == "failed-retryable") {
out = TaskState::FailedRetryable;
} else if (text == "failed-permanent") {
out = TaskState::FailedPermanent;
} else if (text == "dead-lettered") {
out = TaskState::DeadLettered;
} else if (text == "cancelled") {
out = TaskState::Cancelled;
} else {
return false;
}
return true;
}
std::string TaskEnvelope::encode() const {
using common::JValue;
JValue::Object obj;
obj["id"] = JValue(id);
obj["name"] = JValue(name);
JValue payload_value;
if (!common::json_decode(payload, payload_value)) {
payload_value = JValue(payload); // non-JSON payloads ride as strings
}
obj["payload"] = payload_value;
obj["attempts"] = JValue(attempts);
obj["max_retries"] = JValue(max_retries);
if (run_at_ms.has_value()) {
obj["run_at"] = JValue(common::iso8601(*run_at_ms));
} else {
obj["run_at"] = JValue(nullptr);
}
obj["timeout_soft_ms"] = JValue(static_cast<long long>(timeout_soft_ms));
obj["timeout_hard_ms"] = JValue(static_cast<long long>(timeout_hard_ms));
obj["idempotency_key"] = JValue(idempotency_key);
obj["enqueued_at"] = JValue(common::iso8601(enqueued_at_ms));
obj["state"] = JValue(to_string(state));
return common::json_encode(JValue(obj));
}
bool TaskEnvelope::decode(const std::string& text, TaskEnvelope& out) {
using common::JValue;
JValue root;
if (!common::json_decode(text, root) || !root.is_object()) {
return false;
}
const JValue* id = root.find("id");
const JValue* name = root.find("name");
if (id == nullptr || name == nullptr || !id->is_string() || !name->is_string()) {
return false;
}
TaskEnvelope env;
env.id = id->as_string();
env.name = name->as_string();
const JValue* payload = root.find("payload");
if (payload != nullptr) {
env.payload = common::json_encode(*payload);
}
const JValue* attempts = root.find("attempts");
const JValue* max_retries = root.find("max_retries");
if (attempts != nullptr) {
env.attempts = static_cast<int>(attempts->as_int(0));
}
if (max_retries != nullptr) {
env.max_retries = static_cast<int>(max_retries->as_int(5));
}
const JValue* run_at = root.find("run_at");
if (run_at != nullptr && run_at->is_string()) {
const std::int64_t parsed = common::parse_iso8601(run_at->as_string());
if (parsed < 0) {
return false;
}
env.run_at_ms = parsed;
}
const JValue* soft = root.find("timeout_soft_ms");
const JValue* hard = root.find("timeout_hard_ms");
if (soft != nullptr) {
env.timeout_soft_ms = static_cast<long>(soft->as_int(30000));
}
if (hard != nullptr) {
env.timeout_hard_ms = static_cast<long>(hard->as_int(60000));
}
const JValue* key = root.find("idempotency_key");
if (key != nullptr) {
env.idempotency_key = key->as_string();
}
const JValue* enqueued = root.find("enqueued_at");
if (enqueued != nullptr && enqueued->is_string()) {
env.enqueued_at_ms = common::parse_iso8601(enqueued->as_string());
}
const JValue* state = root.find("state");
if (state != nullptr && state->is_string()) {
if (!parse_state(state->as_string(), env.state)) {
return false;
}
}
out = std::move(env);
return true;
}
std::string TaskEnvelope::generate_id() {
// UUIDv7 shape: 48-bit unix ms plus 74 random bits, hex with dashes.
static thread_local std::mt19937_64 rng{std::random_device{}()};
const auto now = std::chrono::system_clock::now();
const auto ms = static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count());
const std::uint64_t hi = (ms << 16) | (rng() & 0xFFFFu);
const std::uint64_t lo = rng();
std::ostringstream out;
out << std::hex << std::setfill('0') << std::setw(12) << (hi >> 16) << "-" << std::setw(4)
<< (hi & 0xFFFFu) << "-7" << std::setw(3) << ((lo >> 48) & 0xFFFu) << "-" << std::setw(4)
<< (((lo >> 32) & 0x3FFFu) | 0x8000u) << "-" << std::setw(12) << (lo & 0xFFFFFFFFFFFFULL);
return out.str();
}
} // namespace molo::tasks