mirror of
https://github.com/PerMalmberg/libcron.git
synced 2025-02-17 17:59:55 +08:00
Added the concept of a clock to Cron.
This commit is contained in:
parent
be2b4424a1
commit
74e1ad4d60
@ -17,4 +17,4 @@ add_library(${PROJECT_NAME}
|
||||
CronSchedule.h
|
||||
externals/date/date.h
|
||||
DateTime.h
|
||||
Task.cpp)
|
||||
Task.cpp CronClock.h)
|
||||
|
@ -13,7 +13,7 @@ namespace libcron
|
||||
{
|
||||
|
||||
Task t{std::move(name), CronSchedule{cron}, std::move(work)};
|
||||
if (t.calculate_next())
|
||||
if (t.calculate_next(clock->now()))
|
||||
{
|
||||
tasks.push(t);
|
||||
}
|
||||
@ -22,7 +22,7 @@ namespace libcron
|
||||
return res;
|
||||
}
|
||||
|
||||
std::chrono::system_clock::duration Cron::time_until_next(std::chrono::system_clock::time_point now) const
|
||||
std::chrono::system_clock::duration Cron::time_until_next() const
|
||||
{
|
||||
system_clock::duration d{};
|
||||
if (tasks.empty())
|
||||
@ -31,7 +31,7 @@ namespace libcron
|
||||
}
|
||||
else
|
||||
{
|
||||
d = tasks.top().time_until_expiry(now);
|
||||
d = tasks.top().time_until_expiry(clock->now());
|
||||
}
|
||||
|
||||
return d;
|
||||
|
@ -5,13 +5,19 @@
|
||||
#include <queue>
|
||||
#include <memory>
|
||||
#include "Task.h"
|
||||
#include "CronClock.h"
|
||||
|
||||
namespace libcron
|
||||
{
|
||||
class Cron
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
explicit Cron(std::unique_ptr<ICronClock> clock = std::make_unique<UTCClock>())
|
||||
: clock(std::move(clock))
|
||||
{
|
||||
}
|
||||
|
||||
bool add_schedule(std::string name, const std::string& schedule, std::function<void()> work);
|
||||
|
||||
size_t count() const
|
||||
@ -20,14 +26,23 @@ namespace libcron
|
||||
}
|
||||
|
||||
size_t
|
||||
execute_expired_tasks(std::chrono::system_clock::time_point now = std::chrono::system_clock::now());
|
||||
execute_expired_tasks()
|
||||
{
|
||||
return execute_expired_tasks(clock->now());
|
||||
}
|
||||
|
||||
size_t
|
||||
execute_expired_tasks(std::chrono::system_clock::time_point now);
|
||||
|
||||
std::chrono::system_clock::duration
|
||||
time_until_next(std::chrono::system_clock::time_point now = std::chrono::system_clock::now()) const;
|
||||
time_until_next() const;
|
||||
|
||||
std::shared_ptr<ICronClock> get_clock() const { return clock; }
|
||||
|
||||
private:
|
||||
// Priority queue placing smallest (i.e. nearest in time) items on top.
|
||||
std::priority_queue<Task, std::vector<Task>, std::greater<>> tasks{};
|
||||
void print_queue(std::priority_queue<Task, std::vector<Task>, std::greater<>> queue);
|
||||
std::shared_ptr<ICronClock> clock{};
|
||||
};
|
||||
}
|
22
libcron/CronClock.h
Normal file
22
libcron/CronClock.h
Normal file
@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace libcron
|
||||
{
|
||||
class ICronClock
|
||||
{
|
||||
public:
|
||||
virtual std::chrono::system_clock::time_point now() = 0;
|
||||
};
|
||||
|
||||
class UTCClock
|
||||
: public ICronClock
|
||||
{
|
||||
public:
|
||||
std::chrono::system_clock::time_point now() override
|
||||
{
|
||||
return std::chrono::system_clock::now();
|
||||
}
|
||||
};
|
||||
}
|
@ -80,6 +80,6 @@ namespace libcron
|
||||
}
|
||||
}
|
||||
|
||||
return std::make_tuple(max_iterations > 0, max_iterations > 0 ? curr : system_clock::now());
|
||||
return std::make_tuple(max_iterations > 0, curr);
|
||||
}
|
||||
}
|
@ -26,17 +26,17 @@ namespace libcron
|
||||
|
||||
Task& operator=(const Task&) = default;
|
||||
|
||||
bool calculate_next(std::chrono::system_clock::time_point from = std::chrono::system_clock::now());
|
||||
bool calculate_next(std::chrono::system_clock::time_point from);
|
||||
|
||||
bool operator>(const Task& other) const
|
||||
{
|
||||
return next_schedule > other.next_schedule;
|
||||
}
|
||||
|
||||
bool is_expired(std::chrono::system_clock::time_point now = std::chrono::system_clock::now()) const;
|
||||
bool is_expired(std::chrono::system_clock::time_point now) const;
|
||||
|
||||
std::chrono::system_clock::duration
|
||||
time_until_expiry(std::chrono::system_clock::time_point now = std::chrono::system_clock::now()) const;
|
||||
time_until_expiry(std::chrono::system_clock::time_point now) const;
|
||||
|
||||
std::string get_name() const
|
||||
{
|
||||
|
@ -44,7 +44,7 @@ SCENARIO("Adding a task")
|
||||
THEN("Count is 1 and task was not expired two seconds ago")
|
||||
{
|
||||
REQUIRE(c.count() == 1);
|
||||
c.execute_expired_tasks(system_clock::now() - 2s);
|
||||
c.execute_expired_tasks(c.get_clock()->now() - 2s);
|
||||
REQUIRE_FALSE(expired);
|
||||
}
|
||||
AND_THEN("Task is expired when calculating based on current time")
|
||||
|
Loading…
Reference in New Issue
Block a user