This is a cron libriary write by c++ .
┌─────────────sec ( 0 - 59 )
| ┌───────────── min (0 - 59)
| │ ┌────────────── hour (0 - 23)
| │ │ ┌─────────────── day of month (1 - 31)
| │ │ │ ┌──────────────── month (1 - 12)
| │ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; )
| │ │ │ │ │
| │ │ │ │ │
* * * * * *
Field name | Mandatory? | Allowed values | Allowed special characters |
---|---|---|---|
Minutes | Yes | 0-59 | * , - ? |
Minutes | Yes | 0-59 | * , - ? |
Hours | Yes | 0-23 | * , - ? |
Day of month | Yes | 1-31 | * , - ? |
Month | Yes | 1-12 or JAN-DEC | * , - ? |
Day of week | No | 0-6 or SUN-SAT | * , - ? |
"@yearly" or "@annually"
"@monthly"
"@weekly"
"@daily" or "@midnight"
"@hourly"
"@every hh:mm:ss"
- Namespace :
Cron
- Basic swap for boost library :
typedef boost::asio::io_service IOService;
- Job function
typedef std::function<void()> Job;
- Log level and log function :
enum CronLogLevel {
Unknow = 0 ,
Info = 1,
Debug = 2 ,
Warning = 3 ,
Error = 4 ,
Fatel = 5
};
// Where Cron print it's logs
typedef std::function<void( CronLogLevel lev , const std::string & string)> CronLogger;
- Cron manager class
Cron
.
- Cron construct :
Cron
doesn't managerIOService
.Cron
and all jobs thatCron
manager are running on thisIOService
.
Cron(IOService & s , CronLogger l)
- Cron start :
void Start();
- Cron stop :
Stop
interface will topCron
. No more jobs will triggered .- Jobs that already triggered will not be stoped .
void Stop();
- Add a job into cron
AddJob
is thread-safe inteface .- you can call
AddJob
anytime . - return
int
is a handler of this job, use it to remove this job .
int AddJob(const std::string & pattern , Job j);
- Remove a specific job :.
1
RemoveJob
is thread-safe inteface . 2. you can callRemoveJob
anytime .
void RemoveJob(int id)
#include "cron.h"
#include <thread>
#include <iostream>
int main() {
Cron::IOService ios;
Cron::Cron cron(ios, [](Cron::CronLogLevel lev , const std::string & log) {
std::cout<<"Log lev : "<<lev<<" -- log message : "<<log<<std::endl;
});
cron.AddJob("* * * * *" , []() {
std::cout<<"Hello worLd"<<std::endl;
});
cron.Start();
ios.run();
return 0 ;
}
- boost
- UnitTest++ ( only if you want to compiler test code )
Any platform that CMake support and if you want to edit CMakeLists.txt for it.
At first I hope to find a cron libriary for c++ language , but after searching from github , it seems no highly stared repository of c++ cron libriary . Instead I find https://github.com/robfig/cron ,a cron libriary of Go language which has hundrends of stars . So I decided to rewrite it by c++ .
###cron language https://en.wikipedia.org/wiki/Cron