71 default:
return "unknown";
93 std::chrono::steady_clock::time_point
end_time;
104 if (
start_time == std::chrono::steady_clock::time_point{})
106 return std::chrono::milliseconds{0};
117 if (
end_time == std::chrono::steady_clock::time_point{} ||
118 start_time == std::chrono::steady_clock::time_point{})
120 return std::chrono::milliseconds{0};
163 explicit dag_job(
const std::string& name =
"dag_job");
186 return state_.load(std::memory_order_acquire);
197 state_.store(new_state, std::memory_order_release);
210 return state_.compare_exchange_strong(expected, desired,
211 std::memory_order_acq_rel,
212 std::memory_order_acquire);
245 for (
const auto&
id : dependency_ids)
257 auto set_work(std::function<common::VoidResult()> work_func) ->
void
272 work_func_ = [
this, func = std::move(work_func)]() -> common::VoidResult {
279 return common::VoidResult(
result.error());
289 auto set_fallback(std::function<common::VoidResult()> fallback_func) ->
void
322 result_ = std::forward<T>(value);
336 return std::any_cast<const T&>(
result_);
385 end_time_ = std::chrono::steady_clock::now();
427 [[nodiscard]] auto
do_work() -> common::VoidResult override;
433 [[nodiscard]] auto
to_string() const ->
std::
string override;
A job with dependency support for DAG-based scheduling.
auto get_info() const -> dag_job_info
Creates a dag_job_info snapshot.
auto get_error_message() const -> const std::optional< std::string > &
Gets the error message.
auto try_transition_state(dag_job_state expected, dag_job_state desired) -> bool
Attempts to transition state atomically.
std::function< common::VoidResult()> work_func_
The work function to execute.
auto get_state() const -> dag_job_state
Gets the current state of the job.
auto record_start_time() -> void
Records the start time.
std::chrono::steady_clock::time_point submit_time_
Time when the job was created.
dag_job(const std::string &name="dag_job")
Constructs a new dag_job with a name.
auto set_work_with_result(std::function< common::Result< T >()> work_func) -> void
Sets the work function with result.
auto get_start_time() const -> std::chrono::steady_clock::time_point
Gets the start time.
std::chrono::steady_clock::time_point start_time_
Time when execution started.
std::atomic< dag_job_state > state_
Current state of the job.
auto add_dependencies(const std::vector< job_id > &dependency_ids) -> void
Adds multiple dependencies.
job_id dag_id_
Unique identifier for this job in the DAG.
auto get_result() const -> const T &
Gets the result value.
std::vector< job_id > dependencies_
List of job IDs this job depends on.
auto set_error_message(const std::string &message) -> void
Sets the error message for failed jobs.
auto has_fallback() const -> bool
Checks if a fallback function is set.
auto has_result() const -> bool
Checks if the job has a result.
auto get_result_any() const -> const std::any &
Gets the result as std::any.
auto get_dependencies() const -> const std::vector< job_id > &
Gets the list of dependency job IDs.
auto set_work(std::function< common::VoidResult()> work_func) -> void
Sets the work function to execute.
std::optional< std::string > error_message_
Error message if job failed.
std::any result_
Result value for passing between jobs.
auto do_work() -> common::VoidResult override
Executes the job's work function.
auto get_dag_id() const -> job_id
Gets the unique DAG job identifier.
std::function< common::VoidResult()> fallback_func_
The fallback function to execute on failure.
auto record_end_time() -> void
Records the end time.
std::chrono::steady_clock::time_point end_time_
Time when execution ended.
~dag_job() override
Virtual destructor.
auto add_dependency(job_id dependency_id) -> void
Adds a dependency on another job.
auto get_fallback() const -> const std::function< common::VoidResult()> &
Gets the fallback function.
auto set_state(dag_job_state new_state) -> void
Sets the job state.
auto set_result(T &&value) -> void
Sets the result value.
auto set_fallback(std::function< common::VoidResult()> fallback_func) -> void
Sets the fallback function to execute on failure.
auto get_submit_time() const -> std::chrono::steady_clock::time_point
Gets the submit time.
auto get_end_time() const -> std::chrono::steady_clock::time_point
Gets the end time.
static std::atomic< job_id > next_dag_id_
Static counter for generating unique DAG job IDs.
auto to_string() const -> std::string override
Returns a string representation of the job.
Represents a unit of work (task) to be executed, typically by a job queue.
A template class representing either a value or an error.
T & value() &
Gets the value.
bool is_ok() const noexcept
Checks if the result is successful.
Error codes and utilities for the thread system.
Base job class for schedulable work units in the thread system.
Core threading foundation of the thread system library.
auto dag_job_state_to_string(dag_job_state state) -> std::string
Convert dag_job_state to string representation.
dag_job_state
State of a job in the DAG scheduler.
@ failed
Execution failed.
@ cancelled
Cancelled by user or dependency failure.
@ running
Currently executing.
@ pending
Waiting for dependencies to complete.
@ completed
Successfully completed.
@ ready
Dependencies satisfied, can be executed.
@ skipped
Skipped due to dependency failure.
std::uint64_t job_id
Unique job identifier for DAG scheduler.
constexpr job_id INVALID_JOB_ID
Invalid job ID constant.
Information about a job in the DAG.
std::chrono::steady_clock::time_point submit_time
When job was added to DAG.
std::optional< std::string > error_message
Error message if failed.
std::vector< job_id > dependencies
Jobs this job depends on.
auto get_wait_time() const -> std::chrono::milliseconds
Calculate wait time (time from submit to start)
std::chrono::steady_clock::time_point end_time
When execution ended.
dag_job_state state
Current job state.
std::vector< job_id > dependents
Jobs that depend on this job.
std::optional< std::any > result
Result value for passing between jobs.
auto get_execution_time() const -> std::chrono::milliseconds
Calculate execution time.
std::string name
Human-readable job name.
std::chrono::steady_clock::time_point start_time
When execution started.