mirror of
https://github.com/PerMalmberg/libcron.git
synced 2025-04-22 00:13:01 -05:00
Implement delayed_by parameter to task callback.
This commit is contained in:
parent
d1ac26bd94
commit
a51aae6c65
@ -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<libcron::LocalClock, libcron::Locker>
|
||||
|
||||
CronMt cron;
|
||||
cron.add_schedule("Hello from Cron", "* * * * * ?", [=]() {
|
||||
cron.add_schedule("Hello from Cron", "* * * * * ?", []() {
|
||||
std::cout << "Hello from CronMt!" std::endl;
|
||||
});
|
||||
|
||||
|
@ -39,7 +39,7 @@ namespace libcron
|
||||
{
|
||||
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 remove_schedule(const std::string& name);
|
||||
|
||||
@ -141,7 +141,7 @@ namespace libcron
|
||||
};
|
||||
|
||||
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);
|
||||
bool res = cron.is_valid();
|
||||
|
@ -11,16 +11,18 @@ namespace libcron
|
||||
class Task
|
||||
{
|
||||
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))
|
||||
{
|
||||
}
|
||||
|
||||
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<void()> task;
|
||||
TaskFunction task;
|
||||
bool valid = false;
|
||||
std::chrono::system_clock::time_point last_run = std::numeric_limits<std::chrono::system_clock::time_point>::min();
|
||||
};
|
||||
|
@ -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) {}));
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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<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")
|
||||
@ -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;
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user