Central service for time-driven scheduling of framework events. More...
#include <TimerService.h>
Public Member Functions | |
TimerService () | |
~TimerService ()=default | |
void | withLock (const std::function< void()> &fn) |
void | scheduleAt (time_t unixTime, const Event &event) |
Schedule a one-time event at an absolute UNIX timestamp. | |
void | scheduleAt (time_t unixTime, const Event &event, const std::string &jobId) |
void | scheduleEvery (uint32_t intervalMs, const Event &event) |
Schedule a repeating event at fixed intervals. | |
void | scheduleEvery (uint32_t intervalMs, const Event &event, const std::string &jobId) |
void | scheduleDailyAt (TimeOfDay time, DaysOfWeek days, const Event &event) |
Schedule a recurring event based on time-of-day and day mask. | |
void | scheduleDailyAt (TimeOfDay time, DaysOfWeek days, const Event &event, const std::string &jobId) |
void | scheduleDuration (TimeOfDay start, DaysOfWeek days, uint32_t durationMs, const Event &startEvent, const Event &stopEvent) |
Schedule a start event and stop event with a delay between them. | |
void | scheduleDuration (TimeOfDay start, DaysOfWeek days, uint32_t durationMs, const Event &startEvent, const Event &stopEvent, const std::string &baseJobId) |
void | checkMissedEvents (time_t now) |
Detect and fire any missed events after a reboot (TBD). | |
bool | cancel (const std::string &jobId) |
void | scheduleCallbackAt (time_t unixTime, std::function< void()> callback) |
Schedule a one-shot callback at a given absolute time. | |
Static Public Member Functions | |
static TimerService & | instance () |
Access the singleton instance. | |
Private Member Functions | |
void | rescheduleDailyJob (const TimerJob &job) |
Placeholder for persistence/rescheduling in the future. | |
Private Attributes | |
SemaphoreHandle_t | lock_ |
std::unordered_map< std::string, TimerHandle_t > | scheduledJobs |
Map of job IDs to TimerHandles. | |
Static Private Attributes | |
static StaticSemaphore_t | lockBuffer_ |
Definition at line 69 of file TimerService.h.
TimerService::TimerService | ( | ) |
Definition at line 47 of file TimerService.cpp.
References configASSERT, lock_, and lockBuffer_.
|
default |
bool TimerService::cancel | ( | const std::string & | jobId | ) |
Definition at line 261 of file TimerService.cpp.
References scheduledJobs, and withLock().
void TimerService::checkMissedEvents | ( | time_t | now | ) |
Detect and fire any missed events after a reboot (TBD).
now | Current UNIX timestamp. |
now | Current UNIX timestamp. |
Definition at line 276 of file TimerService.cpp.
|
static |
Access the singleton instance.
Definition at line 88 of file TimerService.cpp.
|
private |
Placeholder for persistence/rescheduling in the future.
Definition at line 282 of file TimerService.cpp.
Referenced by scheduleDailyAt(), and scheduleDuration().
void TimerService::scheduleAt | ( | time_t | unixTime, |
const Event & | event | ||
) |
Schedule a one-time event at an absolute UNIX timestamp.
unixTime | Epoch time in seconds. |
event | Event to trigger. |
unixTime | Epoch time in seconds. |
event | Event to trigger. |
Definition at line 95 of file TimerService.cpp.
References scheduleAt().
Referenced by scheduleAt().
void TimerService::scheduleAt | ( | time_t | unixTime, |
const Event & | event, | ||
const std::string & | jobId | ||
) |
Definition at line 101 of file TimerService.cpp.
References AppContext::getTypeKey(), PicoTime::now(), scheduledJobs, and withLock().
void TimerService::scheduleCallbackAt | ( | time_t | unixTime, |
std::function< void()> | callback | ||
) |
unixTime | The absolute time to invoke the callback (in seconds). |
callback | The callback to execute. |
Definition at line 287 of file TimerService.cpp.
References PicoTime::now().
void TimerService::scheduleDailyAt | ( | TimeOfDay | time, |
DaysOfWeek | days, | ||
const Event & | event | ||
) |
Schedule a recurring event based on time-of-day and day mask.
time | Time of day to trigger. |
days | Days to run (bitmask). |
event | Event to trigger. |
time | Time of day to trigger. |
days | Days to run (bitmask). |
event | Event to trigger. |
Definition at line 163 of file TimerService.cpp.
References Notification::code(), Event::notification, scheduleDailyAt(), and toSeconds().
Referenced by scheduleDailyAt().
void TimerService::scheduleDailyAt | ( | TimeOfDay | time, |
DaysOfWeek | days, | ||
const Event & | event, | ||
const std::string & | jobId | ||
) |
Definition at line 169 of file TimerService.cpp.
References AppContext::getTypeKey(), PicoTime::now(), rescheduleDailyJob(), scheduledJobs, secondsUntilNextMatch(), and withLock().
void TimerService::scheduleDuration | ( | TimeOfDay | start, |
DaysOfWeek | days, | ||
uint32_t | durationMs, | ||
const Event & | startEvent, | ||
const Event & | stopEvent | ||
) |
Schedule a start event and stop event with a delay between them.
start | Start time of day. |
days | Days to run. |
durationMs | Duration after start to trigger stop. |
startEvent | Event to post at start. |
stopEvent | Event to post after duration. |
start | Start time of day. |
days | Days to run. |
durationMs | Duration after start to trigger stop. |
startEvent | Event to post at start. |
stopEvent | Event to post after duration. |
Definition at line 198 of file TimerService.cpp.
References Notification::code(), Event::notification, and scheduleDuration().
Referenced by scheduleDuration().
void TimerService::scheduleDuration | ( | TimeOfDay | start, |
DaysOfWeek | days, | ||
uint32_t | durationMs, | ||
const Event & | startEvent, | ||
const Event & | stopEvent, | ||
const std::string & | baseJobId | ||
) |
Definition at line 205 of file TimerService.cpp.
References AppContext::getTypeKey(), PicoTime::now(), rescheduleDailyJob(), scheduledJobs, secondsUntilNextMatch(), and withLock().
void TimerService::scheduleEvery | ( | uint32_t | intervalMs, |
const Event & | event | ||
) |
Schedule a repeating event at fixed intervals.
intervalMs | Interval in milliseconds. |
event | Event to trigger repeatedly. |
intervalMs | Interval in milliseconds. |
event | Event to trigger repeatedly. |
Definition at line 135 of file TimerService.cpp.
References Notification::code(), Event::notification, and scheduleEvery().
Referenced by scheduleEvery().
void TimerService::scheduleEvery | ( | uint32_t | intervalMs, |
const Event & | event, | ||
const std::string & | jobId | ||
) |
Definition at line 141 of file TimerService.cpp.
References AppContext::getTypeKey(), scheduledJobs, and withLock().
void TimerService::withLock | ( | const std::function< void()> & | fn | ) |
Definition at line 52 of file TimerService.cpp.
References lock_.
Referenced by cancel(), scheduleAt(), scheduleDailyAt(), scheduleDuration(), and scheduleEvery().
|
private |
Definition at line 144 of file TimerService.h.
Referenced by TimerService(), and withLock().
|
staticprivate |
scheduleAt
, scheduleDailyAt
, scheduleDuration
) dynamically allocate an Event
using new
and clean it up automatically after posting (inside the timer callback).Repeating timers (scheduleEvery
) also allocate an Event
, which is reused on each invocation. It is not deleted automatically and is assumed to remain valid for the lifetime of the timer.
It is now possible to cancel scheduled jobs using a job ID which will delete the timer and free the associated event.
Definition at line 145 of file TimerService.h.
Referenced by TimerService().
|
private |
Definition at line 147 of file TimerService.h.
Referenced by cancel(), scheduleAt(), scheduleDailyAt(), scheduleDuration(), and scheduleEvery().