10 #include <fmt/format.h>
11 #include <fmt/ostream.h>
12 #include <log4cplus/loggingmacros.h>
27 bool AwaitUnwrapReplies(boost::future<std::vector<boost::future<bool>>>&& futures) {
28 std::vector<boost::future<bool>> values = futures.get();
33 std::vector<std::exception_ptr> exceptions;
35 size_t num_exceptions = 0;
36 for (
auto& f : values) {
41 }
catch (std::exception
const& e) {
45 fmt::format(
"daq::op::AwaitUnwrapReplies: Source replied with exception: {}",
51 fmt::format(
"daq::op::AwaitUnwrapReplies: {}/{} replies contained exceptions. {}/{} "
52 "replies report success",
58 return num_ok == values.size();
61 class RecWaitSpec :
public recif::RecWaitSpec {
63 RecWaitSpec(
float timeout) : m_timeout{timeout}, m_info{} {
65 std::string getInfo()
const override {
68 void setInfo(
const std::string& info)
override {
72 float getTimeout()
const override {
75 void setTimeout(
float timeout)
override {
78 bool hasKey()
const override {
81 std::unique_ptr<recif::RecWaitSpec> cloneKey()
const override {
82 throw std::runtime_error(
"not clonable");
84 std::unique_ptr<recif::RecWaitSpec> clone()
const override {
85 throw std::runtime_error(
"not clonable");
87 bool keyEquals(
const recif::RecWaitSpec& other)
const override {
100 : m_params(m_params), m_error(
false), m_parts(), m_abort_requested(
false) {
105 fmt::format(
"AwaitPrimAsync::Initiate: Operation initiating"));
107 return m_promise.get_future();
110 void AwaitPrimAsync::InitiateAwait() {
114 .then(m_params.
common.
executor, [
this](
auto) { return AwaitOnceAsync(); })
116 .then(m_params.
common.
executor, [
this](boost::future<bool> fut) ->
void {
117 if (m_abort_requested) {
119 m_params.common.logger,
120 "AwaitPrimAsync::InitiateAwait: Operation aborted -> setting promise");
122 m_promise.set_value({m_error, std::move(m_parts)});
123 } catch (boost::promise_already_satisfied const&) {
124 LOG4CPLUS_DEBUG(m_params.common.logger,
125 "AwaitPrimAsync::InitiateAwait: Promise already satisfied");
132 auto is_ok = fut.get();
135 m_promise.set_value({m_error, std::move(m_parts)});
140 }
catch (boost::promise_already_satisfied
const&) {
141 LOG4CPLUS_DEBUG(m_params.common.logger,
142 "AwaitPrimAsync::InitiateAwait: Promise already satisfied");
145 m_promise.set_exception(boost::current_exception());
150 boost::future<void> AwaitPrimAsync::MakeInterval() {
151 using std::chrono::duration_cast;
152 using std::chrono::milliseconds;
153 using std::chrono::steady_clock;
157 return boost::make_ready_future();
160 auto now = steady_clock::now();
161 auto next_start = *m_last_start + duration_cast<steady_clock::duration>(m_params.wait_interval);
162 if (now >= next_start) {
164 return boost::make_ready_future();
166 LOG4CPLUS_DEBUG(m_params.common.logger,
167 fmt::format(
"AwaitPrimAsync::MakeInterval: Waiting {}ms until sending RecWait",
168 duration_cast<milliseconds>(next_start - now).count()));
171 m_interval.emplace(m_params.common.executor.get_io_context(), next_start);
172 m_interval->timer.async_wait([
this](boost::system::error_code
const& ec) {
175 m_interval->promise.set_value();
177 return m_interval->promise.get_future();
180 void AwaitPrimAsync::Abort() noexcept {
181 if (m_abort_requested) {
185 LOG4CPLUS_INFO(m_params.common.logger,
186 fmt::format(
"AwaitPrimAsync::Abort: Requested to abort! "
187 "Number of files received so far: {}",
189 m_abort_requested =
true;
191 m_interval->timer.cancel();
195 boost::future<bool> AwaitPrimAsync::AwaitOnceAsync() {
196 using Reply = std::shared_ptr<recif::RecWaitStatus>;
197 using Seconds = std::chrono::duration<float>;
198 using std::chrono::duration_cast;
199 if (m_abort_requested) {
201 m_params.common.logger,
202 fmt::format(
"AwaitPrimAsync::AwaitOnceAsync: Operation requested to abort"));
203 return boost::make_ready_future<bool>(
true);
205 LOG4CPLUS_DEBUG(m_params.common.logger,
206 fmt::format(
"AwaitPrimAsync::AwaitOnceAsync: Sending requests..."));
208 m_last_start = std::chrono::steady_clock::now();
210 return SendRequestAndCollectReplies<bool>(
211 m_params.common.prim_sources.begin(),
212 m_params.common.prim_sources.end(),
215 return IsSubsequentState(State::Stopped, source.GetState());
220 auto& client = s.GetSource().GetRrClient();
221 auto seconds = duration_cast<Seconds>(this->m_params.wait_interval).count();
222 #if defined(UNIT_TEST)
227 auto spec = std::make_shared<RecWaitSpec>(seconds);
229 auto mal = client.getMal();
230 BOOST_ASSERT_MSG(mal,
"MAL RR client returned invalid MAL pointer");
231 auto spec = mal->createDataEntity<recif::RecWaitSpec>();
233 *spec = RecWaitSpec(seconds);
235 return client.RecWait(spec);
239 ->
bool { return HandleRecWaitReply(source, std::move(fut)); },
240 std::string_view(
"AwaitPrimAsync: await primary data acquisition"))
241 .then(m_params.common.executor, AwaitUnwrapReplies);
244 bool AwaitPrimAsync::HandleRecWaitReply(
245 Source<PrimSource>& source, boost::future<std::shared_ptr<recif::RecWaitStatus>>&& fut) {
251 auto reply = fut.get();
253 m_params.common.status.ClearAlert(alert_id);
255 auto status = reply->getStatus();
256 if (status == recif::Success) {
260 auto rec_status = reply->getRecStatus();
261 LOG4CPLUS_INFO(m_params.common.logger,
262 fmt::format(
"Data source '{}' replied successfully and provides {} "
265 rec_status->getDpFiles().size()));
266 if (rec_status->getDpFiles().empty()) {
267 LOG4CPLUS_WARN(m_params.common.logger,
268 fmt::format(
"Data source '{}' replied successfully for "
269 "RecWait but did not produce any files!",
270 source.GetSource()));
272 for (
auto const& file : rec_status->getDpFiles()) {
273 m_parts.emplace_back(std::string(source.GetSource().GetName()), file);
281 }
catch (recif::ExceptionErr
const& e) {
282 m_params.common.status.SetAlert(
284 fmt::format(
"Primary source '{}' request 'RecWaitStatus' replied "
285 "with ICD error: ({}) {}",
286 source.GetSource().GetName(),
290 throw boost::enable_current_exception(
291 DaqSourceError(
"RecWait", std::string(source.GetSource().GetName()), e.what()));
294 m_params.common.status.SetAlert(
296 fmt::format(
"Primary source '{}' request 'RecWaitStatus' replied "
297 "with non-ICD error: {}",
298 source.GetSource().GetName(),
301 throw boost::enable_current_exception(DaqSourceError(
302 "RecWait", std::string(source.GetSource().GetName()),
"unknown exception"));
Contains declaration for the AwaitPrimAsync operation.
Declares daq::State and related functions.
constexpr std::string_view REQUEST
Request.
void FormatException(std::ostream &os, std::exception_ptr ptr)
Report without nesting.
void Reply(AsyncOpParams ¶ms, SourceType const &source, std::string description, bool error)
AlertId MakeAlertId(std::string_view category, std::string key)
@ Stopped
All data sources have reported they have stopped acquiring data.
Alert MakeAlert(std::string_view category, std::string key, std::string description)
Construct alert.
Simple class that holds the source and associated state.
rad::IoExecutor & executor
log4cplus::Logger & logger
Await specific parameters that is not provided with AsyncOpParams.
boost::future< Result< DpParts > > Initiate()
Initiates operation that await acquisition completion.
AwaitPrimAsync(AwaitOpParams params) noexcept
Constructs operation with the privided parameters.
Contains declaration for the async op utilities.