17constexpr unsigned int kSingleCpuFallbackThreshold = 2;
18constexpr std::size_t kSingleWorkerFallback = 1;
19constexpr std::size_t kFallbackWorkerCount = 4;
20constexpr std::size_t kQueueCapacity = 128;
21constexpr std::size_t kSlotLimiterWorkerDivisor = 2;
23std::size_t defaultWorkers()
25 const auto hc = std::thread::hardware_concurrency();
26 if (hc == 0)
return kFallbackWorkerCount;
27 if (hc <= kSingleCpuFallbackThreshold)
return kSingleWorkerFallback;
28 return static_cast<std::size_t
>(hc - 1);
31std::size_t resolveWorkerCount(std::size_t workers)
33 return workers == 0 ? defaultWorkers() : workers;
42 Impl(std::shared_ptr<core::ports::diagnostics::IErrorReporter> errorReporter,
45 ,
scheduler(resolveWorkerCount(workers), kQueueCapacity, std::move(errorReporter))
46 ,
slotLimiter(std::max<std::size_t>(std::size_t{1}, resolveWorkerCount(workers) / kSlotLimiterWorkerDivisor))
56 std::shared_ptr<core::ports::diagnostics::IErrorReporter> errorReporter,
58 : impl_(std::make_unique<
Impl>(std::move(errorReporter), workers)) {
69 return impl_->manager.submit(kind);
74 impl_->manager.start(
id);
79 impl_->manager.publish(event);
84 impl_->manager.fail(
id, error);
89 impl_->manager.finish(
id);
94 return impl_->manager.cancelFlag(
id);
99 return impl_->manager.pauseFlag(
id);
104 return impl_->scheduler;
109 return impl_->slotLimiter;
114 return impl_->manager.subscribe(
id, std::move(cb));
119 impl_->manager.unsubscribe(
id, subId);
124 impl_->manager.cancel(
id);
129 impl_->manager.pause(
id);
134 impl_->manager.resume(
id);
139 return impl_->manager.snapshot(
id);
144 impl_->scheduler.stop();
Declares the private generic job state manager used behind JobSystem.
Declares the generic job system facade.
Declares a bounded worker queue and a simple slot limiter for import tasks.
Impl(std::shared_ptr< core::ports::diagnostics::IErrorReporter > errorReporter, std::size_t workers)
void cancel(const JobId &id)
Cancel a job.
std::shared_ptr< std::atomic< bool > > cancelFlag(const JobId &id) const
Retrieve a job cancellation flag.
void resume(const JobId &id)
Resume a paused job.
JobSystem(std::shared_ptr< core::ports::diagnostics::IErrorReporter > errorReporter, std::size_t workers=0)
Create a generic job system.
~JobSystem()
Destroy the job system.
void start(const JobId &id)
Mark a job as running.
void fail(const JobId &id, const std::string &error)
Mark a job as failed.
void pause(const JobId &id)
Pause a running job.
void unsubscribe(const JobId &id, SubscriptionId subId)
Unsubscribe from job events.
void shutdown()
Shut down the job system.
void publish(const JobEvent &event)
Publish a job event to subscribers and update its snapshot.
std::shared_ptr< std::atomic< bool > > pauseFlag(const JobId &id) const
Retrieve a job pause flag.
void finish(const JobId &id)
Mark a job as finished.
SlotLimiter & slotLimiter()
Access the shared slot limiter.
std::optional< JobSnapshot > snapshot(const JobId &id) const
Retrieve a job snapshot.
JobId submit(JobKind kind=JobKind::Generic)
Create a new tracked job.
Scheduler & scheduler()
Access the shared worker scheduler.
SubscriptionId subscribe(const JobId &id, JobEventCallback cb)
Subscribe to job events.
Runs background tasks on a bounded worker queue.
Limits concurrent access to a bounded number of slots.
std::uint64_t SubscriptionId
Identifier for a job event subscription.
std::string JobId
Stable identifier for one tracked job.
JobKind
Classifies the high-level type of job.
std::function< void(const JobEvent &)> JobEventCallback
Callback invoked for published job events.
Event emitted when a job changes state or progress.