20#ifndef ORES_COMPUTE_MESSAGING_WORKUNIT_HANDLER_HPP
21#define ORES_COMPUTE_MESSAGING_WORKUNIT_HANDLER_HPP
26#include <rfl/json.hpp>
27#include <boost/uuid/random_generator.hpp>
28#include <boost/uuid/uuid_io.hpp>
29#include "ores.logging/make_logger.hpp"
30#include "ores.nats/domain/message.hpp"
31#include "ores.nats/service/client.hpp"
32#include "ores.database/domain/context.hpp"
33#include "ores.security/jwt/jwt_authenticator.hpp"
34#include "ores.service/messaging/handler_helpers.hpp"
35#include "ores.service/service/request_context.hpp"
36#include "ores.compute.api/messaging/workunit_protocol.hpp"
37#include "ores.compute.api/messaging/work_protocol.hpp"
38#include "ores.compute.core/service/workunit_service.hpp"
39#include "ores.dq.api/domain/change_reason.hpp"
40#include "ores.compute.core/service/result_service.hpp"
41#include "ores.compute.core/service/app_version_service.hpp"
43namespace ores::compute::messaging {
46inline auto& workunit_handler_lg() {
47 static auto instance = ores::logging::make_logger(
48 "ores.compute.messaging.workunit_handler");
53using ores::service::messaging::reply;
54using ores::service::messaging::error_reply;
55using ores::service::messaging::decode;
56using ores::service::messaging::stamp;
57using ores::service::messaging::error_reply;
58using ores::service::messaging::has_permission;
61class workunit_handler {
65 std::optional<ores::security::jwt::jwt_authenticator> verifier)
66 : nats_(nats), ctx_(
std::move(ctx)), verifier_(
std::move(verifier)) {}
69 BOOST_LOG_SEV(workunit_handler_lg(), debug)
71 auto ctx_expected = ores::service::service::make_request_context(
72 ctx_, msg, verifier_);
74 error_reply(nats_, msg, ctx_expected.error());
77 const auto& ctx = *ctx_expected;
78 service::workunit_service svc(ctx);
79 list_workunits_response resp;
81 if (
auto req = decode<list_workunits_request>(msg)) {
82 resp.workunits = svc.list();
83 resp.total_available_count =
84 static_cast<int>(resp.workunits.size());
87 reply(nats_, msg, resp);
88 BOOST_LOG_SEV(workunit_handler_lg(), debug)
93 BOOST_LOG_SEV(workunit_handler_lg(), debug)
95 auto ctx_expected = ores::service::service::make_request_context(
96 ctx_, msg, verifier_);
98 error_reply(nats_, msg, ctx_expected.error());
101 const auto& ctx = *ctx_expected;
102 if (!has_permission(ctx,
"compute::batches:write")) {
106 if (
auto req = decode<save_workunit_request>(msg)) {
108 service::workunit_service wu_svc(ctx);
109 stamp(req->workunit, ctx);
110 wu_svc.save(req->workunit);
114 service::result_service result_svc(ctx);
115 const auto wu_id_str =
116 boost::uuids::to_string(req->workunit.id);
117 const auto av_id_str =
118 boost::uuids::to_string(req->workunit.app_version_id);
119 const auto tenant_id_str = ctx.tenant_id().to_string();
120 const auto redundancy = req->workunit.target_redundancy;
123 service::app_version_service av_svc(ctx);
124 const auto av = av_svc.find(av_id_str);
125 const auto package_uri = av ? av->package_uri : std::string{};
127 BOOST_LOG_SEV(workunit_handler_lg(), warn)
128 <<
"app_version not found: " << av_id_str
129 <<
" — package_uri will be empty in assignment event";
132 for (
int i = 0; i < redundancy; ++i) {
134 r.id = boost::uuids::random_generator()();
135 r.workunit_id = req->workunit.id;
137 r.change_reason_code = ores::dq::domain::change_reasons::system_new_record;
138 r.change_commentary =
"Created on workunit dispatch";
142 const auto result_id_str = boost::uuids::to_string(r.id);
143 const auto event = work_assignment_event{
144 .result_id = result_id_str,
145 .workunit_id = wu_id_str,
146 .app_version_id = av_id_str,
147 .package_uri = package_uri,
148 .input_uri = req->workunit.input_uri,
149 .config_uri = req->workunit.config_uri,
150 .output_uri =
"api/v1/compute/results/" + result_id_str +
"/output"};
151 const auto json = rfl::json::write(event);
153 reinterpret_cast<const std::byte*
>(json.data());
155 "compute.v1.work.assignments." + tenant_id_str,
156 std::span<const std::byte>(p, json.size()));
157 BOOST_LOG_SEV(workunit_handler_lg(), debug)
158 <<
"Dispatched result " << result_id_str
159 <<
" for workunit " << wu_id_str;
162 reply(nats_, msg, save_workunit_response{.success =
true});
163 }
catch (
const std::exception& e) {
164 reply(nats_, msg, save_workunit_response{
165 .success =
false, .message = e.what()});
168 BOOST_LOG_SEV(workunit_handler_lg(), warn)
169 <<
"Failed to decode: " << msg.
subject;
171 BOOST_LOG_SEV(workunit_handler_lg(), debug)
172 <<
"Completed " << msg.
subject;
178 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
@ forbidden
The caller is authenticated but lacks the required permission.
Context for the operations on a postgres database.
Definition context.hpp:47
A received NATS message.
Definition message.hpp:40
std::string subject
The subject the message was published to.
Definition message.hpp:44
NATS client: connection, pub/sub, request/reply, and JetStream.
Definition client.hpp:73
void js_publish(std::string_view subject, std::span< const std::byte > data, std::unordered_map< std::string, std::string > headers={})
Publish a message to a JetStream stream.
Definition client.cpp:410