game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
TaskScheduler Class Reference

Service for scheduling and running background tasks. More...

#include <task_scheduler.h>

Classes

struct  Impl

Public Types

using OnTaskComplete = std::function<void(const std::string& taskId, const TaskResult&)>
using OnTaskError = std::function<void(const std::string& taskId, const std::string& error)>
using OnTaskStateChange = std::function<void(const std::string& taskId, TaskState newState)>
using TaskFunction = std::function<TaskResult()>

Public Member Functions

 TaskScheduler ()
 TaskScheduler (const TaskScheduler &)=delete
 ~TaskScheduler ()
std::string addCacheCleanupTask (TaskFunction cleanupFunction, std::chrono::hours interval=std::chrono::hours(24))
 Add a cache cleanup task.
std::string addCloudSyncTask (TaskFunction syncFunction, std::chrono::minutes interval=std::chrono::minutes(30))
 Add a cloud sync task.
std::string addLogCleanupTask (TaskFunction cleanupFunction, std::chrono::hours interval=std::chrono::hours(168))
 Add a log cleanup task.
std::string addUpdateCheckTask (TaskFunction checkFunction, std::chrono::hours interval=std::chrono::hours(1))
 Add an update check task.
int cancelAllPendingTasks ()
 Cancel all pending tasks.
bool cancelTask (const std::string &taskId)
 Cancel a task.
int cancelTasksByName (const std::string &name)
 Cancel all tasks with a given name.
std::vector< ScheduledTask > getAllTasks () const
 Get all scheduled tasks.
int getPendingTaskCount () const
 Get number of pending tasks.
std::vector< ScheduledTask > getPendingTasks () const
 Get pending tasks (sorted by next run time).
int getRunningTaskCount () const
 Get number of running tasks.
std::optional< ScheduledTask > getTask (const std::string &taskId) const
 Get a task by ID.
std::optional< ScheduledTask > getTaskByName (const std::string &name) const
 Get a task by name.
int getTaskCount () const
 Get number of scheduled tasks.
std::vector< ScheduledTask > getTasksByState (TaskState state) const
 Get tasks by state.
int getTotalExecutedCount () const
 Get total tasks executed since start.
int getTotalFailedCount () const
 Get total failed tasks since start.
bool isPaused () const
 Check if paused.
bool isRunning () const
 Check if the scheduler is running.
bool loadTasks (const std::string &path)
 Load scheduled tasks from disk.
TaskScheduler & operator= (const TaskScheduler &)=delete
void pause ()
 Pause task execution.
bool pauseTask (const std::string &taskId)
 Pause a recurring task.
int pruneFinishedTasks ()
 Remove completed/cancelled/failed tasks from the list.
bool rescheduleTask (const std::string &taskId, std::chrono::system_clock::time_point newTime)
 Reschedule a task.
void resume ()
 Resume task execution.
bool resumeTask (const std::string &taskId)
 Resume a paused task.
std::string runNow (const std::string &name, TaskFunction task, TaskPriority priority=TaskPriority::Critical)
 Schedule a task to run immediately.
bool saveTasks (const std::string &path) const
 Save scheduled tasks to disk.
std::string schedule (const TaskDefinition &def)
 Schedule a task using a definition.
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.
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.
std::string scheduleRecurring (const std::string &name, std::chrono::seconds interval, TaskFunction task, TaskPriority priority=TaskPriority::Normal, bool runImmediately=false)
 Schedule a recurring task.
void setOnTaskComplete (OnTaskComplete callback)
void setOnTaskError (OnTaskError callback)
void setOnTaskStateChange (OnTaskStateChange callback)
bool setTaskInterval (const std::string &taskId, std::chrono::seconds newInterval)
 Change the interval of a recurring task.
void start ()
 Start the scheduler.
void stop (bool waitForRunning=true)
 Stop the scheduler.
bool triggerNow (const std::string &taskId)
 Trigger immediate execution of a task.

Detailed Description

Service for scheduling and running background tasks.

Provides a flexible system for scheduling tasks to run at specific times or at regular intervals.

Usage Example

TaskScheduler scheduler;
scheduler.start();
// Schedule a one-shot task
scheduler.scheduleOnce("check-updates",
std::chrono::minutes(5),
[]() { return checkForUpdates(); });
// Schedule a recurring task
scheduler.scheduleRecurring("cloud-sync",
std::chrono::hours(1),
[]() { return syncCloudSaves(); });
// Stop the scheduler
scheduler.stop();
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.
std::string scheduleRecurring(const std::string &name, std::chrono::seconds interval, TaskFunction task, TaskPriority priority=TaskPriority::Normal, bool runImmediately=false)
Schedule a recurring task.
void start()
Start the scheduler.
void stop(bool waitForRunning=true)
Stop the scheduler.

Definition at line 171 of file task_scheduler.h.

Member Typedef Documentation

◆ OnTaskComplete

using TaskScheduler::OnTaskComplete = std::function<void(const std::string& taskId, const TaskResult&)>

Definition at line 175 of file task_scheduler.h.

◆ OnTaskError

using TaskScheduler::OnTaskError = std::function<void(const std::string& taskId, const std::string& error)>

Definition at line 176 of file task_scheduler.h.

◆ OnTaskStateChange

using TaskScheduler::OnTaskStateChange = std::function<void(const std::string& taskId, TaskState newState)>

Definition at line 177 of file task_scheduler.h.

◆ TaskFunction

using TaskScheduler::TaskFunction = std::function<TaskResult()>

Definition at line 174 of file task_scheduler.h.

Constructor & Destructor Documentation

◆ TaskScheduler() [1/2]

TaskScheduler::TaskScheduler ( )

Definition at line 168 of file task_scheduler.cpp.

Referenced by TaskScheduler(), and operator=().

◆ ~TaskScheduler()

TaskScheduler::~TaskScheduler ( )

Definition at line 170 of file task_scheduler.cpp.

References stop().

◆ TaskScheduler() [2/2]

TaskScheduler::TaskScheduler ( const TaskScheduler & )
delete

References TaskScheduler().

Member Function Documentation

◆ addCacheCleanupTask()

std::string TaskScheduler::addCacheCleanupTask ( TaskFunction cleanupFunction,
std::chrono::hours interval = std::chrono::hours(24) )

Add a cache cleanup task.

Parameters
cleanupFunctionFunction that cleans caches
intervalHow often to clean
Returns
Task ID

Definition at line 672 of file task_scheduler.cpp.

References TaskDefinition::description, TaskDefinition::interval, Low, TaskDefinition::name, TaskDefinition::priority, TaskDefinition::recurring, schedule(), and TaskDefinition::task.

◆ addCloudSyncTask()

std::string TaskScheduler::addCloudSyncTask ( TaskFunction syncFunction,
std::chrono::minutes interval = std::chrono::minutes(30) )

Add a cloud sync task.

Parameters
syncFunctionFunction that syncs cloud saves
intervalHow often to sync
Returns
Task ID

Definition at line 659 of file task_scheduler.cpp.

References TaskDefinition::description, TaskDefinition::interval, TaskDefinition::name, Normal, TaskDefinition::priority, TaskDefinition::recurring, schedule(), and TaskDefinition::task.

◆ addLogCleanupTask()

std::string TaskScheduler::addLogCleanupTask ( TaskFunction cleanupFunction,
std::chrono::hours interval = std::chrono::hours(168) )

Add a log cleanup task.

Parameters
cleanupFunctionFunction that cleans logs
intervalHow often to clean
Returns
Task ID

Definition at line 685 of file task_scheduler.cpp.

References TaskDefinition::description, TaskDefinition::interval, Low, TaskDefinition::name, TaskDefinition::priority, TaskDefinition::recurring, schedule(), and TaskDefinition::task.

◆ addUpdateCheckTask()

std::string TaskScheduler::addUpdateCheckTask ( TaskFunction checkFunction,
std::chrono::hours interval = std::chrono::hours(1) )

Add an update check task.

Parameters
checkFunctionFunction that checks for updates
intervalHow often to check
Returns
Task ID

Definition at line 645 of file task_scheduler.cpp.

References TaskDefinition::description, TaskDefinition::interval, TaskDefinition::name, Normal, TaskDefinition::priority, TaskDefinition::recurring, TaskDefinition::runOnStartup, schedule(), and TaskDefinition::task.

◆ cancelAllPendingTasks()

int TaskScheduler::cancelAllPendingTasks ( )

Cancel all pending tasks.

Returns
Number of tasks cancelled

Definition at line 482 of file task_scheduler.cpp.

References Cancelled, and Pending.

◆ cancelTask()

bool TaskScheduler::cancelTask ( const std::string & taskId)

Cancel a task.

Parameters
taskIdTask ID
Returns
True if cancelled

Definition at line 422 of file task_scheduler.cpp.

References Cancelled, Paused, and Pending.

◆ cancelTasksByName()

int TaskScheduler::cancelTasksByName ( const std::string & name)

Cancel all tasks with a given name.

Parameters
nameTask name
Returns
Number of tasks cancelled

Definition at line 451 of file task_scheduler.cpp.

References Cancelled, Paused, and Pending.

◆ getAllTasks()

std::vector< ScheduledTask > TaskScheduler::getAllTasks ( ) const

Get all scheduled tasks.

Returns
Vector of tasks

Definition at line 380 of file task_scheduler.cpp.

◆ getPendingTaskCount()

int TaskScheduler::getPendingTaskCount ( ) const

Get number of pending tasks.

Definition at line 708 of file task_scheduler.cpp.

References getTasksByState(), and Pending.

◆ getPendingTasks()

std::vector< ScheduledTask > TaskScheduler::getPendingTasks ( ) const

Get pending tasks (sorted by next run time).

Returns
Vector of pending tasks

Definition at line 407 of file task_scheduler.cpp.

References getTasksByState(), and Pending.

◆ getRunningTaskCount()

int TaskScheduler::getRunningTaskCount ( ) const

Get number of running tasks.

Definition at line 713 of file task_scheduler.cpp.

References getTasksByState(), and Running.

◆ getTask()

std::optional< ScheduledTask > TaskScheduler::getTask ( const std::string & taskId) const

Get a task by ID.

Parameters
taskIdTask ID
Returns
Task info if found

Definition at line 354 of file task_scheduler.cpp.

◆ getTaskByName()

std::optional< ScheduledTask > TaskScheduler::getTaskByName ( const std::string & name) const

Get a task by name.

Parameters
nameTask name
Returns
Task info if found

Definition at line 366 of file task_scheduler.cpp.

◆ getTaskCount()

int TaskScheduler::getTaskCount ( ) const

Get number of scheduled tasks.

Definition at line 702 of file task_scheduler.cpp.

◆ getTasksByState()

std::vector< ScheduledTask > TaskScheduler::getTasksByState ( TaskState state) const

Get tasks by state.

Parameters
stateTask state to filter
Returns
Vector of matching tasks

Definition at line 392 of file task_scheduler.cpp.

Referenced by getPendingTaskCount(), getPendingTasks(), and getRunningTaskCount().

◆ getTotalExecutedCount()

int TaskScheduler::getTotalExecutedCount ( ) const

Get total tasks executed since start.

Definition at line 718 of file task_scheduler.cpp.

◆ getTotalFailedCount()

int TaskScheduler::getTotalFailedCount ( ) const

Get total failed tasks since start.

Definition at line 723 of file task_scheduler.cpp.

◆ isPaused()

bool TaskScheduler::isPaused ( ) const

Check if paused.

Definition at line 246 of file task_scheduler.cpp.

◆ isRunning()

bool TaskScheduler::isRunning ( ) const

Check if the scheduler is running.

Definition at line 222 of file task_scheduler.cpp.

◆ loadTasks()

bool TaskScheduler::loadTasks ( const std::string & path)

Load scheduled tasks from disk.

Parameters
pathFile path
Returns
True on success

Definition at line 777 of file task_scheduler.cpp.

◆ operator=()

TaskScheduler & TaskScheduler::operator= ( const TaskScheduler & )
delete

References TaskScheduler(), Critical, and Normal.

◆ pause()

void TaskScheduler::pause ( )

Pause task execution.

Definition at line 227 of file task_scheduler.cpp.

◆ pauseTask()

bool TaskScheduler::pauseTask ( const std::string & taskId)

Pause a recurring task.

Parameters
taskIdTask ID
Returns
True if paused

Definition at line 512 of file task_scheduler.cpp.

References Paused, and Pending.

◆ pruneFinishedTasks()

int TaskScheduler::pruneFinishedTasks ( )

Remove completed/cancelled/failed tasks from the list.

Returns
Number of tasks removed

Definition at line 619 of file task_scheduler.cpp.

References Cancelled, Completed, and Failed.

◆ rescheduleTask()

bool TaskScheduler::rescheduleTask ( const std::string & taskId,
std::chrono::system_clock::time_point newTime )

Reschedule a task.

Parameters
taskIdTask ID
newTimeNew scheduled time
Returns
True if rescheduled

Definition at line 567 of file task_scheduler.cpp.

References Completed, Failed, and Pending.

◆ resume()

void TaskScheduler::resume ( )

Resume task execution.

Definition at line 235 of file task_scheduler.cpp.

◆ resumeTask()

bool TaskScheduler::resumeTask ( const std::string & taskId)

Resume a paused task.

Parameters
taskIdTask ID
Returns
True if resumed

Definition at line 537 of file task_scheduler.cpp.

References Paused, and Pending.

◆ runNow()

std::string TaskScheduler::runNow ( const std::string & name,
TaskFunction task,
TaskPriority priority = TaskPriority::Critical )

Schedule a task to run immediately.

Parameters
nameTask name
taskTask function
priorityTask priority
Returns
Task ID

Definition at line 345 of file task_scheduler.cpp.

References scheduleOnce().

◆ saveTasks()

bool TaskScheduler::saveTasks ( const std::string & path) const

Save scheduled tasks to disk.

Parameters
pathFile path
Returns
True on success

Definition at line 754 of file task_scheduler.cpp.

◆ schedule()

◆ scheduleAt()

std::string TaskScheduler::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.

Parameters
nameTask name
whenTime to run
taskTask function
priorityTask priority
Returns
Task ID

Definition at line 319 of file task_scheduler.cpp.

References TaskDefinition::name, TaskDefinition::priority, TaskDefinition::runAt, schedule(), and TaskDefinition::task.

◆ scheduleOnce()

std::string TaskScheduler::scheduleOnce ( const std::string & name,
std::chrono::seconds delay,
TaskFunction task,
TaskPriority priority = TaskPriority::Normal )

Schedule a one-shot task with a delay.

Parameters
nameTask name
delayDelay before running
taskTask function
priorityTask priority
Returns
Task ID

Definition at line 308 of file task_scheduler.cpp.

References TaskDefinition::delay, TaskDefinition::name, TaskDefinition::priority, schedule(), and TaskDefinition::task.

Referenced by runNow().

◆ scheduleRecurring()

std::string TaskScheduler::scheduleRecurring ( const std::string & name,
std::chrono::seconds interval,
TaskFunction task,
TaskPriority priority = TaskPriority::Normal,
bool runImmediately = false )

Schedule a recurring task.

Parameters
nameTask name
intervalTime between runs
taskTask function
priorityTask priority
runImmediatelyRun once immediately
Returns
Task ID

Definition at line 331 of file task_scheduler.cpp.

References TaskDefinition::interval, TaskDefinition::name, TaskDefinition::priority, TaskDefinition::recurring, TaskDefinition::runOnStartup, schedule(), and TaskDefinition::task.

◆ setOnTaskComplete()

void TaskScheduler::setOnTaskComplete ( OnTaskComplete callback)

Definition at line 732 of file task_scheduler.cpp.

◆ setOnTaskError()

void TaskScheduler::setOnTaskError ( OnTaskError callback)

Definition at line 738 of file task_scheduler.cpp.

◆ setOnTaskStateChange()

void TaskScheduler::setOnTaskStateChange ( OnTaskStateChange callback)

Definition at line 744 of file task_scheduler.cpp.

◆ setTaskInterval()

bool TaskScheduler::setTaskInterval ( const std::string & taskId,
std::chrono::seconds newInterval )

Change the interval of a recurring task.

Parameters
taskIdTask ID
newIntervalNew interval
Returns
True if changed

Definition at line 590 of file task_scheduler.cpp.

◆ start()

void TaskScheduler::start ( )

Start the scheduler.

Begins processing scheduled tasks. Must be called before tasks will run.

Definition at line 179 of file task_scheduler.cpp.

References TaskScheduler::Impl::workerLoop().

◆ stop()

void TaskScheduler::stop ( bool waitForRunning = true)

Stop the scheduler.

Parameters
waitForRunningWait for currently running tasks to complete

Definition at line 197 of file task_scheduler.cpp.

Referenced by ~TaskScheduler().

◆ triggerNow()

bool TaskScheduler::triggerNow ( const std::string & taskId)

Trigger immediate execution of a task.

Parameters
taskIdTask ID
Returns
True if triggered

Definition at line 603 of file task_scheduler.cpp.

References Pending.


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