Implement delayed_by parameter to task callback.

This commit is contained in:
Per Malmberg 2020-09-06 14:01:50 +02:00
parent d1ac26bd94
commit a51aae6c65
5 changed files with 40 additions and 24 deletions

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", "* * * * * ?", [](*/std::chrono::system_clock::duration delayed_by*/) {
std::cout << "Hello from libcron!" std::endl; std::cout << "Hello from libcron!" std::endl;
}); });
``` ```
@ -23,8 +23,6 @@ while(true)
} }
``` ```
## 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:
@ -34,8 +32,6 @@ 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:
@ -52,7 +48,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;
}); });

View File

@ -39,7 +39,7 @@ namespace libcron
{ {
public: public:
bool add_schedule(std::string name, const std::string& schedule, std::function<void()> 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);
@ -141,7 +141,7 @@ 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();

View File

@ -11,16 +11,18 @@ namespace libcron
class Task class Task
{ {
public: public:
using TaskFunction = std::function<void(std::chrono::system_clock::duration)>;
Task(std::string name, const CronSchedule schedule, std::function<void()> task) Task(std::string name, 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;
last_run = now; last_run = now;
task(); task(delay);
} }
Task(const Task& other) = default; Task(const Task& other) = default;
@ -50,7 +52,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::function<void()> 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();
}; };

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;
}) })
@ -110,7 +110,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 +118,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 +231,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 +309,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++;
}) })
@ -333,7 +333,25 @@ 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")
@ -346,35 +364,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;
}) })