Skip to content

Commit 700867a

Browse files
committed
more changes
1 parent 2b5e112 commit 700867a

14 files changed

Lines changed: 189 additions & 199 deletions

File tree

include/oryx/chron/chrono_types.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include <chrono>
4+
5+
namespace oryx::chron {
6+
7+
using Clock = std::chrono::system_clock;
8+
using TimePoint = std::chrono::time_point<Clock>;
9+
using Duration = Clock::duration;
10+
11+
} // namespace oryx::chron

include/oryx/chron/clock.hpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,35 @@
55
#include <chrono>
66

77
#include "traits.hpp"
8+
#include "chrono_types.h"
89

910
namespace oryx::chron {
1011

1112
class UTCClock {
1213
public:
13-
auto Now() const -> std::chrono::system_clock::time_point { return std::chrono::system_clock::now(); }
14-
auto UtcOffset(std::chrono::system_clock::time_point) const -> std::chrono::seconds {
15-
using namespace std::chrono;
16-
return 0s;
17-
}
14+
auto Now() const -> TimePoint { return Clock::now(); }
15+
auto UtcOffset(TimePoint) const -> std::chrono::seconds { return std::chrono::seconds(0); }
1816
};
1917

2018
class LocalClock {
2119
public:
22-
auto Now() const -> std::chrono::system_clock::time_point {
23-
auto now = std::chrono::system_clock::now();
20+
auto Now() const -> TimePoint {
21+
auto now = Clock::now();
2422
return now + UtcOffset(now);
2523
}
2624

27-
auto UtcOffset(std::chrono::system_clock::time_point now) const -> std::chrono::seconds;
25+
auto UtcOffset(TimePoint now) const -> std::chrono::seconds;
2826
};
2927

3028
class TzClock {
3129
public:
32-
auto Now() const -> std::chrono::system_clock::time_point {
33-
auto now = std::chrono::system_clock::now();
30+
auto Now() const -> TimePoint {
31+
auto now = Clock::now();
3432
return now + UtcOffset(now);
3533
}
3634

3735
auto TrySetTimezone(std::string_view name) -> bool;
38-
auto UtcOffset(std::chrono::system_clock::time_point now) const -> std::chrono::seconds;
36+
auto UtcOffset(TimePoint now) const -> std::chrono::seconds;
3937

4038
private:
4139
mutable std::mutex mtx_{};

include/oryx/chron/data.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <cstdint>
4+
#include <string>
35
#include <set>
46
#include <regex>
57
#include <string>
Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
#pragma once
22

33
#include <string>
4-
#include <set>
54
#include <tuple>
6-
#include <regex>
75
#include <random>
8-
#include <utility>
9-
10-
#include "data.hpp"
116

127
namespace oryx::chron {
138
class Randomization {
@@ -21,68 +16,8 @@ class Randomization {
2116
auto Parse(const std::string& cron_schedule) -> std::tuple<bool, std::string>;
2217

2318
private:
24-
template <typename T>
25-
auto GetRandomInRange(const std::string& section,
26-
int& selected_value,
27-
std::pair<int, int> limit = std::make_pair(-1, -1)) -> std::pair<bool, std::string>;
28-
29-
auto DayLimiter(const std::set<Months>& month) -> std::pair<int, int>;
30-
auto Cap(int value, int lower, int upper) -> int;
31-
32-
// Members
33-
const std::regex rand_expression_{R"#([rR]\((\d+)\-(\d+)\))#", std::regex_constants::ECMAScript};
3419
std::random_device random_device_;
3520
std::mt19937 twister_;
3621
};
3722

38-
template <typename T>
39-
auto Randomization::GetRandomInRange(const std::string& section, int& selected_value, std::pair<int, int> limit)
40-
-> std::pair<bool, std::string> {
41-
auto result = std::make_pair(true, std::string{});
42-
selected_value = -1;
43-
44-
std::smatch random_match;
45-
46-
if (std::regex_match(section.cbegin(), section.cend(), random_match, rand_expression_)) {
47-
// Random range, parse left and right numbers
48-
auto left = std::stoi(random_match[1].str());
49-
auto right = std::stoi(random_match[2].str());
50-
51-
// Apply limit if provided
52-
if (limit.first != -1 && limit.second != -1) {
53-
left = Cap(left, limit.first, limit.second);
54-
right = Cap(right, limit.first, limit.second);
55-
}
56-
57-
Data cron_data;
58-
std::set<T> numbers;
59-
result.first = cron_data.ConvertFromStringRangeToNumberRange<T>(
60-
std::to_string(left) + "-" + std::to_string(right), numbers);
61-
62-
// Remove items outside the limit
63-
if (limit.first != -1 && limit.second != -1) {
64-
for (auto it = numbers.begin(); it != numbers.end();) {
65-
if (Data::ValueOf(*it) < limit.first || Data::ValueOf(*it) > limit.second) {
66-
it = numbers.erase(it);
67-
} else {
68-
++it;
69-
}
70-
}
71-
}
72-
73-
if (result.first && !numbers.empty()) {
74-
// Select a random value from the valid numbers
75-
std::uniform_int_distribution<> distribution(0, static_cast<int>(numbers.size() - 1));
76-
auto it = numbers.begin();
77-
std::advance(it, distribution(twister_));
78-
selected_value = Data::ValueOf(*it);
79-
result.second = std::to_string(selected_value);
80-
}
81-
} else {
82-
// Not a random section, return as-is
83-
result.second = section;
84-
}
85-
86-
return result;
87-
}
8823
} // namespace oryx::chron

include/oryx/chron/schedule.hpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
#pragma once
22

3-
#include <chrono>
3+
#include <tuple>
44

55
#include "data.hpp"
66
#include "date_time.hpp"
7+
#include "chrono_types.h"
78

89
namespace oryx::chron {
910
class Schedule {
1011
public:
1112
explicit Schedule(Data data)
1213
: data_(std::move(data)) {}
1314

14-
Schedule(const Schedule&) = default;
15+
auto CalculateFrom(const TimePoint& from) const -> std::tuple<bool, TimePoint>;
1516

16-
auto operator=(const Schedule&) -> Schedule& = default;
17-
18-
auto CalculateFrom(const std::chrono::system_clock::time_point& from) const
19-
-> std::tuple<bool, std::chrono::system_clock::time_point>;
20-
21-
static auto ToCalendarTime(std::chrono::system_clock::time_point time) -> DateTime;
17+
static auto ToCalendarTime(TimePoint time) -> DateTime;
2218

2319
private:
2420
Data data_;

include/oryx/chron/scheduler.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "traits.hpp"
1212
#include "task.hpp"
1313
#include "clock.hpp"
14+
#include "chrono_types.h"
1415

1516
namespace oryx::chron {
1617
namespace details {
@@ -27,7 +28,7 @@ template <traits::Clock ClockType = LocalClock, traits::BasicLockable LockType =
2728
class Scheduler {
2829
public:
2930
// Schedule management
30-
auto AddSchedule(std::string name, const std::string& schedule, Task::TaskFunction work) -> bool {
31+
auto AddSchedule(std::string name, const std::string& schedule, Task::TaskFn work) -> bool {
3132
auto data = Data::Create(schedule);
3233
if (!data.IsValid()) {
3334
return false;
@@ -45,7 +46,7 @@ class Scheduler {
4546
}
4647

4748
template <typename Schedules = std::map<std::string, std::string>>
48-
auto AddSchedule(const Schedules& name_schedule_map, Task::TaskFunction work)
49+
auto AddSchedule(const Schedules& name_schedule_map, Task::TaskFn work)
4950
-> std::tuple<bool, std::string, std::string> {
5051
bool is_valid{true};
5152
std::tuple<bool, std::string, std::string> result{false, "", ""};
@@ -97,7 +98,7 @@ class Scheduler {
9798
for (auto& task : tasks_) task.CalculateNext(clock_.Now() + std::chrono::seconds(1));
9899
}
99100

100-
auto Tick(std::chrono::system_clock::time_point now) -> size_t {
101+
auto Tick(TimePoint now) -> size_t {
101102
std::lock_guard lock{tasks_mtx_};
102103

103104
size_t executed_count{};
@@ -142,7 +143,7 @@ class Scheduler {
142143

143144
auto Tick() -> size_t { return Tick(clock_.Now()); }
144145

145-
auto TimeUntilNext() const -> std::chrono::system_clock::duration {
146+
auto TimeUntilNext() const -> Duration {
146147
std::lock_guard lock{tasks_mtx_};
147148
if (tasks_.empty()) {
148149
return std::chrono::minutes::max();
@@ -153,8 +154,7 @@ class Scheduler {
153154
auto GetClock() -> ClockType& { return clock_; }
154155
auto GetNumTasks() const -> size_t { return tasks_.size(); }
155156

156-
void GetTimeUntilExpiryForTasks(
157-
std::vector<std::tuple<std::string, std::chrono::system_clock::duration>>& status) const {
157+
void GetTimeUntilExpiryForTasks(std::vector<std::tuple<std::string, Duration>>& status) const {
158158
std::lock_guard lock{tasks_mtx_};
159159
auto now = clock_.Now();
160160
status.clear();
@@ -170,7 +170,7 @@ class Scheduler {
170170
std::vector<Task> tasks_;
171171
mutable std::mutex tasks_mtx_;
172172
ClockType clock_;
173-
std::chrono::system_clock::time_point last_tick_;
173+
TimePoint last_tick_;
174174
bool first_tick_{true};
175175
};
176176

include/oryx/chron/task.hpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
#pragma once
22

33
#include <functional>
4-
#include <chrono>
4+
#include <string>
55

66
#include "schedule.hpp"
7+
#include "chrono_types.h"
78

89
namespace oryx::chron {
910

1011
class TaskInformation {
1112
public:
1213
virtual ~TaskInformation() = default;
13-
virtual auto GetDelay() const -> std::chrono::system_clock::duration = 0;
14+
virtual auto GetDelay() const -> Duration = 0;
1415
virtual auto GetName() const -> std::string_view = 0;
1516
};
1617

1718
class Task : public TaskInformation {
1819
public:
19-
using TaskFunction = std::function<void(const TaskInformation &)>;
20+
using TaskFn = std::function<void(const TaskInformation &)>;
2021

21-
Task(std::string name, Schedule schedule, TaskFunction task);
22-
Task(const Task &other) = default;
22+
Task(std::string name, Schedule schedule, TaskFn task);
2323

2424
auto operator=(const Task &) -> Task & = default;
2525
auto operator>(const Task &other) const -> bool { return next_schedule_ > other.next_schedule_; }
2626
auto operator<(const Task &other) const -> bool { return next_schedule_ < other.next_schedule_; }
2727

28-
void Execute(std::chrono::system_clock::time_point now);
29-
auto CalculateNext(std::chrono::system_clock::time_point from) -> bool;
30-
auto TimeUntilExpiry(std::chrono::system_clock::time_point now) const -> std::chrono::system_clock::duration;
28+
void Execute(TimePoint now);
29+
auto CalculateNext(TimePoint from) -> bool;
30+
auto TimeUntilExpiry(TimePoint now) const -> Duration;
3131

32-
auto IsExpired(std::chrono::system_clock::time_point now) const -> bool;
32+
auto IsExpired(TimePoint now) const -> bool;
3333
auto GetName() const -> std::string_view override { return name_; }
34-
auto GetDelay() const -> std::chrono::system_clock::duration override { return delay_; }
35-
auto GetStatus(std::chrono::system_clock::time_point now) const -> std::string;
34+
auto GetDelay() const -> Duration override { return delay_; }
35+
auto GetStatus(TimePoint now) const -> std::string;
3636

3737
private:
3838
std::string name_;
3939
Schedule schedule_;
40-
TaskFunction task_;
41-
std::chrono::system_clock::time_point next_schedule_;
42-
std::chrono::system_clock::duration delay_;
43-
std::chrono::system_clock::time_point last_run_;
40+
TaskFn task_;
41+
TimePoint next_schedule_;
42+
Duration delay_;
43+
TimePoint last_run_;
4444
bool valid_;
4545
};
4646

include/oryx/chron/traits.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
#include <chrono>
44
#include <concepts>
55

6+
#include "chrono_types.h"
7+
68
namespace oryx::chron::traits {
79

810
template <typename T>
9-
concept Clock = requires(const T& t, std::chrono::system_clock::time_point tp) {
11+
concept Clock = requires(const T& t, TimePoint tp) {
1012
// Must provide: Now() -> std::chrono::system_clock::time_point
11-
{ t.Now() } -> std::same_as<std::chrono::system_clock::time_point>;
13+
{ t.Now() } -> std::same_as<TimePoint>;
1214

1315
// Must provide: UtcOffset(time_point) -> std::chrono::seconds
1416
{ t.UtcOffset(tp) } -> std::same_as<std::chrono::seconds>;

src/clock.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using namespace std::chrono;
1313

1414
namespace oryx::chron {
1515

16-
auto LocalClock::UtcOffset(system_clock::time_point now) const -> seconds {
16+
auto LocalClock::UtcOffset(TimePoint now) const -> seconds {
1717
#ifdef WIN32
1818
(void)now;
1919

@@ -51,7 +51,7 @@ auto TzClock::TrySetTimezone(std::string_view name) -> bool {
5151
return true;
5252
}
5353

54-
auto TzClock::UtcOffset(system_clock::time_point now) const -> seconds {
54+
auto TzClock::UtcOffset(TimePoint now) const -> seconds {
5555
// If we don't have a timezone set we use utc
5656
std::lock_guard lock(mtx_);
5757
if (timezone_)

0 commit comments

Comments
 (0)