22#include <spdlog/spdlog.h>
27namespace mqss::qrmci {
45template <
class Message,
class Handler>
47 std::chrono::milliseconds timeout,
48 const std::atomic<bool> &terminationFlag, Handler &&handle) {
49 auto received = comms.
receive<Message>(queueName, timeout, terminationFlag);
51 spdlog::warn(
"Could not read from queue '{}': {}", queueName,
52 received.error().detail);
55 if (!received->has_value()) {
58 std::invoke(std::forward<Handler>(handle), std::move(**received));
72template <
class Message,
class Handler>
74 const std::atomic<bool> &terminationFlag, Handler &&handle) {
75 bool handledAny =
false;
80 terminationFlag, handle)) {
95template <
class Message>
97 const std::string &queueName) {
98 if (
auto sent = comms.
send(message, queueName); !sent) {
99 spdlog::warn(
"{}", sent.error().detail);
110 const mqss::QuantumTask &task,
const Error &failure,
111 std::string_view stage) {
113 task.result_destination());
114 spdlog::warn(
"Task {} {} [{}]: {}", task.task_id(), stage,
123 const mqss::QuantumTask &task,
124 const mqss::QuantumResult &result) {
125 send(comms, result, task.result_destination());
126 spdlog::info(
"Job Executed successfully. Results for task {} sent to '{}'.",
127 task.task_id(), task.result_destination());
The RabbitMQ send/receive helpers that carry tasks, results and backend status between QRM&CI stages.
bool drainQueue(CommunicationHandler &comms, const std::string &queueName, const std::atomic< bool > &terminationFlag, Handler &&handle)
Receive from a queue until it is empty or the broker fails, handing every message to handle.
void sendResult(CommunicationHandler &comms, const mqss::QuantumTask &task, const mqss::QuantumResult &result)
Send a task's result to its own result destination, and log it.
void sendFailure(CommunicationHandler &comms, const mqss::QuantumTask &task, const Error &failure, std::string_view stage)
Tell a task's submitter that the task failed, and log why.
bool receiveNext(CommunicationHandler &comms, const std::string &queueName, std::chrono::milliseconds timeout, const std::atomic< bool > &terminationFlag, Handler &&handle)
Receive one message from a queue and hand it to handle.
void send(CommunicationHandler &comms, const Message &message, const std::string &queueName)
Send a message, logging rather than propagating a failure.
The error type every std::expected in QRM&CI's public interfaces carries.
std::string_view toString(Error::Kind kind) noexcept
Name an error kind for a log line.
The QRM&CI pipeline stages as free functions: choosing a backend, compiling a task for it,...
mqss::QuantumResult cancelQuantumTask(const mqss::QuantumTask &task, const std::string &cancelReason)
Cancel the quantum task and return a cancellation result.
CommunicationHandler abstracts the messaging operations that move pipeline messages between QRM&CI st...
std::expected< std::optional< Message >, Error > receive(const std::string &queueName, std::chrono::milliseconds timeout, const std::atomic< bool > &terminationFlag)
Retrieve the next message of the given type from the specified queue.
std::expected< void, Error > send(const Message &message, const std::string &queueName)
Send a message to the specified queue.
A failure reported by a QRM&CI operation: a kind the caller can act on, plus a human-readable detail ...
Kind kind
The kind of failure, for a caller deciding what to do next.
std::string detail
A human-readable description, for log lines and for the cancellation message sent back to the task's ...