Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::job_builder Class Reference

Fluent builder for creating and configuring jobs with composition. More...

#include <job_builder.h>

Collaboration diagram for kcenon::thread::job_builder:
Collaboration graph

Classes

class  built_job
 Internal job implementation created by the builder. More...
 

Public Member Functions

 job_builder ()=default
 Default constructor.
 
auto name (const std::string &name) -> job_builder &
 Sets the job name.
 
auto work (std::function< common::VoidResult()> work_fn) -> job_builder &
 Sets the work function for the job.
 
auto work_with_data (const std::vector< uint8_t > &data, std::function< common::VoidResult(const std::vector< uint8_t > &)> work_fn) -> job_builder &
 Sets the work function with data parameter.
 
auto cancellation (const cancellation_token &token) -> job_builder &
 Sets a cancellation token for cooperative cancellation.
 
auto on_complete (std::function< void(common::VoidResult)> callback) -> job_builder &
 Sets a completion callback.
 
auto on_error (std::function< void(const common::error_info &)> callback) -> job_builder &
 Sets an error callback.
 
auto priority (job_priority prio) -> job_builder &
 Sets the job priority.
 
auto retry (const retry_policy &policy) -> job_builder &
 Sets the retry policy.
 
auto timeout (std::chrono::milliseconds timeout) -> job_builder &
 Sets the execution timeout.
 
template<typename JobType , typename... Args>
auto from (Args &&... args) -> job_builder &
 
auto build () -> std::unique_ptr< job >
 Builds and returns the configured job.
 
auto build_shared () -> std::shared_ptr< job >
 Builds and returns the configured job as a shared pointer.
 

Private Attributes

std::string name_
 
std::vector< uint8_t > data_
 
std::function< common::VoidResult()> work_fn_
 
std::function< common::VoidResult(const std::vector< uint8_t > &)> data_work_fn_
 
cancellation_token cancellation_token_
 
bool has_cancellation_ {false}
 
std::function< void(common::VoidResult)> on_complete_
 
std::function< void(const common::error_info &)> on_error_
 
job_priority priority_ {job_priority::normal}
 
bool has_priority_ {false}
 
retry_policy retry_policy_
 
bool has_retry_ {false}
 
std::chrono::milliseconds timeout_ {0}
 
bool has_timeout_ {false}
 
std::function< std::unique_ptr< job >()> custom_job_factory_
 
bool use_custom_job_ {false}
 

Detailed Description

Fluent builder for creating and configuring jobs with composition.

The job_builder class provides a clean, fluent interface for creating jobs with various behaviors composed together.

Design Philosophy

Instead of inheritance-based specialization:

// Old way (inheritance)
class my_cancellable_callback_job : public callback_job { ... };
A specialized job class that encapsulates user-defined callbacks.

Use composition via the builder:

// New way (composition)
auto job = job_builder()
.name("my_job")
.work([](){ return common::ok(); })
.cancellation(token)
.on_complete([](auto result){ ... })
.build();
auto cancellation(const cancellation_token &token) -> job_builder &
Sets a cancellation token for cooperative cancellation.
job_builder()=default
Default constructor.
auto build() -> std::unique_ptr< job >
Builds and returns the configured job.
Represents a unit of work (task) to be executed, typically by a job queue.
Definition job.h:136
A template class representing either a value or an error.

Thread Safety

  • The builder itself is not thread-safe during construction
  • The resulting job is safe to submit to any queue
  • Callbacks are invoked on the worker thread

Usage Examples

Basic Job

auto job = job_builder()
.name("simple_job")
.work([]{ std::cout << "Hello\n"; return common::ok(); })
.build();

Job with Retry and Callback

auto job = job_builder()
.name("network_request")
.work([]{ return fetch_data(); })
.on_error([](const auto& err) {
log_error("Failed: {}", err.message);
})
.build();
static auto exponential_backoff(std::size_t max_attempts, std::chrono::milliseconds initial_delay=std::chrono::milliseconds(100), double multiplier=2.0, std::chrono::milliseconds max_delay=std::chrono::milliseconds(30000), bool use_jitter=false) -> retry_policy
Creates an exponential backoff retry policy.
@ retry
Retry failed job (with max retries)

Custom Job Type with Builder

class MyJob : public job {
public:
MyJob(int value) : job("my_job"), value_(value) {}
auto do_work() -> common::VoidResult override { ... }
private:
int value_;
};
auto job = job_builder()
.from<MyJob>(42) // Creates MyJob with constructor arg 42
.timeout(std::chrono::seconds(30))
.build();
virtual auto do_work(void) -> common::VoidResult
The core task execution method to be overridden by derived classes.
Definition job.cpp:135
@ high
High priority, time-sensitive tasks.
@ priority
Priority-based scheduling.

Definition at line 97 of file job_builder.h.

Constructor & Destructor Documentation

◆ job_builder()

kcenon::thread::job_builder::job_builder ( )
default

Member Function Documentation

◆ build()

auto kcenon::thread::job_builder::build ( ) -> std::unique_ptr<job>
inlinenodiscard

Builds and returns the configured job.

Returns
Unique pointer to the constructed job
Note
If no work function is set and no custom job type is used, the job's do_work() will return an error.
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/job_builder.h.

Definition at line 271 of file job_builder.h.

272 {
273 std::unique_ptr<job> result;
274
276 {
277 result = custom_job_factory_();
278 }
279 else
280 {
281 result = std::make_unique<built_job>(
282 name_.empty() ? "builder_job" : name_,
283 data_,
284 std::move(work_fn_),
285 std::move(data_work_fn_)
286 );
287 }
288
289 // Apply composition
291 {
292 result->with_cancellation(cancellation_token_);
293 }
294 if (on_complete_)
295 {
296 result->with_on_complete(std::move(on_complete_));
297 }
298 if (on_error_)
299 {
300 result->with_on_error(std::move(on_error_));
301 }
302 if (has_priority_)
303 {
304 result->with_priority(priority_);
305 }
306 if (has_retry_)
307 {
308 result->with_retry(retry_policy_);
309 }
310 if (has_timeout_)
311 {
312 result->with_timeout(timeout_);
313 }
314
315 return result;
316 }
cancellation_token cancellation_token_
std::function< common::VoidResult()> work_fn_
std::vector< uint8_t > data_
std::function< void(const common::error_info &)> on_error_
std::function< std::unique_ptr< job >()> custom_job_factory_
std::chrono::milliseconds timeout_
std::function< void(common::VoidResult)> on_complete_
std::function< common::VoidResult(const std::vector< uint8_t > &)> data_work_fn_

References cancellation_token_, custom_job_factory_, data_, data_work_fn_, has_cancellation_, has_priority_, has_retry_, has_timeout_, name_, on_complete_, on_error_, priority_, retry_policy_, timeout_, use_custom_job_, and work_fn_.

Referenced by build_shared().

Here is the caller graph for this function:

◆ build_shared()

auto kcenon::thread::job_builder::build_shared ( ) -> std::shared_ptr<job>
inlinenodiscard

Builds and returns the configured job as a shared pointer.

Returns
Shared pointer to the constructed job
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/job_builder.h.

Definition at line 323 of file job_builder.h.

324 {
325 return build();
326 }

References build().

Here is the call graph for this function:

◆ cancellation()

auto kcenon::thread::job_builder::cancellation ( const cancellation_token & token) -> job_builder&
inline

Sets a cancellation token for cooperative cancellation.

Parameters
tokenCancellation token to use
Returns
Reference to this builder for chaining
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/job_builder.h.

Definition at line 153 of file job_builder.h.

154 {
155 cancellation_token_ = token;
156 has_cancellation_ = true;
157 return *this;
158 }

References cancellation_token_, and has_cancellation_.

◆ from()

template<typename JobType , typename... Args>
auto kcenon::thread::job_builder::from ( Args &&... args) -> job_builder&
inline
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/job_builder.h.

Definition at line 248 of file job_builder.h.

249 {
250 static_assert(std::is_base_of_v<job, JobType>,
251 "JobType must be derived from kcenon::thread::job");
252
253 custom_job_factory_ = [args = std::make_tuple(std::forward<Args>(args)...)]() mutable
254 {
255 return std::apply([](auto&&... a) {
256 return std::make_unique<JobType>(std::forward<decltype(a)>(a)...);
257 }, std::move(args));
258 };
259 use_custom_job_ = true;
260 return *this;
261 }

References custom_job_factory_, and use_custom_job_.

◆ name()

auto kcenon::thread::job_builder::name ( const std::string & name) -> job_builder&
inline

Sets the job name.

Parameters
nameDescriptive name for the job
Returns
Reference to this builder for chaining
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/job_builder.h.

Definition at line 110 of file job_builder.h.

111 {
112 name_ = name;
113 return *this;
114 }
auto name(const std::string &name) -> job_builder &
Sets the job name.

References name(), and name_.

Referenced by name().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ on_complete()

auto kcenon::thread::job_builder::on_complete ( std::function< void(common::VoidResult)> callback) -> job_builder&
inline

Sets a completion callback.

Parameters
callbackFunction called when job completes (success or failure)
Returns
Reference to this builder for chaining
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/job_builder.h.

Definition at line 166 of file job_builder.h.

167 {
168 on_complete_ = std::move(callback);
169 return *this;
170 }
@ callback
Call user callback for custom decision.

References kcenon::thread::callback, and on_complete_.

◆ on_error()

auto kcenon::thread::job_builder::on_error ( std::function< void(const common::error_info &)> callback) -> job_builder&
inline

Sets an error callback.

Parameters
callbackFunction called only when job fails
Returns
Reference to this builder for chaining
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/job_builder.h.

Definition at line 178 of file job_builder.h.

179 {
180 on_error_ = std::move(callback);
181 return *this;
182 }

References kcenon::thread::callback, and on_error_.

◆ priority()

auto kcenon::thread::job_builder::priority ( job_priority prio) -> job_builder&
inline

Sets the job priority.

Parameters
prioPriority level for scheduling
Returns
Reference to this builder for chaining

Definition at line 190 of file job_builder.h.

191 {
192 priority_ = prio;
193 has_priority_ = true;
194 return *this;
195 }

References has_priority_, and priority_.

◆ retry()

auto kcenon::thread::job_builder::retry ( const retry_policy & policy) -> job_builder&
inline

Sets the retry policy.

Parameters
policyRetry behavior configuration
Returns
Reference to this builder for chaining

Definition at line 203 of file job_builder.h.

204 {
205 retry_policy_ = policy;
206 has_retry_ = true;
207 return *this;
208 }

References has_retry_, and retry_policy_.

◆ timeout()

auto kcenon::thread::job_builder::timeout ( std::chrono::milliseconds timeout) -> job_builder&
inline

Sets the execution timeout.

Parameters
timeoutMaximum execution time allowed
Returns
Reference to this builder for chaining
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/job_builder.h.

Definition at line 216 of file job_builder.h.

217 {
219 has_timeout_ = true;
220 return *this;
221 }
auto timeout(std::chrono::milliseconds timeout) -> job_builder &
Sets the execution timeout.

References has_timeout_, timeout(), and timeout_.

Referenced by timeout().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ work()

auto kcenon::thread::job_builder::work ( std::function< common::VoidResult()> work_fn) -> job_builder&
inline

Sets the work function for the job.

Parameters
work_fnFunction that performs the job's work
Returns
Reference to this builder for chaining
Note
The work function should return common::ok() on success or common::error_info on failure.
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/job_builder.h.

Definition at line 125 of file job_builder.h.

126 {
127 work_fn_ = std::move(work_fn);
128 return *this;
129 }

References work_fn_.

◆ work_with_data()

auto kcenon::thread::job_builder::work_with_data ( const std::vector< uint8_t > & data,
std::function< common::VoidResult(const std::vector< uint8_t > &)> work_fn ) -> job_builder&
inline

Sets the work function with data parameter.

Parameters
dataBinary data to pass to the work function
work_fnFunction that processes the data
Returns
Reference to this builder for chaining
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/job_builder.h.

Definition at line 138 of file job_builder.h.

141 {
142 data_ = data;
143 data_work_fn_ = std::move(work_fn);
144 return *this;
145 }

References data_, and data_work_fn_.

Member Data Documentation

◆ cancellation_token_

cancellation_token kcenon::thread::job_builder::cancellation_token_
private

◆ custom_job_factory_

std::function<std::unique_ptr<job>()> kcenon::thread::job_builder::custom_job_factory_
private

◆ data_

std::vector<uint8_t> kcenon::thread::job_builder::data_
private

◆ data_work_fn_

std::function<common::VoidResult(const std::vector<uint8_t>&)> kcenon::thread::job_builder::data_work_fn_
private

◆ has_cancellation_

bool kcenon::thread::job_builder::has_cancellation_ {false}
private

◆ has_priority_

bool kcenon::thread::job_builder::has_priority_ {false}
private

◆ has_retry_

bool kcenon::thread::job_builder::has_retry_ {false}
private

◆ has_timeout_

bool kcenon::thread::job_builder::has_timeout_ {false}
private

◆ name_

std::string kcenon::thread::job_builder::name_
private

◆ on_complete_

std::function<void(common::VoidResult)> kcenon::thread::job_builder::on_complete_
private

◆ on_error_

std::function<void(const common::error_info&)> kcenon::thread::job_builder::on_error_
private

◆ priority_

job_priority kcenon::thread::job_builder::priority_ {job_priority::normal}
private
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/job_builder.h.

Definition at line 406 of file job_builder.h.

@ normal
Normal priority, default for most jobs.

Referenced by build(), and priority().

◆ retry_policy_

retry_policy kcenon::thread::job_builder::retry_policy_
private

◆ timeout_

std::chrono::milliseconds kcenon::thread::job_builder::timeout_ {0}
private

◆ use_custom_job_

bool kcenon::thread::job_builder::use_custom_job_ {false}
private

◆ work_fn_

std::function<common::VoidResult()> kcenon::thread::job_builder::work_fn_
private

The documentation for this class was generated from the following file: