Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
job.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
17#pragma once
18
21#include "error_handling.h"
22#include "cancellation_token.h"
23#include "retry_policy.h"
24
25#include <tuple>
26#include <memory>
27#include <string>
28#include <vector>
29#include <optional>
30#include <string_view>
31#include <atomic>
32#include <chrono>
33#include <cstdint>
34#include <functional>
35
36namespace kcenon::thread
37{
38 class job_queue;
39
47 enum class job_priority
48 {
49 lowest = 0,
50 low = 1,
51 normal = 2,
52 high = 3,
53 highest = 4,
54 realtime = 5
55 };
56
57 // Forward declarations for future composition components
58 class retry_policy;
59
69 {
71 std::function<void(common::VoidResult)> on_complete;
72
74 std::function<void(const common::error_info&)> on_error;
75
77 std::optional<job_priority> priority;
78
80 std::optional<retry_policy> retry;
81
83 std::optional<std::chrono::milliseconds> timeout;
84
87 };
88
135 class job
136 {
137 public:
145 [[nodiscard]] auto get_job_id() const -> std::uint64_t { return job_id_; }
146
151 [[nodiscard]] auto get_enqueue_time() const -> std::chrono::steady_clock::time_point
152 {
153 return enqueue_time_;
154 }
155
170 job(const std::string& name = "job");
171
188 job(const std::vector<uint8_t>& data, const std::string& name = "data_job");
189
194 virtual ~job(void);
195
203 [[nodiscard]] auto get_name(void) const -> std::string;
204
223 [[nodiscard]] virtual auto do_work(void) -> common::VoidResult;
224
230 virtual auto set_cancellation_token(const cancellation_token& token) -> void;
231
237 [[nodiscard]] virtual auto get_cancellation_token() const -> cancellation_token;
238
252 virtual auto set_job_queue(const std::shared_ptr<job_queue>& job_queue) -> void;
253
273 [[nodiscard]] virtual auto get_job_queue(void) const -> std::shared_ptr<job_queue>;
274
290 [[nodiscard]] virtual auto to_string(void) const -> std::string;
291
292 // ========================================================================
293 // Composition Methods (Fluent Interface)
294 // ========================================================================
295
321 auto with_on_complete(std::function<void(common::VoidResult)> callback) -> job&;
322
340 auto with_on_error(std::function<void(const common::error_info&)> callback) -> job&;
341
359
383 auto with_cancellation(const cancellation_token& token) -> job&;
384
407 auto with_retry(const retry_policy& policy) -> job&;
408
429 auto with_timeout(std::chrono::milliseconds timeout) -> job&;
430
436 [[nodiscard]] auto get_priority() const -> job_priority;
437
443 [[nodiscard]] auto get_retry_policy() const -> std::optional<retry_policy>;
444
450 [[nodiscard]] auto get_timeout() const -> std::optional<std::chrono::milliseconds>;
451
457 [[nodiscard]] auto has_explicit_cancellation() const -> bool;
458
464 [[nodiscard]] auto has_components() const -> bool;
465
466 protected:
476 auto invoke_callbacks(const common::VoidResult& result) -> void;
480 std::string name_;
481
488 std::vector<uint8_t> data_;
489
497
505
506 private:
510 static std::atomic<std::uint64_t> next_job_id_;
511
518 std::uint64_t job_id_;
519
526 std::chrono::steady_clock::time_point enqueue_time_;
527
536
541 };
542} // namespace kcenon::thread
543
544// ----------------------------------------------------------------------------
545// Formatter specializations for job
546// ----------------------------------------------------------------------------
547
548#include <kcenon/thread/utils/formatter_macros.h>
549
550// Generate formatter specializations for kcenon::thread::job
Provides a mechanism for cooperative cancellation of operations.
A thread-safe job queue for managing and dispatching work items.
Definition job_queue.h:65
Represents a unit of work (task) to be executed, typically by a job queue.
Definition job.h:136
auto get_priority() const -> job_priority
Gets the priority level of this job.
Definition job.cpp:400
std::chrono::steady_clock::time_point enqueue_time_
Time when the job was created.
Definition job.h:526
auto with_retry(const retry_policy &policy) -> job &
Attaches a retry policy to this job.
Definition job.cpp:368
std::unique_ptr< job_components > components_
Composed components for this job.
Definition job.h:535
virtual auto set_job_queue(const std::shared_ptr< job_queue > &job_queue) -> void
Associates this job with a specific job_queue.
Definition job.cpp:219
auto ensure_components() -> job_components &
Ensures components_ is allocated (lazy initialization).
Definition job.cpp:279
auto has_components() const -> bool
Checks if this job has any composed components.
Definition job.cpp:468
std::vector< uint8_t > data_
An optional container of raw byte data that may be used by the job.
Definition job.h:488
auto with_on_error(std::function< void(const common::error_info &)> callback) -> job &
Attaches an error callback to this job.
Definition job.cpp:316
std::string name_
The descriptive name of the job, used primarily for identification and logging.
Definition job.h:480
virtual auto set_cancellation_token(const cancellation_token &token) -> void
Sets a cancellation token that can be used to cancel the job.
Definition job.cpp:163
virtual ~job(void)
Virtual destructor for the job class to allow proper cleanup in derived classes.
Definition job.cpp:100
virtual auto to_string(void) const -> std::string
Provides a string representation of the job for logging or debugging.
Definition job.cpp:262
std::uint64_t job_id_
Unique identifier for this job.
Definition job.h:518
static std::atomic< std::uint64_t > next_job_id_
Static counter for generating unique job IDs.
Definition job.h:510
auto with_timeout(std::chrono::milliseconds timeout) -> job &
Sets a timeout for job execution.
Definition job.cpp:385
auto invoke_callbacks(const common::VoidResult &result) -> void
Invokes the completion callbacks if they are set.
Definition job.cpp:484
virtual auto get_job_queue(void) const -> std::shared_ptr< job_queue >
Retrieves the job_queue associated with this job, if any.
Definition job.cpp:244
auto with_priority(job_priority priority) -> job &
Sets the priority level for this job.
Definition job.cpp:333
auto has_explicit_cancellation() const -> bool
Checks if this job has an explicit cancellation set via composition.
Definition job.cpp:454
auto get_timeout() const -> std::optional< std::chrono::milliseconds >
Gets the timeout duration for this job.
Definition job.cpp:436
virtual auto do_work(void) -> common::VoidResult
The core task execution method to be overridden by derived classes.
Definition job.cpp:135
std::weak_ptr< job_queue > job_queue_
A weak reference to the job_queue that currently manages this job.
Definition job.h:496
auto get_name(void) const -> std::string
Retrieves the name of this job.
Definition job.cpp:112
cancellation_token cancellation_token_
The cancellation token associated with this job.
Definition job.h:504
auto get_enqueue_time() const -> std::chrono::steady_clock::time_point
Gets the time when this job was created (enqueued).
Definition job.h:151
auto get_job_id() const -> std::uint64_t
Gets the unique ID of this job.
Definition job.h:145
auto with_on_complete(std::function< void(common::VoidResult)> callback) -> job &
Attaches a completion callback to this job.
Definition job.cpp:299
virtual auto get_cancellation_token() const -> cancellation_token
Gets the cancellation token associated with this job.
Definition job.cpp:192
auto get_retry_policy() const -> std::optional< retry_policy >
Gets the retry policy of this job.
Definition job.cpp:418
job(const std::string &name="job")
Constructs a new job with an optional human-readable name.
Definition job.cpp:53
auto with_cancellation(const cancellation_token &token) -> job &
Attaches a cancellation token to this job via composition.
Definition job.cpp:350
A template class representing either a value or an error.
Encapsulates retry behavior configuration for jobs.
@ low
Between low and high watermark.
@ high
Above high_watermark, approaching capacity.
@ callback
Call user callback for custom decision.
Implementation of a cancellation token for cooperative cancellation.
Error codes and utilities for the thread system.
String encoding conversion, Base64 encoding/decoding utilities.
Generic formatter for enum types using user-provided converter functors.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
job_priority
Priority levels for job scheduling.
Definition job.h:48
@ lowest
Lowest priority, executed when no other jobs are pending.
@ realtime
Real-time priority, should be used sparingly.
@ highest
Highest priority, critical tasks.
@ normal
Normal priority, default for most jobs.
@ priority
Priority-based scheduling.
STL namespace.
Retry policy with configurable strategies: fixed, linear, and exponential backoff.
Internal structure holding composed behaviors for a job.
Definition job.h:69
std::optional< job_priority > priority
Optional priority override for this job.
Definition job.h:77
bool has_explicit_cancellation
Flag indicating explicit cancellation token was set via composition.
Definition job.h:86
std::function< void(const common::error_info &)> on_error
Callback invoked specifically on error.
Definition job.h:74
std::optional< std::chrono::milliseconds > timeout
Optional timeout for job execution.
Definition job.h:83
std::function< void(common::VoidResult)> on_complete
Callback invoked when job completes (success or error)
Definition job.h:71
std::optional< retry_policy > retry
Retry policy for automatic retry on failure.
Definition job.h:80
#define DECLARE_FORMATTER(CLASS_NAME)
Generates std::formatter specializations for narrow and wide characters.