19#include <unordered_map>
26inline constexpr std::string_view kImportServiceUnavailable =
27 "core::domain::Statement import service not available";
28inline constexpr std::string_view kUnknownError =
"Unknown error";
35 std::shared_ptr<core::jobs::JobSystem>
jobSystem)
44 std::map<std::string, std::vector<std::uint8_t>>
artifacts;
51 entry.statement = std::move(result.
data);
53 entry.artifacts = std::move(result.
artifacts);
59 std::unordered_map<core::jobs::JobId, ImportJobResult>
results;
63 std::shared_ptr<IImportStatement> importService,
64 std::shared_ptr<core::ports::diagnostics::IErrorReporter> errorReporter,
67 std::move(importService),
68 std::make_shared<
core::
jobs::JobSystem>(std::move(errorReporter), workers))
73 std::shared_ptr<IImportStatement> importService,
74 std::shared_ptr<core::jobs::JobSystem> jobSystem)
75 : impl_(std::make_unique<
Impl>(std::move(importService), std::move(jobSystem)))
87 if (!impl_->jobSystem) {
91 const auto id = impl_->jobSystem->submit();
93 impl_->jobSystem->scheduler().enqueue([
this,
id, spec]() {
94 impl_->jobSystem->start(
id);
97 if (!impl_->importService) {
98 impl_->jobSystem->fail(
id, std::string(kImportServiceUnavailable));
102 if (spec.sourcePath.empty() || !std::filesystem::exists(spec.sourcePath)) {
107 if (spec.runRoot.empty()) {
112 const auto cancel = impl_->jobSystem->cancelFlag(
id);
113 const auto pause = impl_->jobSystem->pauseFlag(
id);
114 auto progressCallback = [
this, id](
double progress,
const std::string& message) {
120 event.progress = progress;
121 event.message = message;
122 impl_->jobSystem->publish(event);
127 importRequest.runRoot = spec.runRoot;
128 importRequest.runIdPrefix = spec.runIdPrefix;
129 importRequest.jobId = id;
130 importRequest.progressCallback = std::move(progressCallback);
131 importRequest.cancelFlag = cancel;
132 importRequest.pauseFlag = pause;
133 importRequest.scheduler = &impl_->jobSystem->scheduler();
134 importRequest.ocrLimiter = &impl_->jobSystem->slotLimiter();
136 auto result = impl_->importService->importStatement(importRequest);
142 impl_->setResult(
id, std::move(result));
144 if (cancel && cancel->load()) {
145 impl_->jobSystem->cancel(
id);
149 impl_->jobSystem->finish(
id);
150 }
catch (
const std::exception& ex) {
151 impl_->jobSystem->fail(
id, ex.what());
153 impl_->jobSystem->fail(
id, std::string(kUnknownError));
164 return impl_->jobSystem ? impl_->jobSystem->subscribe(
id, std::move(cb)) : 0;
169 if (impl_->jobSystem) impl_->jobSystem->unsubscribe(
id, subId);
174 if (impl_->jobSystem) impl_->jobSystem->cancel(
id);
179 if (impl_->jobSystem) impl_->jobSystem->pause(
id);
184 if (impl_->jobSystem) impl_->jobSystem->resume(
id);
189 return impl_->jobSystem ? impl_->jobSystem->snapshot(
id) : std::nullopt;
194 std::lock_guard<std::mutex> lock(impl_->resultsMutex);
195 const auto it = impl_->results.find(
id);
196 return it == impl_->results.end() ? nullptr : it->second.statement;
201 std::lock_guard<std::mutex> lock(impl_->resultsMutex);
202 const auto it = impl_->results.find(
id);
203 return it == impl_->results.end() ? std::vector<draft::TransactionDraft>{} : it->second.transactions;
206std::map<std::string, std::vector<std::uint8_t>>
209 std::lock_guard<std::mutex> lock(impl_->resultsMutex);
210 const auto it = impl_->results.find(
id);
211 if (it == impl_->results.end())
return {};
212 auto out = std::move(it->second.artifacts);
213 it->second.artifacts.clear();
219 if (impl_->jobSystem) impl_->jobSystem->shutdown();
Declares the public statement import service interface and factory.
Defines private constants shared by core import orchestration.
Declares the generic job system facade.
Declares a bounded worker queue and a simple slot limiter for import tasks.
Declares asynchronous statement import job orchestration.
Declares the import execution request contract.
Declares the import execution result contract.
void setResult(const core::jobs::JobId &id, ImportResult result)
std::shared_ptr< core::jobs::JobSystem > jobSystem
std::shared_ptr< IImportStatement > importService
std::unordered_map< core::jobs::JobId, ImportJobResult > results
Impl(std::shared_ptr< IImportStatement > importService, std::shared_ptr< core::jobs::JobSystem > jobSystem)
void unsubscribe(const core::jobs::JobId &id, core::jobs::SubscriptionId subId)
std::map< std::string, std::vector< std::uint8_t > > takeStatementArtifacts(const core::jobs::JobId &id)
core::jobs::SubscriptionId subscribe(const core::jobs::JobId &id, core::jobs::JobEventCallback cb)
~StatementImportJobSystem()
core::jobs::JobId startImportStatement(const ImportStatementJobSpec &spec)
std::optional< core::jobs::JobSnapshot > snapshot(const core::jobs::JobId &id) const
std::shared_ptr< core::domain::Statement > statementResult(const core::jobs::JobId &id) const
StatementImportJobSystem(std::shared_ptr< IImportStatement > importService, std::shared_ptr< core::ports::diagnostics::IErrorReporter > errorReporter, std::size_t workers=0)
void cancel(const core::jobs::JobId &id)
void resume(const core::jobs::JobId &id)
std::vector< draft::TransactionDraft > statementTransactions(const core::jobs::JobId &id) const
void pause(const core::jobs::JobId &id)
constexpr std::string_view kErrorSourceMissing
constexpr std::string_view kErrorRunRootMissing
constexpr std::string_view kErrorExtractionFailed
std::uint64_t SubscriptionId
Identifier for a job event subscription.
std::string JobId
Stable identifier for one tracked job.
std::function< void(const JobEvent &)> JobEventCallback
Callback invoked for published job events.
Describes one import execution request.
std::map< std::string, std::vector< std::uint8_t > > artifacts
std::shared_ptr< core::domain::Statement > data
std::vector< core::application::importing::draft::TransactionDraft > transactions
std::shared_ptr< core::domain::Statement > statement
std::vector< draft::TransactionDraft > transactions
std::map< std::string, std::vector< std::uint8_t > > artifacts
Event emitted when a job changes state or progress.