NetworkedApplications: durable-job reference composition over Messaging with Scheduler-driven maintenance (P-NETWORKED-07)

This commit is contained in:
faumaray committed 2026-12-04 10:28:06 +03:00
1 parent 21b1c8422d
commit aad91c7d01
4 files changed
+553 -2

No files matched your search

@@ -0,0 +1,123 @@
#pragma once
#include <Void/Functional/Result.hpp>
#include <Void/Messaging/Queue.hpp>
#include <Void/Scheduler/Scheduler.hpp>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <deque>
#include <functional>
#include <map>
#include <mutex>
#include <string>
#include <string_view>
#include <vector>
namespace NetworkedKv
{
struct JobServiceConfig {
std::string queue_name{"jobs"};
std::string group_name{"workers"};
std::chrono::milliseconds visibility_timeout{
std::chrono::seconds{30}};
/// Attempts before the queue dead-letters the job.
std::uint32_t max_attempts{3};
std::chrono::milliseconds retry_delay{
std::chrono::milliseconds{50}};
/// Bounded status records; the oldest is evicted beyond this.
std::size_t max_tracked_jobs{1024};
/// Memory is explicit for the in-memory profile; a durable queue
/// profile passes Durable.
Void::Messaging::PublishDurability publish_durability{
Void::Messaging::PublishDurability::Memory};
};
enum class JobState : std::uint8_t {
Submitted,
Running,
Succeeded,
RetryScheduled,
DeadLettered,
Unknown
};
[[nodiscard]] std::string_view job_state_name(JobState state) noexcept;
struct JobView {
std::string id;
JobState state{JobState::Unknown};
std::uint32_t attempts{0};
std::string last_error;
};
/// Runs one delivered job. `attempt` starts at 1.
using JobHandler = std::function<Void::Result::Result<void>(
std::string_view payload, std::uint32_t attempt)>;
/// Durable-job reference composition over the Messaging queue contract.
///
/// Delivery semantics are the queue's (at-least-once with visibility
/// timeout and DLQ after exhausted attempts); this service adds bounded
/// job status tracking and a Scheduler-driven maintenance job. Workers
/// must be idempotent per job id: a redelivery after a lost ack is
/// possible by contract.
class JobService
{
public:
JobService(Void::Messaging::Queue& queue, JobServiceConfig config = {});
JobService(const JobService&) = delete;
JobService& operator=(const JobService&) = delete;
/// Create the queue and consumer group. Idempotent for an
/// existing compatible configuration.
[[nodiscard]] Void::Result::Result<void> initialize();
/// Publish one job and return its stable id. The id is the
/// queue's idempotency key, so a repeated publish of the same id
/// is deduplicated by the queue.
[[nodiscard]] Void::Result::Result<std::string> submit(std::string payload);
/// Receive at most one job, run the handler and ack or nack it.
/// Returns the affected job view, or empty when no job is ready.
/// A handler failure is recorded (retry or dead letter), not
/// returned as an error; queue failures are.
[[nodiscard]] Void::Result::Result<std::optional<JobView>>
process_one(const JobHandler& handler);
[[nodiscard]] Void::Result::Result<JobView>
status(std::string_view id) const;
/// Reap expired in-flight deliveries back to pending (or the DLQ
/// when attempts are exhausted). Driven by the Scheduler job.
[[nodiscard]] Void::Result::Result<std::size_t> maintain();
[[nodiscard]] Void::Result::Result<
std::vector<Void::Messaging::DeadLetterEntry>>
dead_letters(std::size_t limit = 100) const;
/// Register a fixed-rate Scheduler job that runs `maintain()`.
/// The scheduler is pure and thread-free: the caller drives it with
/// tick()/dispatch_one(). Returns the scheduler job id.
[[nodiscard]] Void::Result::Result<std::uint64_t>
schedule_maintenance(Void::Scheduler::Scheduler& scheduler,
std::chrono::milliseconds period =
std::chrono::seconds{30});
private:
void track(const JobView& view);
Void::Messaging::Queue& m_queue;
JobServiceConfig m_config;
std::uint64_t m_next_id{1};
bool m_initialized{false};
bool m_maintenance_registered{false};
mutable std::mutex m_mutex;
std::map<std::string, JobView, std::less<>> m_jobs;
std::deque<std::string> m_order;
};
} // namespace NetworkedKv