mirror of
https://github.com/PerMalmberg/libcron.git
synced 2025-07-03 17:32:57 -05:00
Compare commits
2 Commits
v1.3.0
...
feature/al
Author | SHA1 | Date | |
---|---|---|---|
a51aae6c65 | |||
d1ac26bd94 |
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", "* * * * * ?", [=](auto&) {
|
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,30 +23,6 @@ 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:
|
||||||
@ -56,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:
|
||||||
@ -74,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;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -31,4 +31,4 @@ target_include_directories(${PROJECT_NAME}
|
|||||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out/${CMAKE_BUILD_TYPE}"
|
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out/${CMAKE_BUILD_TYPE}"
|
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out/${CMAKE_BUILD_TYPE}")
|
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out/${CMAKE_BUILD_TYPE}")
|
@ -20,10 +20,12 @@ namespace libcron
|
|||||||
class Locker
|
class Locker
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void lock() { m.lock(); }
|
Locker() : lck(m, std::defer_lock) {}
|
||||||
void unlock() { m.unlock(); }
|
void lock() { lck.lock(); }
|
||||||
|
void unlock() { lck.unlock(); }
|
||||||
private:
|
private:
|
||||||
std::recursive_mutex m{};
|
std::mutex m{};
|
||||||
|
std::unique_lock<std::mutex> lck;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename ClockType, typename LockType>
|
template<typename ClockType, typename LockType>
|
||||||
@ -36,6 +38,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, Task::TaskFunction work);
|
||||||
void clear_schedules();
|
void clear_schedules();
|
||||||
void remove_schedule(const std::string& name);
|
void remove_schedule(const std::string& name);
|
||||||
@ -136,7 +139,7 @@ namespace libcron
|
|||||||
bool first_tick = true;
|
bool first_tick = true;
|
||||||
std::chrono::system_clock::time_point last_tick{};
|
std::chrono::system_clock::time_point last_tick{};
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename ClockType, typename LockType>
|
template<typename ClockType, typename LockType>
|
||||||
bool Cron<ClockType, LockType>::add_schedule(std::string name, const std::string& schedule, Task::TaskFunction work)
|
bool Cron<ClockType, LockType>::add_schedule(std::string name, const std::string& schedule, Task::TaskFunction work)
|
||||||
{
|
{
|
||||||
@ -145,7 +148,7 @@ namespace libcron
|
|||||||
if (res)
|
if (res)
|
||||||
{
|
{
|
||||||
tasks.lock_queue();
|
tasks.lock_queue();
|
||||||
Task t{std::move(name), CronSchedule{cron}, work };
|
Task t{std::move(name), CronSchedule{cron}, std::move(work)};
|
||||||
if (t.calculate_next(clock.now()))
|
if (t.calculate_next(clock.now()))
|
||||||
{
|
{
|
||||||
tasks.push(t);
|
tasks.push(t);
|
||||||
|
@ -39,4 +39,4 @@ namespace libcron
|
|||||||
|
|
||||||
std::chrono::seconds utc_offset(std::chrono::system_clock::time_point now) const override;
|
std::chrono::seconds utc_offset(std::chrono::system_clock::time_point now) const override;
|
||||||
};
|
};
|
||||||
}
|
}
|
@ -1,42 +1,28 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <chrono>
|
|
||||||
#include <utility>
|
|
||||||
#include "CronData.h"
|
#include "CronData.h"
|
||||||
#include "CronSchedule.h"
|
#include "CronSchedule.h"
|
||||||
|
#include <chrono>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
namespace libcron
|
namespace libcron
|
||||||
{
|
{
|
||||||
class TaskInformation
|
class Task
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~TaskInformation() = default;
|
using TaskFunction = std::function<void(std::chrono::system_clock::duration)>;
|
||||||
virtual std::chrono::system_clock::duration get_delay() const = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Task : public TaskInformation
|
Task(std::string name, CronSchedule schedule, TaskFunction task)
|
||||||
{
|
|
||||||
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)
|
auto delay = now - last_run;
|
||||||
delay = now - next_schedule;
|
|
||||||
|
|
||||||
last_run = now;
|
last_run = now;
|
||||||
task(*this);
|
task(delay);
|
||||||
}
|
|
||||||
|
|
||||||
std::chrono::system_clock::duration get_delay() const override
|
|
||||||
{
|
|
||||||
return delay;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Task(const Task& other) = default;
|
Task(const Task& other) = default;
|
||||||
@ -66,7 +52,6 @@ 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,3 +1,4 @@
|
|||||||
|
#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,50 +99,6 @@ 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")
|
||||||
@ -154,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](auto&)
|
[&_5_second_expired](auto)
|
||||||
{
|
{
|
||||||
_5_second_expired++;
|
_5_second_expired++;
|
||||||
})
|
})
|
||||||
@ -162,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](auto&)
|
[&_3_second_expired](auto)
|
||||||
{
|
{
|
||||||
_3_second_expired++;
|
_3_second_expired++;
|
||||||
})
|
})
|
||||||
@ -275,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 * * * ?", [](auto&)
|
REQUIRE(c.add_schedule("Clock change task", "0 0 * * * ?", [](auto)
|
||||||
{
|
{
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@ -353,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](auto&)
|
REQUIRE(c.add_schedule("Clock change task", "*/10 0 * * * ?", [&run_count](auto)
|
||||||
{
|
{
|
||||||
run_count++;
|
run_count++;
|
||||||
})
|
})
|
||||||
@ -377,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")
|
||||||
@ -390,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](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