IQRF Gateway Daemon
|
Tasks scheduler. More...
#include <Scheduler.h>
Public Types | |
typedef long | TaskHandle |
![]() | |
typedef long | TaskHandle |
Task handle is task identification. More... | |
typedef std::function< void(const std::string &)> | TaskHandlerFunc |
Task to be processed handler functional type. More... | |
Public Member Functions | |
Scheduler () | |
virtual | ~Scheduler () |
void | start () override |
Start IScheduler instance. More... | |
void | stop () override |
Stop IScheduler instance. More... | |
void | registerMessageHandler (const std::string &clientId, TaskHandlerFunc fun) override |
Register task handler. More... | |
void | unregisterMessageHandler (const std::string &clientId) override |
Unregister task handler. More... | |
std::vector< std::string > | getMyTasks (const std::string &clientId) const override |
Get scheduled tasks for a client. More... | |
std::string | getMyTask (const std::string &clientId, const TaskHandle &hndl) const override |
Get a particular tasks for a client. More... | |
TaskHandle | scheduleTaskAt (const std::string &clientId, const std::string &task, const std::chrono::system_clock::time_point &tp) override |
Schedule task at time point. More... | |
TaskHandle | scheduleTaskPeriodic (const std::string &clientId, const std::string &task, const std::chrono::seconds &sec, const std::chrono::system_clock::time_point &tp) override |
Schedule periodic task. More... | |
void | removeAllMyTasks (const std::string &clientId) override |
Remove all task for client. More... | |
void | removeTask (const std::string &clientId, TaskHandle hndl) override |
Remove task for client. More... | |
void | removeTasks (const std::string &clientId, std::vector< TaskHandle > hndls) override |
Remove tasks for client. More... | |
void | updateConfiguration (const rapidjson::Value &cfg) |
Update configuration. More... | |
![]() | |
virtual | ~IScheduler () |
Additional Inherited Members | |
![]() | |
static const TaskHandle | TASK_HANDLE_INVALID = 0 |
Invalid task handle. More... | |
Tasks scheduler.
Scheduled tasks are set according a configuration file or during runtime via IScheduler interface. The tasks are scheduled periodically or according a time pattern as defined by Cron syntax. The tasks are stored as std::string and delivered to clients via registered callbacks. It is up to the client to parse content of delivered std::string and handle it approproiatelly. Handling must not block as it is done in the scheduler tasks queue and would block handling of other fired tasks. The client shall create its own handling thread in case of blocking processing.
Runtime scheduling is possible via methods scheduleTaskAt() for task firing at exact time point in future or scheduleTaskPeriodic() for periodic firing with period in soconds.
Scheduling via configuration file allows scheduling according time pattern similar to unix Cron. See for details e.g: https://en.wikipedia.org/wiki/Cron link. Time pattern consist of seven tokens. Unlike Cron it has the token for seconds: "sec min hour day mon year wday". The tokens are accepted in these Cron forms, e.g:
Token | meaning |
---|---|
* | every |
3 | 3rd |
1,2,3 | 1st, 2nd, 3rd |
5/* | every divisible by 5 |
It is possible to use Cron nicknames for time pattern.
Configuration file is accepted in this JSON format:
typedef long Scheduler::TaskHandle |
Scheduler::Scheduler | ( | ) |
|
virtual |
|
overridevirtual |
Get a particular tasks for a client.
[in] | clientId | client identification |
[in] | hndl | task handle identification |
Returns a particular task planned for a client or an empty task if doesn't exists
Implements IScheduler.
|
overridevirtual |
Get scheduled tasks for a client.
[in] | clientId | client identification |
Returns all pending scheduled tasks for the client
Implements IScheduler.
|
overridevirtual |
Register task handler.
[in] | clientId | client identification registering handler function |
[in] | fun | handler function |
Whenever the scheduler evaluate a task to be handled it is passed to the handler function. The only tasks planned for particular clientId are delivered. Repeated registration with the same client identification replaces previously registered handler
Implements IScheduler.
|
overridevirtual |
Remove all task for client.
[in] | clientId | client identification |
Scheduler removes all tasks for the client
Implements IScheduler.
|
overridevirtual |
Remove task for client.
[in] | clientId | client identification |
[in] | hndl | task handle identification |
Scheduler removes a particular tasks for the client
Implements IScheduler.
|
overridevirtual |
Remove tasks for client.
[in] | clientId | client identification |
[in] | hndls | task handles identification |
Scheduler removes a group of tasks passed in hndls for the client
Implements IScheduler.
|
overridevirtual |
Schedule task at time point.
[in] | clientId | client identification |
[in] | task | planned task |
[in] | tp | time point |
Schedules task at exact time point. When the time point is reached the task is passed to its handler and released Use it for one shot tasks
Implements IScheduler.
|
overridevirtual |
Schedule periodic task.
[in] | clientId | client identification |
[in] | task | planned task |
[in] | sec | period in seconds |
[in] | tp | time point of delayed start |
Schedules periodic task. It is started immediatelly by default, the first shot after one period. If the start shall be delayed use appropriate time point of start
Implements IScheduler.
|
overridevirtual |
Start IScheduler instance.
Scheduler implementation starts to process scheduled tasks
Implements IScheduler.
|
overridevirtual |
Stop IScheduler instance.
Scheduler implementation stops processing of scheduled tasks
Implements IScheduler.
|
overridevirtual |
Unregister task handler.
[in] | clientId | client identification |
If the handler is not required anymore, it is possible to unregister via this method.
Implements IScheduler.
void Scheduler::updateConfiguration | ( | const rapidjson::Value & | cfg | ) |
Update configuration.
[in] | cfg | configuration data |
Configuration data are taken from passed cfg and the instance is configured accordingly