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

A job wrapper that integrates circuit breaker protection. More...

#include <protected_job.h>

Inheritance diagram for kcenon::thread::protected_job:
Inheritance graph
Collaboration diagram for kcenon::thread::protected_job:
Collaboration graph

Public Member Functions

 protected_job (std::unique_ptr< job > inner, std::shared_ptr< circuit_breaker > cb)
 Constructs a protected job wrapper.
 
 ~protected_job () override
 Destructor.
 
auto do_work () -> common::VoidResult override
 Executes the wrapped job with circuit breaker protection.
 
auto get_name () const -> std::string
 Gets the name of this job.
 
- Public Member Functions inherited from kcenon::thread::job
auto get_job_id () const -> std::uint64_t
 Gets the unique ID of this job.
 
auto get_enqueue_time () const -> std::chrono::steady_clock::time_point
 Gets the time when this job was created (enqueued).
 
 job (const std::string &name="job")
 Constructs a new job with an optional human-readable name.
 
 job (const std::vector< uint8_t > &data, const std::string &name="data_job")
 Constructs a new job with associated raw byte data and a name.
 
virtual ~job (void)
 Virtual destructor for the job class to allow proper cleanup in derived classes.
 
auto get_name (void) const -> std::string
 Retrieves the name of this job.
 
virtual auto set_cancellation_token (const cancellation_token &token) -> void
 Sets a cancellation token that can be used to cancel the job.
 
virtual auto get_cancellation_token () const -> cancellation_token
 Gets the cancellation token associated with this job.
 
virtual auto set_job_queue (const std::shared_ptr< job_queue > &job_queue) -> void
 Associates this job with a specific job_queue.
 
virtual auto get_job_queue (void) const -> std::shared_ptr< job_queue >
 Retrieves the job_queue associated with this job, if any.
 
virtual auto to_string (void) const -> std::string
 Provides a string representation of the job for logging or debugging.
 
auto with_on_complete (std::function< void(common::VoidResult)> callback) -> job &
 Attaches a completion callback to this job.
 
auto with_on_error (std::function< void(const common::error_info &)> callback) -> job &
 Attaches an error callback to this job.
 
auto with_priority (job_priority priority) -> job &
 Sets the priority level for this job.
 
auto with_cancellation (const cancellation_token &token) -> job &
 Attaches a cancellation token to this job via composition.
 
auto with_retry (const retry_policy &policy) -> job &
 Attaches a retry policy to this job.
 
auto with_timeout (std::chrono::milliseconds timeout) -> job &
 Sets a timeout for job execution.
 
auto get_priority () const -> job_priority
 Gets the priority level of this job.
 
auto get_retry_policy () const -> std::optional< retry_policy >
 Gets the retry policy of this job.
 
auto get_timeout () const -> std::optional< std::chrono::milliseconds >
 Gets the timeout duration for this job.
 
auto has_explicit_cancellation () const -> bool
 Checks if this job has an explicit cancellation set via composition.
 
auto has_components () const -> bool
 Checks if this job has any composed components.
 

Private Attributes

std::unique_ptr< jobinner_
 
std::shared_ptr< circuit_breaker > cb_
 

Additional Inherited Members

- Protected Member Functions inherited from kcenon::thread::job
auto invoke_callbacks (const common::VoidResult &result) -> void
 Invokes the completion callbacks if they are set.
 
- Protected Attributes inherited from kcenon::thread::job
std::string name_
 The descriptive name of the job, used primarily for identification and logging.
 
std::vector< uint8_t > data_
 An optional container of raw byte data that may be used by the job.
 
std::weak_ptr< job_queuejob_queue_
 A weak reference to the job_queue that currently manages this job.
 
cancellation_token cancellation_token_
 The cancellation token associated with this job.
 

Detailed Description

A job wrapper that integrates circuit breaker protection.

This class wraps an existing job and adds circuit breaker protection. Before executing the inner job, it checks if the circuit breaker allows the request. After execution, it records success or failure.

Thread Safety

Thread safety depends on the wrapped job. The circuit breaker integration itself is thread-safe.

Usage Example

auto cb = std::make_shared<circuit_breaker>();
auto inner = std::make_unique<my_job>();
auto protected = std::make_unique<protected_job>(std::move(inner), cb);
pool->enqueue(std::move(protected));
See also
circuit_breaker
job

Definition at line 46 of file protected_job.h.

Constructor & Destructor Documentation

◆ protected_job()

kcenon::thread::protected_job::protected_job ( std::unique_ptr< job > inner,
std::shared_ptr< circuit_breaker > cb )

Constructs a protected job wrapper.

Parameters
innerThe job to wrap and protect.
cbThe circuit breaker to use for protection.

Definition at line 12 of file protected_job.cpp.

15 : job("protected_" + (inner ? inner->get_name() : "unknown"))
16 , inner_(std::move(inner))
17 , cb_(std::move(cb))
18 {
19 }
job(const std::string &name="job")
Constructs a new job with an optional human-readable name.
Definition job.cpp:53
std::shared_ptr< circuit_breaker > cb_
std::unique_ptr< job > inner_

◆ ~protected_job()

kcenon::thread::protected_job::~protected_job ( )
overridedefault

Destructor.

Member Function Documentation

◆ do_work()

auto kcenon::thread::protected_job::do_work ( void ) -> common::VoidResult
overridevirtual

Executes the wrapped job with circuit breaker protection.

Returns
Result from the inner job, or error if circuit is open.

Behavior:

  1. Checks circuit breaker - returns error if open
  2. Executes inner job
  3. Records success or failure to circuit breaker
  4. Returns inner job result

Reimplemented from kcenon::thread::job.

Definition at line 23 of file protected_job.cpp.

24 {
25 if (!cb_)
26 {
27 return make_error_result(
29 "Circuit breaker is null");
30 }
31
32 if (!inner_)
33 {
34 return make_error_result(
36 "Inner job is null");
37 }
38
39 // Check if circuit breaker allows the request
40 if (!cb_->allow_request())
41 {
42 return make_error_result(
44 "Circuit breaker is open");
45 }
46
47 // Create guard for automatic failure recording
48 auto guard = cb_->make_guard();
49
50 try
51 {
52 auto result = inner_->do_work();
53 if (result.is_ok())
54 {
55 guard.record_success();
56 }
57 // If result is error, let guard destructor record failure
58 return result;
59 }
60 catch (...)
61 {
62 // Guard destructor will automatically record failure
63 throw;
64 }
65 }
common::VoidResult make_error_result(error_code code, const std::string &message="")
Create a common::VoidResult error from a thread::error_code.

References kcenon::thread::result< T >::is_ok(), kcenon::thread::job_invalid, kcenon::thread::make_error_result(), and kcenon::thread::operation_canceled.

Here is the call graph for this function:

◆ get_name()

auto kcenon::thread::protected_job::get_name ( void ) const -> std::string
nodiscard

Gets the name of this job.

Returns
Name of the protected job (includes inner job name).

Definition at line 67 of file protected_job.cpp.

68 {
69 return job::get_name();
70 }
auto get_name(void) const -> std::string
Retrieves the name of this job.
Definition job.cpp:112

References kcenon::thread::job::get_name().

Here is the call graph for this function:

Member Data Documentation

◆ cb_

std::shared_ptr<circuit_breaker> kcenon::thread::protected_job::cb_
private

Definition at line 83 of file protected_job.h.

◆ inner_

std::unique_ptr<job> kcenon::thread::protected_job::inner_
private

Definition at line 82 of file protected_job.h.


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