IQRF Gateway Daemon
Public Types | Public Member Functions | List of all members
Scheduler Class Reference

Tasks scheduler. More...

#include <Scheduler.h>

Inheritance diagram for Scheduler:
IScheduler

Public Types

typedef long TaskHandle
 
- Public Types inherited from IScheduler
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...
 
- Public Member Functions inherited from IScheduler
virtual ~IScheduler ()
 

Additional Inherited Members

- Static Public Attributes inherited from IScheduler
static const TaskHandle TASK_HANDLE_INVALID = 0
 Invalid task handle. More...
 

Detailed Description

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:

{
"TasksJson" : [ #tasks array
{
"time": "*/5 6 * * * * *", #time pattern
"service" : "BaseServiceForMQTT1", #id of callback registrator
"message" : { #task (passed as std::string)
"ctype": "dpa",
"type" : "raw",
"msgid" : "1",
"timeout" : 1000,
"request" : "00.00.06.03.ff.ff",
"request_ts" : "",
"confirmation" : ".",
"confirmation_ts" : "",
"response" : ".",
"response_ts" : ""
}
]
}

Member Typedef Documentation

◆ TaskHandle

typedef long Scheduler::TaskHandle

Constructor & Destructor Documentation

◆ Scheduler()

Scheduler::Scheduler ( )

◆ ~Scheduler()

Scheduler::~Scheduler ( )
virtual

Member Function Documentation

◆ getMyTask()

std::string Scheduler::getMyTask ( const std::string &  clientId,
const TaskHandle hndl 
) const
overridevirtual

Get a particular tasks for a client.

Parameters
[in]clientIdclient identification
[in]hndltask handle identification
Returns
scheduled tasks

Returns a particular task planned for a client or an empty task if doesn't exists

Implements IScheduler.

◆ getMyTasks()

std::vector< std::string > Scheduler::getMyTasks ( const std::string &  clientId) const
overridevirtual

Get scheduled tasks for a client.

Parameters
[in]clientIdclient identification
Returns
scheduled tasks

Returns all pending scheduled tasks for the client

Implements IScheduler.

◆ registerMessageHandler()

void Scheduler::registerMessageHandler ( const std::string &  clientId,
TaskHandlerFunc  fun 
)
overridevirtual

Register task handler.

Parameters
[in]clientIdclient identification registering handler function
[in]funhandler 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.

◆ removeAllMyTasks()

void Scheduler::removeAllMyTasks ( const std::string &  clientId)
overridevirtual

Remove all task for client.

Parameters
[in]clientIdclient identification

Scheduler removes all tasks for the client

Implements IScheduler.

◆ removeTask()

void Scheduler::removeTask ( const std::string &  clientId,
TaskHandle  hndl 
)
overridevirtual

Remove task for client.

Parameters
[in]clientIdclient identification
[in]hndltask handle identification

Scheduler removes a particular tasks for the client

Implements IScheduler.

◆ removeTasks()

void Scheduler::removeTasks ( const std::string &  clientId,
std::vector< TaskHandle hndls 
)
overridevirtual

Remove tasks for client.

Parameters
[in]clientIdclient identification
[in]hndlstask handles identification

Scheduler removes a group of tasks passed in hndls for the client

Implements IScheduler.

◆ scheduleTaskAt()

Scheduler::TaskHandle Scheduler::scheduleTaskAt ( const std::string &  clientId,
const std::string &  task,
const std::chrono::system_clock::time_point &  tp 
)
overridevirtual

Schedule task at time point.

Parameters
[in]clientIdclient identification
[in]taskplanned task
[in]tptime point
Returns
scheduled task handle

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.

◆ scheduleTaskPeriodic()

Scheduler::TaskHandle Scheduler::scheduleTaskPeriodic ( const std::string &  clientId,
const std::string &  task,
const std::chrono::seconds &  sec,
const std::chrono::system_clock::time_point &  tp 
)
overridevirtual

Schedule periodic task.

Parameters
[in]clientIdclient identification
[in]taskplanned task
[in]secperiod in seconds
[in]tptime point of delayed start
Returns
scheduled task handle

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.

◆ start()

void Scheduler::start ( )
overridevirtual

Start IScheduler instance.

Scheduler implementation starts to process scheduled tasks

Implements IScheduler.

◆ stop()

void Scheduler::stop ( )
overridevirtual

Stop IScheduler instance.

Scheduler implementation stops processing of scheduled tasks

Implements IScheduler.

◆ unregisterMessageHandler()

void Scheduler::unregisterMessageHandler ( const std::string &  clientId)
overridevirtual

Unregister task handler.

Parameters
[in]clientIdclient identification

If the handler is not required anymore, it is possible to unregister via this method.

Implements IScheduler.

◆ updateConfiguration()

void Scheduler::updateConfiguration ( const rapidjson::Value &  cfg)

Update configuration.

Parameters
[in]cfgconfiguration data

Configuration data are taken from passed cfg and the instance is configured accordingly


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