From a51aae6c65319579b5d9310bb268893d46a41345 Mon Sep 17 00:00:00 2001 From: Per Malmberg Date: Sun, 6 Sep 2020 14:01:50 +0200 Subject: [PATCH] Implement delayed_by parameter to task callback. --- README.md | 8 ++----- libcron/include/libcron/Cron.h | 4 ++-- libcron/include/libcron/Task.h | 8 ++++--- test/CronRandomizationTest.cpp | 4 ++-- test/CronTest.cpp | 40 ++++++++++++++++++++++++---------- 5 files changed, 40 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 4a8cfcf..b1941a8 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Libcron offers an easy to use API to add callbacks with corresponding cron-forma ``` 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; }); ``` @@ -23,8 +23,6 @@ while(true) } ``` - - ## Removing schedules from `libcron::Cron` 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. - - ## 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: @@ -52,7 +48,7 @@ class Cron using CronMt = libcron::Cron CronMt cron; -cron.add_schedule("Hello from Cron", "* * * * * ?", [=]() { +cron.add_schedule("Hello from Cron", "* * * * * ?", []() { std::cout << "Hello from CronMt!" std::endl; }); diff --git a/libcron/include/libcron/Cron.h b/libcron/include/libcron/Cron.h index 43651cc..8e1589c 100644 --- a/libcron/include/libcron/Cron.h +++ b/libcron/include/libcron/Cron.h @@ -39,7 +39,7 @@ namespace libcron { public: - bool add_schedule(std::string name, const std::string& schedule, std::function work); + bool add_schedule(std::string name, const std::string& schedule, Task::TaskFunction work); void clear_schedules(); void remove_schedule(const std::string& name); @@ -141,7 +141,7 @@ namespace libcron }; template - bool Cron::add_schedule(std::string name, const std::string& schedule, std::function work) + bool Cron::add_schedule(std::string name, const std::string& schedule, Task::TaskFunction work) { auto cron = CronData::create(schedule); bool res = cron.is_valid(); diff --git a/libcron/include/libcron/Task.h b/libcron/include/libcron/Task.h index 72fcda8..7049d65 100644 --- a/libcron/include/libcron/Task.h +++ b/libcron/include/libcron/Task.h @@ -11,16 +11,18 @@ namespace libcron class Task { public: + using TaskFunction = std::function; - Task(std::string name, const CronSchedule schedule, std::function task) + Task(std::string name, CronSchedule schedule, TaskFunction task) : name(std::move(name)), schedule(std::move(schedule)), task(std::move(task)) { } void execute(std::chrono::system_clock::time_point now) { + auto delay = now - last_run; last_run = now; - task(); + task(delay); } Task(const Task& other) = default; @@ -50,7 +52,7 @@ namespace libcron std::string name; CronSchedule schedule; std::chrono::system_clock::time_point next_schedule; - std::function task; + TaskFunction task; bool valid = false; std::chrono::system_clock::time_point last_run = std::numeric_limits::min(); }; diff --git a/test/CronRandomizationTest.cpp b/test/CronRandomizationTest.cpp index 0ecef3c..1f80b79 100644 --- a/test/CronRandomizationTest.cpp +++ b/test/CronRandomizationTest.cpp @@ -23,13 +23,13 @@ void test(const char* const random_schedule, bool expect_failure = false) if(expect_failure) { // 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); } else { REQUIRE(std::get<0>(res)); - REQUIRE(cron.add_schedule("validate schedule", schedule, []() {})); + REQUIRE(cron.add_schedule("validate schedule", schedule, [](auto) {})); } } diff --git a/test/CronTest.cpp b/test/CronTest.cpp index aa66ad0..a9c275c 100644 --- a/test/CronTest.cpp +++ b/test/CronTest.cpp @@ -35,7 +35,7 @@ SCENARIO("Adding a task") WHEN("Adding a task that runs every second") { REQUIRE(c.add_schedule("A task", "* * * * * ?", - [&expired]() + [&expired](auto) { expired = true; }) @@ -68,7 +68,7 @@ SCENARIO("Adding a task that expires in the future") Cron<> c; REQUIRE(c.add_schedule("A task", create_schedule_expiring_in(c.get_clock().now(), hours{0}, minutes{0}, seconds{3}), - [&expired]() + [&expired](auto) { expired = true; }) @@ -110,7 +110,7 @@ SCENARIO("Task priority") Cron<> c; REQUIRE(c.add_schedule("Five", create_schedule_expiring_in(c.get_clock().now(), hours{0}, minutes{0}, seconds{5}), - [&_5_second_expired]() + [&_5_second_expired](auto) { _5_second_expired++; }) @@ -118,7 +118,7 @@ SCENARIO("Task priority") REQUIRE(c.add_schedule("Three", create_schedule_expiring_in(c.get_clock().now(), hours{0}, minutes{0}, seconds{3}), - [&_3_second_expired]() + [&_3_second_expired](auto) { _3_second_expired++; }) @@ -231,7 +231,7 @@ SCENARIO("Clock changes") clock.set(sys_days{2018_y / 05 / 05}); // 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; // 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++; }) @@ -333,7 +333,25 @@ SCENARIO("Multiple ticks per second") } } +} +SCENARIO("Delayed run") +{ + Cron 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") @@ -346,35 +364,35 @@ SCENARIO("Tasks can be added and removed from the scheduler") WHEN("Adding 5 tasks that runs every second") { REQUIRE(c.add_schedule("Task-1", "* * * * * ?", - [&expired]() + [&expired](auto) { expired = true; }) ); REQUIRE(c.add_schedule("Task-2", "* * * * * ?", - [&expired]() + [&expired](auto) { expired = true; }) ); REQUIRE(c.add_schedule("Task-3", "* * * * * ?", - [&expired]() + [&expired](auto) { expired = true; }) ); REQUIRE(c.add_schedule("Task-4", "* * * * * ?", - [&expired]() + [&expired](auto) { expired = true; }) ); REQUIRE(c.add_schedule("Task-5", "* * * * * ?", - [&expired]() + [&expired](auto) { expired = true; })