Refactored polling to be more accurate on scheduling tasks
This commit is contained in:
parent
fc573e165b
commit
1b4c1db44d
@ -13,6 +13,7 @@
|
|||||||
### Changes from v2.0.3-rc
|
### Changes from v2.0.3-rc
|
||||||
|
|
||||||
* Continue documentation updates
|
* Continue documentation updates
|
||||||
|
* Refactored polling to be more accurate on scheduling tasks
|
||||||
|
|
||||||
## v2.0.3-rc
|
## v2.0.3-rc
|
||||||
|
|
||||||
|
@ -38,47 +38,57 @@ void polling::frequency_thread(
|
|||||||
std::function<std::uint32_t()> get_frequency_seconds, frequency freq) {
|
std::function<std::uint32_t()> get_frequency_seconds, frequency freq) {
|
||||||
REPERTORY_USES_FUNCTION_NAME();
|
REPERTORY_USES_FUNCTION_NAME();
|
||||||
|
|
||||||
|
auto last_run = std::chrono::system_clock::time_point::min();
|
||||||
while (not get_stop_requested()) {
|
while (not get_stop_requested()) {
|
||||||
unique_mutex_lock lock(mutex_);
|
auto elapsed = std::chrono::system_clock::now() - last_run;
|
||||||
auto futures = std::accumulate(
|
auto max_elapsed = std::chrono::seconds(get_frequency_seconds());
|
||||||
items_.begin(), items_.end(), std::deque<tasks::task_ptr>{},
|
|
||||||
[this, &freq](auto &&list, auto &&item) -> auto {
|
if (std::chrono::duration_cast<std::chrono::seconds>(elapsed) >=
|
||||||
if (item.second.freq != freq) {
|
max_elapsed) {
|
||||||
|
unique_mutex_lock lock(mutex_);
|
||||||
|
auto futures = std::accumulate(
|
||||||
|
items_.begin(), items_.end(), std::deque<tasks::task_ptr>{},
|
||||||
|
[this, &freq](auto &&list, auto &&item) -> auto {
|
||||||
|
if (item.second.freq != freq) {
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto future = tasks::instance().schedule({
|
||||||
|
[this, &freq, item](auto &&task_stopped) {
|
||||||
|
if (config_->get_event_level() == event_level::trace ||
|
||||||
|
freq != frequency::second) {
|
||||||
|
event_system::instance().raise<polling_item_begin>(
|
||||||
|
function_name, item.first);
|
||||||
|
}
|
||||||
|
item.second.action(task_stopped);
|
||||||
|
if (config_->get_event_level() == event_level::trace ||
|
||||||
|
freq != frequency::second) {
|
||||||
|
event_system::instance().raise<polling_item_end>(
|
||||||
|
function_name, item.first);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
list.emplace_back(future);
|
||||||
return list;
|
return list;
|
||||||
}
|
|
||||||
|
|
||||||
auto future = tasks::instance().schedule({
|
|
||||||
[this, &freq, item](auto &&task_stopped) {
|
|
||||||
if (config_->get_event_level() == event_level::trace ||
|
|
||||||
freq != frequency::second) {
|
|
||||||
event_system::instance().raise<polling_item_begin>(
|
|
||||||
function_name, item.first);
|
|
||||||
}
|
|
||||||
item.second.action(task_stopped);
|
|
||||||
if (config_->get_event_level() == event_level::trace ||
|
|
||||||
freq != frequency::second) {
|
|
||||||
event_system::instance().raise<polling_item_end>(
|
|
||||||
function_name, item.first);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
lock.unlock();
|
||||||
|
|
||||||
list.emplace_back(future);
|
while (not futures.empty()) {
|
||||||
return list;
|
futures.front()->wait();
|
||||||
});
|
futures.pop_front();
|
||||||
lock.unlock();
|
}
|
||||||
|
|
||||||
while (not futures.empty()) {
|
last_run = std::chrono::system_clock::now();
|
||||||
futures.front()->wait();
|
elapsed = std::chrono::seconds(0U);
|
||||||
futures.pop_front();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unique_mutex_lock lock(mutex_);
|
||||||
if (get_stop_requested()) {
|
if (get_stop_requested()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
lock.lock();
|
notify_.wait_for(lock, max_elapsed - elapsed);
|
||||||
notify_.wait_for(lock, std::chrono::seconds(get_frequency_seconds()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user