FOSSredder 0.5.0
Loading...
Searching...
No Matches
JobSystem.cpp
Go to the documentation of this file.
1
5
7
8#include "JobManager.h"
10
11#include <algorithm>
12#include <thread>
13#include <utility>
14
15namespace {
16
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;
22
23std::size_t defaultWorkers()
24{
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);
29}
30
31std::size_t resolveWorkerCount(std::size_t workers)
32{
33 return workers == 0 ? defaultWorkers() : workers;
34}
35
36}
37
38namespace core::jobs {
39
41public:
42 Impl(std::shared_ptr<core::ports::diagnostics::IErrorReporter> errorReporter,
43 std::size_t workers)
44 : manager(errorReporter)
45 , scheduler(resolveWorkerCount(workers), kQueueCapacity, std::move(errorReporter))
46 , slotLimiter(std::max<std::size_t>(std::size_t{1}, resolveWorkerCount(workers) / kSlotLimiterWorkerDivisor))
47 {
48 }
49
53};
54
56 std::shared_ptr<core::ports::diagnostics::IErrorReporter> errorReporter,
57 std::size_t workers)
58 : impl_(std::make_unique<Impl>(std::move(errorReporter), workers)) {
59}
60
61JobSystem::~JobSystem() = default;
62
63JobSystem::JobSystem(JobSystem&&) noexcept = default;
64
65JobSystem& JobSystem::operator=(JobSystem&&) noexcept = default;
66
68{
69 return impl_->manager.submit(kind);
70}
71
72void JobSystem::start(const JobId& id)
73{
74 impl_->manager.start(id);
75}
76
77void JobSystem::publish(const JobEvent& event)
78{
79 impl_->manager.publish(event);
80}
81
82void JobSystem::fail(const JobId& id, const std::string& error)
83{
84 impl_->manager.fail(id, error);
85}
86
87void JobSystem::finish(const JobId& id)
88{
89 impl_->manager.finish(id);
90}
91
92std::shared_ptr<std::atomic<bool>> JobSystem::cancelFlag(const JobId& id) const
93{
94 return impl_->manager.cancelFlag(id);
95}
96
97std::shared_ptr<std::atomic<bool>> JobSystem::pauseFlag(const JobId& id) const
98{
99 return impl_->manager.pauseFlag(id);
100}
101
103{
104 return impl_->scheduler;
105}
106
108{
109 return impl_->slotLimiter;
110}
111
113{
114 return impl_->manager.subscribe(id, std::move(cb));
115}
116
118{
119 impl_->manager.unsubscribe(id, subId);
120}
121
123{
124 impl_->manager.cancel(id);
125}
126
127void JobSystem::pause(const JobId& id)
128{
129 impl_->manager.pause(id);
130}
131
133{
134 impl_->manager.resume(id);
135}
136
137std::optional<JobSnapshot> JobSystem::snapshot(const JobId& id) const
138{
139 return impl_->manager.snapshot(id);
140}
141
143{
144 impl_->scheduler.stop();
145}
146
147}
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)
Definition JobSystem.cpp:42
void cancel(const JobId &id)
Cancel a job.
std::shared_ptr< std::atomic< bool > > cancelFlag(const JobId &id) const
Retrieve a job cancellation flag.
Definition JobSystem.cpp:92
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.
Definition JobSystem.cpp:55
~JobSystem()
Destroy the job system.
void start(const JobId &id)
Mark a job as running.
Definition JobSystem.cpp:72
void fail(const JobId &id, const std::string &error)
Mark a job as failed.
Definition JobSystem.cpp:82
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.
Definition JobSystem.cpp:77
std::shared_ptr< std::atomic< bool > > pauseFlag(const JobId &id) const
Retrieve a job pause flag.
Definition JobSystem.cpp:97
void finish(const JobId &id)
Mark a job as finished.
Definition JobSystem.cpp:87
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.
Definition JobSystem.cpp:67
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.
Definition Scheduler.h:55
Limits concurrent access to a bounded number of slots.
Definition Scheduler.h:27
std::uint64_t SubscriptionId
Identifier for a job event subscription.
Definition JobTypes.h:88
std::string JobId
Stable identifier for one tracked job.
Definition JobTypes.h:21
JobKind
Classifies the high-level type of job.
Definition JobTypes.h:26
std::function< void(const JobEvent &)> JobEventCallback
Callback invoked for published job events.
Definition JobTypes.h:93
Event emitted when a job changes state or progress.
Definition JobTypes.h:52