24#include <condition_variable>
189 std::shared_ptr<kcenon::thread::thread_pool> thread_pool,
209 std::shared_ptr<kcenon::common::interfaces::IExecutor> executor,
245 void stop(
bool wait_for_completion =
true);
252 [[nodiscard]]
auto is_running() const noexcept ->
bool;
313 const std::
string&
name,
314 const std::
string& description,
315 std::chrono::seconds interval,
328 const std::
string&
name,
329 const std::
string& description,
343 const std::
string&
name,
344 const std::
string& description,
345 std::chrono::system_clock::time_point execute_at,
491 [[nodiscard]] auto
pending_count() const noexcept -> std::
size_t;
498 [[nodiscard]] auto
running_count() const noexcept -> std::
size_t;
572 std::chrono::system_clock::time_point from =
573 std::chrono::system_clock::now()) const
574 -> std::optional<std::chrono::system_clock::time_point>;
585 std::chrono::system_clock::time_point from) const
586 -> std::optional<std::chrono::system_clock::time_point>;
674 std::shared_ptr<kcenon::common::interfaces::IExecutor>
executor_;
686 std::condition_variable
cv_;
695 std::map<task_id, scheduled_task>
tasks_;
std::atomic< uint64_t > next_task_id_
Next task ID counter.
storage::file_storage * file_storage_
Optional reference to file storage.
storage::index_database & database_
Reference to PACS index database.
void execute_cycle()
Execute a single scheduler cycle.
auto generate_execution_id() const -> std::string
Generate unique execution ID.
std::map< task_id, scheduled_task > tasks_
All scheduled tasks (id -> task)
void update_stats(const task_execution_record &record)
Update statistics after execution.
auto deserialize_tasks(const std::string &json) -> std::size_t
Deserialize tasks from JSON.
auto execute_task(scheduled_task &task) -> task_execution_record
Execute a single task.
task_scheduler(const task_scheduler &)=delete
Non-copyable.
std::condition_variable cv_
Condition variable for sleep/wake.
auto pause_task(const task_id &id) -> bool
Pause a scheduled task.
std::atomic< bool > running_
Flag indicating scheduler is running.
auto cancel_task(const task_id &id) -> bool
Cancel a scheduled task.
auto create_verification_callback(const verification_config &config) -> task_callback_with_result
Create verification task callback.
auto save_tasks() const -> bool
Save all tasks to persistence storage.
std::mutex mutex_
Mutex for thread synchronization.
std::mutex stats_mutex_
Mutex for statistics.
task_scheduler(storage::index_database &database, const task_scheduler_config &config={})
Construct task scheduler.
task_scheduler_config config_
Service configuration.
std::shared_ptr< kcenon::common::interfaces::IExecutor > executor_
IExecutor for task execution (recommended, Issue #487)
auto running_count() const noexcept -> std::size_t
Get number of running tasks.
auto calculate_next_cron_run(const cron_schedule &cron, std::chrono::system_clock::time_point from) const -> std::optional< std::chrono::system_clock::time_point >
Calculate next run time for cron schedule.
auto create_archive_callback(const archive_config &config) -> task_callback_with_result
Create archive task callback.
auto serialize_tasks() const -> std::string
Serialize tasks to JSON.
void set_task_complete_callback(task_scheduler_config::task_complete_callback callback)
Set the task complete callback.
task_scheduler & operator=(task_scheduler &&)=delete
void set_error_callback(task_scheduler_config::task_error_callback callback)
Set the error callback.
auto is_running() const noexcept -> bool
Check if the scheduler is running.
std::thread scheduler_thread_
Background scheduler thread.
auto update_schedule(const task_id &id, const kcenon::pacs::workflow::schedule &new_schedule) -> bool
Update task schedule.
std::mutex tasks_mutex_
Mutex for tasks map.
auto schedule_verification(const verification_config &config) -> task_id
Schedule verification task.
auto get_stats() const -> scheduler_stats
Get scheduler statistics.
auto list_tasks() const -> std::vector< scheduled_task >
List all scheduled tasks.
void clear_history(const task_id &id, std::size_t keep_last=0)
Clear execution history for a task.
~task_scheduler()
Destructor - ensures graceful shutdown.
auto get_execution_history(const task_id &id, std::size_t limit=100) const -> std::vector< task_execution_record >
Get execution history for a task.
task_scheduler(task_scheduler &&)=delete
Non-movable.
auto schedule_archive(const archive_config &config) -> task_id
Schedule archive task.
void run_loop()
Background thread main loop.
auto generate_task_id() const -> task_id
Generate unique task ID.
task_scheduler & operator=(const task_scheduler &)=delete
auto calculate_next_run(const kcenon::pacs::workflow::schedule &sched, std::chrono::system_clock::time_point from=std::chrono::system_clock::now()) const -> std::optional< std::chrono::system_clock::time_point >
Calculate next run time for a schedule.
void record_execution(const task_id &task_id, const task_execution_record &record)
Record task execution.
auto resume_task(const task_id &id) -> bool
Resume a paused task.
auto get_recent_executions(std::size_t limit=100) const -> std::vector< task_execution_record >
Get all recent executions.
auto load_tasks() -> std::size_t
Load tasks from persistence storage.
std::mutex history_mutex_
Mutex for execution history.
std::map< task_id, std::vector< task_execution_record > > execution_history_
Execution history (task_id -> records)
auto get_task(const task_id &id) const -> std::optional< scheduled_task >
Get a specific task by ID.
auto schedule_cleanup(const cleanup_config &config) -> task_id
Schedule cleanup task.
std::atomic< std::size_t > running_count_
Running task count.
auto pending_count() const noexcept -> std::size_t
Get number of pending tasks.
scheduler_stats stats_
Scheduler statistics.
std::atomic< uint64_t > next_execution_id_
Next execution ID counter.
auto schedule_once(const std::string &name, const std::string &description, std::chrono::system_clock::time_point execute_at, task_callback_with_result callback) -> task_id
Schedule a one-time task.
std::shared_ptr< kcenon::thread::thread_pool > thread_pool_
Thread pool for parallel task execution (legacy)
void stop(bool wait_for_completion=true)
Stop the scheduler service.
auto trigger_task(const task_id &id) -> bool
Trigger immediate execution of a task.
std::chrono::steady_clock::time_point start_time_
Start time for uptime calculation.
auto create_cleanup_callback(const cleanup_config &config) -> task_callback_with_result
Create cleanup task callback.
void start()
Start the scheduler service.
std::atomic< bool > stop_requested_
Flag to signal shutdown.
task_state
Task execution state.
std::function< std::optional< std::string >()> task_callback_with_result
Task callback with result details.
task_type
Task type enumeration.
std::string task_id
Unique task identifier.
std::variant< interval_schedule, cron_schedule, one_time_schedule > schedule
Combined schedule type.
Configuration for archive scheduling.
Configuration for cleanup scheduling.
Cron-like schedule expression.
Scheduled task definition.
Statistics for task scheduler operations.
Record of a single task execution.
Configuration for the task scheduler service.
Configuration for verification scheduling.
Configuration for task scheduler service.