Feature/add executed on time check (#7)

* Initial commit on executed on time feature. A task was executed on time if the function call happened within one second since it expired.

* Adding tests, fixing some errors.

* Using recursirve mutex to allowing to call safely call was_executed_on_time in an Mt-environment

* Changing from boolean expression to get_delay, being even more flexibel

* Cleanup

* Adding dedicated TaskContext

* Changing to Interface-Class Approach

* Renaming to TaskInformation, making it pure virtual

* Removing unnecessary Proxy-Class

* Cleaning up

* Passing a const reference instead of a pointer to avoid nullptr checks in the callback

* Cleaning up add_schedule.

* Adding TaskInformation API to readme.

Co-authored-by: Heinz-Peter Liechtenecker <h.liechtenecker@fh-kaernten.at>
This commit is contained in:
Heinz-Peter Liechtenecker 2020-09-10 19:03:50 +02:00 committed by GitHub
parent 76da315c13
commit 7ef39558a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 113 additions and 33 deletions

View File

@ -1,5 +1,6 @@
cmake_minimum_required(VERSION 3.6) cmake_minimum_required(VERSION 3.6)
project(top)
add_subdirectory(libcron) add_subdirectory(libcron)
add_subdirectory(test) add_subdirectory(test)

View File

@ -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", "* * * * * ?", [=]() { cron.add_schedule("Hello from Cron", "* * * * * ?", [=](auto&) {
std::cout << "Hello from libcron!" std::endl; std::cout << "Hello from libcron!" std::endl;
}); });
``` ```
@ -23,6 +23,28 @@ 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`

View File

@ -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,8 +36,7 @@ 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, std::function<void()> work);
void clear_schedules(); void clear_schedules();
void remove_schedule(const std::string& name); void remove_schedule(const std::string& name);
@ -141,14 +138,14 @@ namespace libcron
}; };
template<typename ClockType, typename LockType> template<typename ClockType, typename LockType>
bool Cron<ClockType, LockType>::add_schedule(std::string name, const std::string& schedule, std::function<void()> work) bool Cron<ClockType, LockType>::add_schedule(std::string name, const std::string& schedule, Task::TaskFunction work)
{ {
auto cron = CronData::create(schedule); auto cron = CronData::create(schedule);
bool res = cron.is_valid(); bool res = cron.is_valid();
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);

View File

@ -1,26 +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:
virtual ~TaskInformation() = default;
virtual std::chrono::system_clock::duration get_delay() const = 0;
};
Task(std::string name, const CronSchedule schedule, std::function<void()> 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)
{ {
// Next Schedule is still the current schedule, calculate delay (actual execution - planned execution)
delay = now - next_schedule;
last_run = now; last_run = now;
task(); task(*this);
}
std::chrono::system_clock::duration get_delay() const override
{
return delay;
} }
Task(const Task& other) = default; Task(const Task& other) = default;
@ -50,7 +66,8 @@ 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::function<void()> task; std::chrono::system_clock::duration delay = std::chrono::seconds(-1);
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();
}; };

View File

@ -1,4 +1,3 @@
#include <iostream>
#include "libcron/Task.h" #include "libcron/Task.h"
using namespace std::chrono; using namespace std::chrono;

View File

@ -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 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, []() {})); REQUIRE(cron.add_schedule("validate schedule", schedule, [](auto&) {}));
} }
} }

View File

@ -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]() [&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]() [&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]() [&_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]() [&_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 * * * ?", []() 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]() REQUIRE(c.add_schedule("Clock change task", "*/10 0 * * * ?", [&run_count](auto&)
{ {
run_count++; run_count++;
}) })
@ -346,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]() [&expired](auto&)
{ {
expired = true; expired = true;
}) })
); );
REQUIRE(c.add_schedule("Task-2", "* * * * * ?", REQUIRE(c.add_schedule("Task-2", "* * * * * ?",
[&expired]() [&expired](auto&)
{ {
expired = true; expired = true;
}) })
); );
REQUIRE(c.add_schedule("Task-3", "* * * * * ?", REQUIRE(c.add_schedule("Task-3", "* * * * * ?",
[&expired]() [&expired](auto&)
{ {
expired = true; expired = true;
}) })
); );
REQUIRE(c.add_schedule("Task-4", "* * * * * ?", REQUIRE(c.add_schedule("Task-4", "* * * * * ?",
[&expired]() [&expired](auto&)
{ {
expired = true; expired = true;
}) })
); );
REQUIRE(c.add_schedule("Task-5", "* * * * * ?", REQUIRE(c.add_schedule("Task-5", "* * * * * ?",
[&expired]() [&expired](auto&)
{ {
expired = true; expired = true;
}) })