mirror of
https://github.com/PerMalmberg/libcron.git
synced 2025-07-03 17:32:57 -05:00
Compare commits
1 Commits
feature/al
...
v1.3.0
Author | SHA1 | Date | |
---|---|---|---|
7ef39558a1 |
30
README.md
30
README.md
@ -8,7 +8,7 @@ Libcron offers an easy to use API to add callbacks with corresponding cron-forma
|
|||||||
```
|
```
|
||||||
libcron::Cron cron;
|
libcron::Cron cron;
|
||||||
|
|
||||||
cron.add_schedule("Hello from Cron", "* * * * * ?", [](*/std::chrono::system_clock::duration delayed_by*/) {
|
cron.add_schedule("Hello from Cron", "* * * * * ?", [=](auto&) {
|
||||||
std::cout << "Hello from libcron!" std::endl;
|
std::cout << "Hello from libcron!" std::endl;
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
@ -23,6 +23,30 @@ while(true)
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
The callback must have the following signature:
|
||||||
|
|
||||||
|
```
|
||||||
|
std::function<void(const libcron::TaskInformation&)>
|
||||||
|
```
|
||||||
|
|
||||||
|
`libcron::Taskinformation` offers a convenient API to retrieve further information:
|
||||||
|
|
||||||
|
- `libcron::TaskInformation::get_delay` informs about the delay between planned and actual execution of the callback. Hence, it is possible to ensure that a task was executed within a specific tolerance:
|
||||||
|
|
||||||
|
```
|
||||||
|
libcron::Cron cron;
|
||||||
|
|
||||||
|
cron.add_schedule("Hello from Cron", "* * * * * ?", [=](auto& i) {
|
||||||
|
using namespace std::chrono_literals;
|
||||||
|
if (i.get_delay() >= 1s)
|
||||||
|
{
|
||||||
|
std::cout << "The Task was executed too late..." << std::endl;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Removing schedules from `libcron::Cron`
|
## Removing schedules from `libcron::Cron`
|
||||||
|
|
||||||
libcron::Cron offers two convenient functions to remove schedules:
|
libcron::Cron offers two convenient functions to remove schedules:
|
||||||
@ -32,6 +56,8 @@ libcron::Cron offers two convenient functions to remove schedules:
|
|||||||
|
|
||||||
For example, `cron.remove_schedule("Hello from Cron")` will remove the previously added task.
|
For example, `cron.remove_schedule("Hello from Cron")` will remove the previously added task.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Removing/Adding tasks at runtime in a multithreaded environment
|
## Removing/Adding tasks at runtime in a multithreaded environment
|
||||||
|
|
||||||
When Calling `libcron::Cron::tick` from another thread than `add_schedule`, `clear_schedule` and `remove_schedule`, one must take care to protect the internal resources of `libcron::Cron` so that tasks are not removed or added while `libcron::Cron` is iterating over the schedules. `libcron::Cron` can take care of that, you simply have to define your own aliases:
|
When Calling `libcron::Cron::tick` from another thread than `add_schedule`, `clear_schedule` and `remove_schedule`, one must take care to protect the internal resources of `libcron::Cron` so that tasks are not removed or added while `libcron::Cron` is iterating over the schedules. `libcron::Cron` can take care of that, you simply have to define your own aliases:
|
||||||
@ -48,7 +74,7 @@ class Cron
|
|||||||
using CronMt = libcron::Cron<libcron::LocalClock, libcron::Locker>
|
using CronMt = libcron::Cron<libcron::LocalClock, libcron::Locker>
|
||||||
|
|
||||||
CronMt cron;
|
CronMt cron;
|
||||||
cron.add_schedule("Hello from Cron", "* * * * * ?", []() {
|
cron.add_schedule("Hello from Cron", "* * * * * ?", [=]() {
|
||||||
std::cout << "Hello from CronMt!" std::endl;
|
std::cout << "Hello from CronMt!" std::endl;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -20,12 +20,10 @@ namespace libcron
|
|||||||
class Locker
|
class Locker
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Locker() : lck(m, std::defer_lock) {}
|
void lock() { m.lock(); }
|
||||||
void lock() { lck.lock(); }
|
void unlock() { m.unlock(); }
|
||||||
void unlock() { lck.unlock(); }
|
|
||||||
private:
|
private:
|
||||||
std::mutex m{};
|
std::recursive_mutex m{};
|
||||||
std::unique_lock<std::mutex> lck;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename ClockType, typename LockType>
|
template<typename ClockType, typename LockType>
|
||||||
@ -38,7 +36,6 @@ namespace libcron
|
|||||||
class Cron
|
class Cron
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
bool add_schedule(std::string name, const std::string& schedule, Task::TaskFunction work);
|
bool add_schedule(std::string name, const std::string& schedule, Task::TaskFunction work);
|
||||||
void clear_schedules();
|
void clear_schedules();
|
||||||
void remove_schedule(const std::string& name);
|
void remove_schedule(const std::string& name);
|
||||||
@ -148,7 +145,7 @@ namespace libcron
|
|||||||
if (res)
|
if (res)
|
||||||
{
|
{
|
||||||
tasks.lock_queue();
|
tasks.lock_queue();
|
||||||
Task t{std::move(name), CronSchedule{cron}, std::move(work)};
|
Task t{std::move(name), CronSchedule{cron}, work };
|
||||||
if (t.calculate_next(clock.now()))
|
if (t.calculate_next(clock.now()))
|
||||||
{
|
{
|
||||||
tasks.push(t);
|
tasks.push(t);
|
||||||
|
@ -1,28 +1,42 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include "CronData.h"
|
|
||||||
#include "CronSchedule.h"
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include "CronData.h"
|
||||||
|
#include "CronSchedule.h"
|
||||||
|
|
||||||
namespace libcron
|
namespace libcron
|
||||||
{
|
{
|
||||||
class Task
|
class TaskInformation
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using TaskFunction = std::function<void(std::chrono::system_clock::duration)>;
|
virtual ~TaskInformation() = default;
|
||||||
|
virtual std::chrono::system_clock::duration get_delay() const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
Task(std::string name, CronSchedule schedule, TaskFunction task)
|
class Task : public TaskInformation
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using TaskFunction = std::function<void(const TaskInformation&)>;
|
||||||
|
|
||||||
|
Task(std::string name, const CronSchedule schedule, TaskFunction task)
|
||||||
: name(std::move(name)), schedule(std::move(schedule)), task(std::move(task))
|
: name(std::move(name)), schedule(std::move(schedule)), task(std::move(task))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void execute(std::chrono::system_clock::time_point now)
|
void execute(std::chrono::system_clock::time_point now)
|
||||||
{
|
{
|
||||||
auto delay = now - last_run;
|
// Next Schedule is still the current schedule, calculate delay (actual execution - planned execution)
|
||||||
|
delay = now - next_schedule;
|
||||||
|
|
||||||
last_run = now;
|
last_run = now;
|
||||||
task(delay);
|
task(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::chrono::system_clock::duration get_delay() const override
|
||||||
|
{
|
||||||
|
return delay;
|
||||||
}
|
}
|
||||||
|
|
||||||
Task(const Task& other) = default;
|
Task(const Task& other) = default;
|
||||||
@ -52,6 +66,7 @@ namespace libcron
|
|||||||
std::string name;
|
std::string name;
|
||||||
CronSchedule schedule;
|
CronSchedule schedule;
|
||||||
std::chrono::system_clock::time_point next_schedule;
|
std::chrono::system_clock::time_point next_schedule;
|
||||||
|
std::chrono::system_clock::duration delay = std::chrono::seconds(-1);
|
||||||
TaskFunction task;
|
TaskFunction task;
|
||||||
bool valid = false;
|
bool valid = false;
|
||||||
std::chrono::system_clock::time_point last_run = std::numeric_limits<std::chrono::system_clock::time_point>::min();
|
std::chrono::system_clock::time_point last_run = std::numeric_limits<std::chrono::system_clock::time_point>::min();
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
#include <iostream>
|
|
||||||
#include "libcron/Task.h"
|
#include "libcron/Task.h"
|
||||||
|
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
|
@ -23,13 +23,13 @@ void test(const char* const random_schedule, bool expect_failure = false)
|
|||||||
if(expect_failure)
|
if(expect_failure)
|
||||||
{
|
{
|
||||||
// Parsing of random might succeed, but it yields an invalid schedule.
|
// Parsing of random might succeed, but it yields an invalid schedule.
|
||||||
auto r = std::get<0>(res) && cron.add_schedule("validate schedule", schedule, [](auto) {});
|
auto r = std::get<0>(res) && cron.add_schedule("validate schedule", schedule, [](auto&) {});
|
||||||
REQUIRE_FALSE(r);
|
REQUIRE_FALSE(r);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
REQUIRE(std::get<0>(res));
|
REQUIRE(std::get<0>(res));
|
||||||
REQUIRE(cron.add_schedule("validate schedule", schedule, [](auto) {}));
|
REQUIRE(cron.add_schedule("validate schedule", schedule, [](auto&) {}));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ SCENARIO("Adding a task")
|
|||||||
WHEN("Adding a task that runs every second")
|
WHEN("Adding a task that runs every second")
|
||||||
{
|
{
|
||||||
REQUIRE(c.add_schedule("A task", "* * * * * ?",
|
REQUIRE(c.add_schedule("A task", "* * * * * ?",
|
||||||
[&expired](auto)
|
[&expired](auto&)
|
||||||
{
|
{
|
||||||
expired = true;
|
expired = true;
|
||||||
})
|
})
|
||||||
@ -68,7 +68,7 @@ SCENARIO("Adding a task that expires in the future")
|
|||||||
Cron<> c;
|
Cron<> c;
|
||||||
REQUIRE(c.add_schedule("A task",
|
REQUIRE(c.add_schedule("A task",
|
||||||
create_schedule_expiring_in(c.get_clock().now(), hours{0}, minutes{0}, seconds{3}),
|
create_schedule_expiring_in(c.get_clock().now(), hours{0}, minutes{0}, seconds{3}),
|
||||||
[&expired](auto)
|
[&expired](auto&)
|
||||||
{
|
{
|
||||||
expired = true;
|
expired = true;
|
||||||
})
|
})
|
||||||
@ -99,6 +99,50 @@ SCENARIO("Adding a task that expires in the future")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SCENARIO("Get delay using Task-Information")
|
||||||
|
{
|
||||||
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
|
GIVEN("A Cron instance with one task expiring in 2 seconds, but taking 3 seconds to execute")
|
||||||
|
{
|
||||||
|
auto _2_second_expired = 0;
|
||||||
|
auto _delay = std::chrono::system_clock::duration(-1s);
|
||||||
|
|
||||||
|
Cron<> c;
|
||||||
|
REQUIRE(c.add_schedule("Two",
|
||||||
|
"*/2 * * * * ?",
|
||||||
|
[&_2_second_expired, &_delay](auto& i)
|
||||||
|
{
|
||||||
|
_2_second_expired++;
|
||||||
|
_delay = i.get_delay();
|
||||||
|
std::this_thread::sleep_for(3s);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
THEN("Not yet expired")
|
||||||
|
{
|
||||||
|
REQUIRE_FALSE(_2_second_expired);
|
||||||
|
REQUIRE(_delay <= 0s);
|
||||||
|
}
|
||||||
|
WHEN("Exactly schedule task")
|
||||||
|
{
|
||||||
|
while (_2_second_expired == 0)
|
||||||
|
c.tick();
|
||||||
|
|
||||||
|
THEN("Task should have expired within a valid time")
|
||||||
|
{
|
||||||
|
REQUIRE(_2_second_expired == 1);
|
||||||
|
REQUIRE(_delay <= 1s);
|
||||||
|
}
|
||||||
|
AND_THEN("Executing another tick again, leading to execute task again immediatly, but not on time as execution has taken 3 seconds.")
|
||||||
|
{
|
||||||
|
c.tick();
|
||||||
|
REQUIRE(_2_second_expired == 2);
|
||||||
|
REQUIRE(_delay >= 1s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SCENARIO("Task priority")
|
SCENARIO("Task priority")
|
||||||
{
|
{
|
||||||
GIVEN("A Cron instance with two tasks expiring in 3 and 5 seconds, added in 'reverse' order")
|
GIVEN("A Cron instance with two tasks expiring in 3 and 5 seconds, added in 'reverse' order")
|
||||||
@ -110,7 +154,7 @@ SCENARIO("Task priority")
|
|||||||
Cron<> c;
|
Cron<> c;
|
||||||
REQUIRE(c.add_schedule("Five",
|
REQUIRE(c.add_schedule("Five",
|
||||||
create_schedule_expiring_in(c.get_clock().now(), hours{0}, minutes{0}, seconds{5}),
|
create_schedule_expiring_in(c.get_clock().now(), hours{0}, minutes{0}, seconds{5}),
|
||||||
[&_5_second_expired](auto)
|
[&_5_second_expired](auto&)
|
||||||
{
|
{
|
||||||
_5_second_expired++;
|
_5_second_expired++;
|
||||||
})
|
})
|
||||||
@ -118,7 +162,7 @@ SCENARIO("Task priority")
|
|||||||
|
|
||||||
REQUIRE(c.add_schedule("Three",
|
REQUIRE(c.add_schedule("Three",
|
||||||
create_schedule_expiring_in(c.get_clock().now(), hours{0}, minutes{0}, seconds{3}),
|
create_schedule_expiring_in(c.get_clock().now(), hours{0}, minutes{0}, seconds{3}),
|
||||||
[&_3_second_expired](auto)
|
[&_3_second_expired](auto&)
|
||||||
{
|
{
|
||||||
_3_second_expired++;
|
_3_second_expired++;
|
||||||
})
|
})
|
||||||
@ -231,7 +275,7 @@ SCENARIO("Clock changes")
|
|||||||
clock.set(sys_days{2018_y / 05 / 05});
|
clock.set(sys_days{2018_y / 05 / 05});
|
||||||
|
|
||||||
// Every hour
|
// Every hour
|
||||||
REQUIRE(c.add_schedule("Clock change task", "0 0 * * * ?", [](auto)
|
REQUIRE(c.add_schedule("Clock change task", "0 0 * * * ?", [](auto&)
|
||||||
{
|
{
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@ -309,7 +353,7 @@ SCENARIO("Multiple ticks per second")
|
|||||||
int run_count = 0;
|
int run_count = 0;
|
||||||
|
|
||||||
// Every 10 seconds
|
// Every 10 seconds
|
||||||
REQUIRE(c.add_schedule("Clock change task", "*/10 0 * * * ?", [&run_count](auto)
|
REQUIRE(c.add_schedule("Clock change task", "*/10 0 * * * ?", [&run_count](auto&)
|
||||||
{
|
{
|
||||||
run_count++;
|
run_count++;
|
||||||
})
|
})
|
||||||
@ -333,25 +377,7 @@ SCENARIO("Multiple ticks per second")
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
SCENARIO("Delayed run")
|
|
||||||
{
|
|
||||||
Cron<TestClock> c{};
|
|
||||||
auto& clock = c.get_clock();
|
|
||||||
|
|
||||||
auto now = sys_days{ 2018_y / 05 / 05 };
|
|
||||||
clock.set(now);
|
|
||||||
|
|
||||||
// 3 seconds past the minute
|
|
||||||
REQUIRE(c.add_schedule("Task that expires every 3 seconds", "3 * * * * ?", [](auto delayed_by)
|
|
||||||
{
|
|
||||||
// At four past the original time we're a second late.
|
|
||||||
REQUIRE(delayed_by >= seconds{ 1 });
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
c.tick(now + seconds{ 4 });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SCENARIO("Tasks can be added and removed from the scheduler")
|
SCENARIO("Tasks can be added and removed from the scheduler")
|
||||||
@ -364,35 +390,35 @@ SCENARIO("Tasks can be added and removed from the scheduler")
|
|||||||
WHEN("Adding 5 tasks that runs every second")
|
WHEN("Adding 5 tasks that runs every second")
|
||||||
{
|
{
|
||||||
REQUIRE(c.add_schedule("Task-1", "* * * * * ?",
|
REQUIRE(c.add_schedule("Task-1", "* * * * * ?",
|
||||||
[&expired](auto)
|
[&expired](auto&)
|
||||||
{
|
{
|
||||||
expired = true;
|
expired = true;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
REQUIRE(c.add_schedule("Task-2", "* * * * * ?",
|
REQUIRE(c.add_schedule("Task-2", "* * * * * ?",
|
||||||
[&expired](auto)
|
[&expired](auto&)
|
||||||
{
|
{
|
||||||
expired = true;
|
expired = true;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
REQUIRE(c.add_schedule("Task-3", "* * * * * ?",
|
REQUIRE(c.add_schedule("Task-3", "* * * * * ?",
|
||||||
[&expired](auto)
|
[&expired](auto&)
|
||||||
{
|
{
|
||||||
expired = true;
|
expired = true;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
REQUIRE(c.add_schedule("Task-4", "* * * * * ?",
|
REQUIRE(c.add_schedule("Task-4", "* * * * * ?",
|
||||||
[&expired](auto)
|
[&expired](auto&)
|
||||||
{
|
{
|
||||||
expired = true;
|
expired = true;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
REQUIRE(c.add_schedule("Task-5", "* * * * * ?",
|
REQUIRE(c.add_schedule("Task-5", "* * * * * ?",
|
||||||
[&expired](auto)
|
[&expired](auto&)
|
||||||
{
|
{
|
||||||
expired = true;
|
expired = true;
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user