The Cronjob Scheduler is a thread-safe manager of Cronjobs.
A Cronjob is a piece of code that is executed periodically at fixed intervals. It is often used for cleanup routines or simply for logic that needs to be executed at specific intervals.
To use the Cronjob Scheduler the scheduler.h header must be included and the Scheduler class from the cronjob namespace must be instantiated:
#include "scheduler.h"
cronjob::Scheduler scheduler;- Two types of jobs are supported:
- One-shot job - Executed once at the end of the configured interval. These jobs are added to the
Schedulerby callingRunOnce(interval, callable_function, list_of_arguments). - Repetitive job - Executed every time the configured interval expires. These jobs are added to the
Schedulerby callingRun(interval, callable_function, list_of_arguments).
- One-shot job - Executed once at the end of the configured interval. These jobs are added to the
-
The
CronJob's interface allows adding and removing of jobs at any time (from any thread and even from another job). -
Each type of job could be added to the
Schedulerby its dedicated method:// Add one-shot job. std::shared_ptr<Job> Scheduler::RunOnce(int interval, _Callable&& function, _Args&& ... args); // Add repetitive job. std::shared_ptr<Job> Scheduler::Run(int interval, _Callable&& function, _Args&& ... args);
-
These methods returns pointers to the added jobs to allow clients to remove them later by calling:
void Remove(std::shared_ptr<Job>& job);
-
The pointers to the one-shot jobs are invalidated if the
Schedulerexecutes the jobs or theOnNewTime(timeval)method is called with a past time. In this case the pointer could be set tonullptrmanually or by callingRemove(job)method. -
Nothing is happening in case of double job removing.
- The
CronJob's internal "clock" is driven by the publicvoid onNewTime(const struct timeval&)method. In other words, it doesn't use the system time for the logic. onNewTime(timeval)could be called with three types of values:- The same value as the one from the previous call. In this case nothing is happening.
- A value smaller than the one from the previous call (past time). In this case no job will run, but all repetitive jobs will be rescheduled based on the new value. All one-shot jobs are removed from the
Schedulerand the pointers become invalid. - A value greater than one from the previous call (future time). In this case all jobs, that should been triggered in this period, will run just once and the repetitive ones are rescheduled.
- All jobs with expired time run in the thread that called
onNewTime(timeval). - The time resolution is of 1 second.
- Job's period are restricted to not be less than 1 (second).
- The interface doesn't support change of periods of already added jobs.
-
The Following modules were used:
- Unit testing:
- Code coverage
-
The output of the code coverage executed with gcov can be seen by opening the following file in a web browser:
cmake-build-debug/test_coverage/index.html
-
This project follows the Google C++ Style Guide.