91 std::optional<std::chrono::system_clock::time_point>
lastRunAt;
92 std::optional<std::chrono::system_clock::time_point>
nextRunAt;
126 std::optional<std::chrono::system_clock::time_point>
runAt;
127 std::optional<std::chrono::seconds>
delay;
176 using OnTaskError = std::function<void(
const std::string& taskId,
const std::string& error)>;
201 void stop(
bool waitForRunning =
true);
253 std::string
scheduleAt(
const std::string& name, std::chrono::system_clock::time_point when,
265 std::string
scheduleRecurring(
const std::string& name, std::chrono::seconds interval,
267 bool runImmediately =
false);
288 std::optional<ScheduledTask>
getTask(
const std::string& taskId)
const;
295 std::optional<ScheduledTask>
getTaskByName(
const std::string& name)
const;
341 bool pauseTask(
const std::string& taskId);
356 bool rescheduleTask(
const std::string& taskId, std::chrono::system_clock::time_point newTime);
364 bool setTaskInterval(
const std::string& taskId, std::chrono::seconds newInterval);
390 std::chrono::hours interval = std::chrono::hours(1));
399 std::chrono::minutes interval = std::chrono::minutes(30));
408 std::chrono::hours interval = std::chrono::hours(24));
417 std::chrono::hours interval = std::chrono::hours(168));
465 bool saveTasks(
const std::string& path)
const;
476 std::unique_ptr<Impl> impl_;
std::optional< ScheduledTask > getTask(const std::string &taskId) const
Get a task by ID.
std::function< void(const std::string &taskId, const std::string &error)> OnTaskError
std::vector< ScheduledTask > getTasksByState(TaskState state) const
Get tasks by state.
int getTotalFailedCount() const
Get total failed tasks since start.
int pruneFinishedTasks()
Remove completed/cancelled/failed tasks from the list.
std::function< void(const std::string &taskId, TaskState newState)> OnTaskStateChange
bool isRunning() const
Check if the scheduler is running.
bool setTaskInterval(const std::string &taskId, std::chrono::seconds newInterval)
Change the interval of a recurring task.
std::string scheduleOnce(const std::string &name, std::chrono::seconds delay, TaskFunction task, TaskPriority priority=TaskPriority::Normal)
Schedule a one-shot task with a delay.
bool triggerNow(const std::string &taskId)
Trigger immediate execution of a task.
std::string addUpdateCheckTask(TaskFunction checkFunction, std::chrono::hours interval=std::chrono::hours(1))
Add an update check task.
void setOnTaskComplete(OnTaskComplete callback)
int getPendingTaskCount() const
Get number of pending tasks.
TaskScheduler(const TaskScheduler &)=delete
int getRunningTaskCount() const
Get number of running tasks.
bool loadTasks(const std::string &path)
Load scheduled tasks from disk.
int cancelTasksByName(const std::string &name)
Cancel all tasks with a given name.
void setOnTaskError(OnTaskError callback)
TaskScheduler & operator=(const TaskScheduler &)=delete
std::string scheduleRecurring(const std::string &name, std::chrono::seconds interval, TaskFunction task, TaskPriority priority=TaskPriority::Normal, bool runImmediately=false)
Schedule a recurring task.
std::string schedule(const TaskDefinition &def)
Schedule a task using a definition.
int cancelAllPendingTasks()
Cancel all pending tasks.
std::string runNow(const std::string &name, TaskFunction task, TaskPriority priority=TaskPriority::Critical)
Schedule a task to run immediately.
bool resumeTask(const std::string &taskId)
Resume a paused task.
void start()
Start the scheduler.
std::string addCacheCleanupTask(TaskFunction cleanupFunction, std::chrono::hours interval=std::chrono::hours(24))
Add a cache cleanup task.
bool isPaused() const
Check if paused.
std::optional< ScheduledTask > getTaskByName(const std::string &name) const
Get a task by name.
std::vector< ScheduledTask > getAllTasks() const
Get all scheduled tasks.
bool cancelTask(const std::string &taskId)
Cancel a task.
std::vector< ScheduledTask > getPendingTasks() const
Get pending tasks (sorted by next run time).
std::function< TaskResult()> TaskFunction
bool pauseTask(const std::string &taskId)
Pause a recurring task.
std::string scheduleAt(const std::string &name, std::chrono::system_clock::time_point when, TaskFunction task, TaskPriority priority=TaskPriority::Normal)
Schedule a task for a specific time.
int getTaskCount() const
Get number of scheduled tasks.
void setOnTaskStateChange(OnTaskStateChange callback)
std::string addLogCleanupTask(TaskFunction cleanupFunction, std::chrono::hours interval=std::chrono::hours(168))
Add a log cleanup task.
void resume()
Resume task execution.
void stop(bool waitForRunning=true)
Stop the scheduler.
int getTotalExecutedCount() const
Get total tasks executed since start.
bool rescheduleTask(const std::string &taskId, std::chrono::system_clock::time_point newTime)
Reschedule a task.
void pause()
Pause task execution.
bool saveTasks(const std::string &path) const
Save scheduled tasks to disk.
std::function< void(const std::string &taskId, const TaskResult &)> OnTaskComplete
std::string addCloudSyncTask(TaskFunction syncFunction, std::chrono::minutes interval=std::chrono::minutes(30))
Add a cloud sync task.
Information about a scheduled task.
std::string name
Human-readable name.
std::chrono::system_clock::time_point createdAt
std::string id
Unique task ID.
std::chrono::system_clock::time_point scheduledFor
std::optional< std::chrono::system_clock::time_point > lastRunAt
std::string getNextRunStr() const
std::string description
Task description.
std::chrono::seconds interval
std::string getStateStr() const
std::optional< std::chrono::system_clock::time_point > nextRunAt
bool runMissedOnStartup
Run if scheduled time was missed.
std::optional< TaskResult > lastResult
Definition for creating a new task.
std::chrono::seconds retryDelay
std::optional< std::chrono::seconds > delay
std::chrono::seconds interval
std::optional< std::chrono::system_clock::time_point > runAt
std::function< TaskResult()> task
Task function to execute.
Result of a task execution.
std::chrono::milliseconds duration
std::optional< std::string > errorDetails
std::string taskPriorityName(TaskPriority priority)
Get human-readable priority name.
TaskState
Current state of a scheduled task.
@ Completed
Finished successfully.
@ Running
Currently executing.
@ Cancelled
Was cancelled.
@ Failed
Finished with error.
@ Paused
Temporarily paused.
std::string taskStateName(TaskState state)
Get human-readable state name.
TaskPriority
Priority levels for tasks.
@ Critical
Must run ASAP (user-initiated).
@ Low
Background maintenance tasks.
@ High
Important tasks (update checks).
@ Normal
Regular scheduled tasks.