#include "telemetry.hpp" #include "attachment_queue.hpp" #include "errors.hpp" #include void AttachmentQueue::publish(const std::string& payload) { auto lease = pool_.acquire(); std::unique_ptr reply( static_cast(redisCommand( lease.ctx(), "XADD jobs:attachments * payload %b", payload.data(), payload.size())), &freeReplyObject); if (!reply || reply->type != REDIS_REPLY_STRING || reply->len == 0) { throw DependencyUnavailable("attachment queue publish failed"); } } namespace { using Reply = std::unique_ptr; Reply checked(redisReply* reply) { Reply owned(reply, &freeReplyObject); if (!owned || owned->type == REDIS_REPLY_ERROR) { throw DependencyUnavailable("attachment stream command failed"); } return owned; } JobDelivery delivery(const redisReply* entry) { if (entry->type != REDIS_REPLY_ARRAY || entry->elements != 2 || entry->element[0]->type != REDIS_REPLY_STRING || entry->element[1]->type != REDIS_REPLY_ARRAY) { throw DependencyUnavailable("invalid stream response"); } JobDelivery result{std::string(entry->element[0]->str, entry->element[0]->len), {}}; const auto* fields = entry->element[1]; for (std::size_t i = 0; i + 1 < fields->elements; i += 2) { if (fields->element[i]->type == REDIS_REPLY_STRING && std::string(fields->element[i]->str, fields->element[i]->len) == "payload" && fields->element[i + 1]->type == REDIS_REPLY_STRING && fields->element[i + 1]->len <= 4096) { result.payload.assign(fields->element[i + 1]->str, fields->element[i + 1]->len); } } return result; } } // namespace void AttachmentQueue::ensure_group() { auto lease = pool_.acquire(); Reply reply(static_cast(redisCommand(lease.ctx(), "XGROUP CREATE jobs:attachments attachment-workers 0 MKSTREAM")), &freeReplyObject); if (!reply || (reply->type == REDIS_REPLY_ERROR && std::string(reply->str, reply->len).rfind("BUSYGROUP", 0) != 0)) { throw DependencyUnavailable("attachment consumer group unavailable"); } } std::optional AttachmentQueue::read(const std::string& consumer, int reclaim_idle_ms) { auto lease = pool_.acquire(); auto pending = checked(static_cast(redisCommand(lease.ctx(), "XAUTOCLAIM jobs:attachments attachment-workers %s %d %s COUNT 1", consumer.c_str(), reclaim_idle_ms, claim_cursor_.c_str()))); if (pending->type != REDIS_REPLY_ARRAY || pending->elements < 2 || pending->element[0]->type != REDIS_REPLY_STRING || pending->element[1]->type != REDIS_REPLY_ARRAY) { throw DependencyUnavailable("invalid pending response"); } claim_cursor_.assign(pending->element[0]->str, pending->element[0]->len); if (pending->element[1]->elements) return delivery(pending->element[1]->element[0]); auto fresh = checked(static_cast(redisCommand(lease.ctx(), "XREADGROUP GROUP attachment-workers %s COUNT 1 BLOCK 1000 STREAMS jobs:attachments >", consumer.c_str()))); if (fresh->type == REDIS_REPLY_NIL) return std::nullopt; if (fresh->type != REDIS_REPLY_ARRAY || fresh->elements != 1 || fresh->element[0]->type != REDIS_REPLY_ARRAY || fresh->element[0]->elements != 2 || fresh->element[0]->element[1]->type != REDIS_REPLY_ARRAY || fresh->element[0]->element[1]->elements != 1) { throw DependencyUnavailable("invalid stream read response"); } return delivery(fresh->element[0]->element[1]->element[0]); } void AttachmentQueue::ack(const std::string& stream_id) { auto lease = pool_.acquire(); checked(static_cast(redisCommand(lease.ctx(), "XACK jobs:attachments attachment-workers %s", stream_id.c_str()))); } void AttachmentQueue::reject(const std::string& stream_id) { { auto lease = pool_.acquire(); checked(static_cast(redisCommand(lease.ctx(), "XADD jobs:attachments:invalid * stream_id %s error_code invalid_job", stream_id.c_str()))); } ack(stream_id); }