NetworkedApplications: durable-job reference composition over Messaging with Scheduler-driven maintenance (P-NETWORKED-07)
This commit is contained in:
1 parent
21b1c8422d
commit
aad91c7d01
4 files changed
+553
-2
No files matched your search
@@ -0,0 +1,232 @@
|
||||
#include "networked_kv/job_service.hpp"
|
||||
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <exception>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace NetworkedKv
|
||||
{
|
||||
namespace
|
||||
{
|
||||
[[nodiscard]] Void::Result::Error conflict(std::string message)
|
||||
{
|
||||
return Void::Result::Error{Void::Result::ErrorKind::conflict,
|
||||
std::move(message)};
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Error not_found(std::string message)
|
||||
{
|
||||
return Void::Result::Error{Void::Result::ErrorKind::not_found,
|
||||
std::move(message)};
|
||||
}
|
||||
|
||||
[[nodiscard]] std::vector<std::byte> to_bytes(std::string_view text)
|
||||
{
|
||||
const auto raw = std::as_bytes(std::span{text.data(), text.size()});
|
||||
return std::vector<std::byte>{raw.begin(), raw.end()};
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string to_string(std::span<const std::byte> bytes)
|
||||
{
|
||||
return std::string{
|
||||
reinterpret_cast<const char*>(bytes.data()), bytes.size()};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::string_view job_state_name(JobState state) noexcept
|
||||
{
|
||||
switch (state) {
|
||||
case JobState::Submitted: return "submitted";
|
||||
case JobState::Running: return "running";
|
||||
case JobState::Succeeded: return "succeeded";
|
||||
case JobState::RetryScheduled: return "retry-scheduled";
|
||||
case JobState::DeadLettered: return "dead-lettered";
|
||||
case JobState::Unknown: return "unknown";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
JobService::JobService(Void::Messaging::Queue& queue,
|
||||
JobServiceConfig config)
|
||||
: m_queue(queue), m_config(std::move(config))
|
||||
{
|
||||
}
|
||||
|
||||
Void::Result::Result<void> JobService::initialize()
|
||||
{
|
||||
if (m_initialized) {
|
||||
return {};
|
||||
}
|
||||
Void::Messaging::QueueConfig queue;
|
||||
queue.name = m_config.queue_name;
|
||||
queue.retry = Void::Messaging::RetryPolicy{
|
||||
.max_attempts = m_config.max_attempts,
|
||||
.base_delay = m_config.retry_delay,
|
||||
.max_delay = m_config.retry_delay};
|
||||
if (auto created = m_queue.create_queue(std::move(queue)); !created) {
|
||||
return created;
|
||||
}
|
||||
Void::Messaging::GroupConfig group;
|
||||
group.name = m_config.group_name;
|
||||
group.visibility_timeout = m_config.visibility_timeout;
|
||||
if (auto created =
|
||||
m_queue.create_group(m_config.queue_name, std::move(group));
|
||||
!created) {
|
||||
return created;
|
||||
}
|
||||
m_initialized = true;
|
||||
return {};
|
||||
}
|
||||
|
||||
Void::Result::Result<std::string> JobService::submit(std::string payload)
|
||||
{
|
||||
if (!m_initialized) {
|
||||
return std::unexpected{
|
||||
conflict("JobService: initialize() before submitting")};
|
||||
}
|
||||
Void::Messaging::Message message;
|
||||
message.id = "job-" + std::to_string(m_next_id++);
|
||||
message.payload = to_bytes(payload);
|
||||
auto published = m_queue.publish(
|
||||
m_config.queue_name, std::move(message),
|
||||
m_config.publish_durability);
|
||||
if (!published) {
|
||||
return std::unexpected{std::move(published).error()};
|
||||
}
|
||||
track(JobView{.id = published->message_id,
|
||||
.state = JobState::Submitted});
|
||||
return published->message_id;
|
||||
}
|
||||
|
||||
Void::Result::Result<std::optional<JobView>>
|
||||
JobService::process_one(const JobHandler& handler)
|
||||
{
|
||||
auto received =
|
||||
m_queue.try_receive(m_config.queue_name, m_config.group_name);
|
||||
if (!received) {
|
||||
return std::unexpected{std::move(received).error()};
|
||||
}
|
||||
if (!received->has_value()) {
|
||||
return std::optional<JobView>{};
|
||||
}
|
||||
Void::Messaging::Delivery delivery = std::move(**received);
|
||||
const std::string id = delivery.message.id;
|
||||
|
||||
JobView view;
|
||||
view.id = id;
|
||||
view.attempts = delivery.attempt;
|
||||
view.state = JobState::Running;
|
||||
track(view);
|
||||
|
||||
Void::Result::Result<void> handled;
|
||||
try {
|
||||
handled = handler
|
||||
? handler(to_string(delivery.message.payload),
|
||||
delivery.attempt)
|
||||
: Void::Result::Result<void>{};
|
||||
} catch (const std::exception& error) {
|
||||
handled = std::unexpected{Void::Result::Error{
|
||||
Void::Result::ErrorKind::internal,
|
||||
std::string{"job handler threw: "} + error.what()}};
|
||||
} catch (...) {
|
||||
handled = std::unexpected{Void::Result::Error{
|
||||
Void::Result::ErrorKind::internal,
|
||||
"job handler threw an unknown exception"}};
|
||||
}
|
||||
|
||||
if (handled.has_value()) {
|
||||
if (auto acknowledged = m_queue.ack(delivery.receipt);
|
||||
!acknowledged) {
|
||||
return std::unexpected{std::move(acknowledged).error()};
|
||||
}
|
||||
view.state = JobState::Succeeded;
|
||||
track(view);
|
||||
return std::optional<JobView>{view};
|
||||
}
|
||||
|
||||
view.last_error = handled.error().message();
|
||||
// Exhausted attempts are moved to the DLQ by the queue; record the
|
||||
// terminal state now so status does not depend on another receive.
|
||||
view.state = delivery.attempt >= m_config.max_attempts
|
||||
? JobState::DeadLettered
|
||||
: JobState::RetryScheduled;
|
||||
if (auto rejected = m_queue.nack(delivery.receipt,
|
||||
m_config.retry_delay);
|
||||
!rejected) {
|
||||
return std::unexpected{std::move(rejected).error()};
|
||||
}
|
||||
track(view);
|
||||
return std::optional<JobView>{view};
|
||||
}
|
||||
|
||||
Void::Result::Result<JobView>
|
||||
JobService::status(std::string_view id) const
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
const auto found = m_jobs.find(id);
|
||||
if (found == m_jobs.end()) {
|
||||
return std::unexpected{not_found(
|
||||
"JobService: no tracked job for the given id")};
|
||||
}
|
||||
return found->second;
|
||||
}
|
||||
|
||||
Void::Result::Result<std::size_t> JobService::maintain()
|
||||
{
|
||||
return m_queue.reap_expired_visibility();
|
||||
}
|
||||
|
||||
Void::Result::Result<std::vector<Void::Messaging::DeadLetterEntry>>
|
||||
JobService::dead_letters(std::size_t limit) const
|
||||
{
|
||||
return m_queue.list_dead_letters(m_config.queue_name,
|
||||
m_config.group_name, limit);
|
||||
}
|
||||
|
||||
Void::Result::Result<std::uint64_t> JobService::schedule_maintenance(
|
||||
Void::Scheduler::Scheduler& scheduler, std::chrono::milliseconds period)
|
||||
{
|
||||
if (!m_maintenance_registered) {
|
||||
auto registered = scheduler.register_handler(
|
||||
"job-maintenance",
|
||||
[this](const Void::Scheduler::OccurrenceContext&,
|
||||
std::span<const std::byte>,
|
||||
Void::Async::OperationOptions) -> Void::Result::Result<void> {
|
||||
auto reaped = maintain();
|
||||
if (!reaped) {
|
||||
return std::unexpected{std::move(reaped).error()};
|
||||
}
|
||||
return {};
|
||||
});
|
||||
if (!registered) {
|
||||
return std::unexpected{std::move(registered).error()};
|
||||
}
|
||||
m_maintenance_registered = true;
|
||||
}
|
||||
Void::Scheduler::JobSpec spec;
|
||||
spec.id = "job-maintenance";
|
||||
spec.handler_id = "job-maintenance";
|
||||
spec.schedule = Void::Scheduler::Schedule::fixed_rate(
|
||||
period, Void::Time::SystemMonotonicClock::instance().now());
|
||||
return scheduler.add_job(std::move(spec));
|
||||
}
|
||||
|
||||
void JobService::track(const JobView& view)
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
const auto found = m_jobs.find(view.id);
|
||||
if (found == m_jobs.end()) {
|
||||
if (m_jobs.size() >= m_config.max_tracked_jobs &&
|
||||
!m_order.empty()) {
|
||||
m_jobs.erase(m_order.front());
|
||||
m_order.pop_front();
|
||||
}
|
||||
m_order.push_back(view.id);
|
||||
}
|
||||
m_jobs.insert_or_assign(view.id, view);
|
||||
}
|
||||
|
||||
} // namespace NetworkedKv
|
||||
Reference in new issue
Block a user