Quantum Resource Manager and Compiler Infrastructure (QRM&CI) 0.1.0
DaemonMessaging.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 MQSS Maintainers
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 */
7
12
13#pragma once
14
16#include "qrmci/Error.h"
17#include "qrmci/Runners.h"
18
19#include <atomic>
20#include <chrono>
21#include <functional>
22#include <spdlog/spdlog.h>
23#include <string>
24#include <string_view>
25#include <utility>
26
27namespace mqss::qrmci {
28
45template <class Message, class Handler>
46bool receiveNext(CommunicationHandler &comms, const std::string &queueName,
47 std::chrono::milliseconds timeout,
48 const std::atomic<bool> &terminationFlag, Handler &&handle) {
49 auto received = comms.receive<Message>(queueName, timeout, terminationFlag);
50 if (!received) {
51 spdlog::warn("Could not read from queue '{}': {}", queueName,
52 received.error().detail);
53 return false;
54 }
55 if (!received->has_value()) {
56 return false;
57 }
58 std::invoke(std::forward<Handler>(handle), std::move(**received));
59 return true;
60}
61
72template <class Message, class Handler>
73bool drainQueue(CommunicationHandler &comms, const std::string &queueName,
74 const std::atomic<bool> &terminationFlag, Handler &&handle) {
75 bool handledAny = false;
76 // A single non-blocking poll per turn: the queue is drained by looping,
77 // not by waiting, so an empty queue costs one round trip rather than a
78 // timeout.
79 while (receiveNext<Message>(comms, queueName, std::chrono::milliseconds(0),
80 terminationFlag, handle)) {
81 handledAny = true;
82 }
83 return handledAny;
84}
85
95template <class Message>
96void send(CommunicationHandler &comms, const Message &message,
97 const std::string &queueName) {
98 if (auto sent = comms.send(message, queueName); !sent) {
99 spdlog::warn("{}", sent.error().detail);
100 }
101}
102
110 const mqss::QuantumTask &task, const Error &failure,
111 std::string_view stage) {
112 send(comms, cancelQuantumTask(task, failure.detail),
113 task.result_destination());
114 spdlog::warn("Task {} {} [{}]: {}", task.task_id(), stage,
115 toString(failure.kind), failure.detail);
116}
117
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());
128}
129
130} // namespace mqss::qrmci
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 ...
Definition Error.h:29
Kind kind
The kind of failure, for a caller deciding what to do next.
Definition Error.h:64
std::string detail
A human-readable description, for log lines and for the cancellation message sent back to the task's ...
Definition Error.h:67