Skip to content

Commit

Permalink
Tasks: simplify sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
PBrunot committed Jun 28, 2024
1 parent 57221b2 commit 77a4ec7
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions src/Tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,17 @@ namespace fabomatic::Tasks

auto Scheduler::execute() const -> void
{
// Tasks shall be run in order of expiration (the most expired task shall run first)
std::vector<decltype(tasks)::const_iterator> iters;
for (auto it = tasks.begin(); it != tasks.end(); ++it)
{
iters.push_back(it); // Vector of iterators
}
// Need a copy to mutate the order
std::vector mutableTasks(tasks.begin(), tasks.end());

// Sort the iterators array as we cannot sort directly reference_wrappers
std::sort(iters.begin(), iters.end(),
[](const auto &it1, const auto &it2)
{
return (it1->get().getNextRun() < it2->get().getNextRun());
});
// Tasks shall be run in order of expiration (the most expired task shall run first)
std::sort(mutableTasks.begin(), mutableTasks.end(), [](const Task &a, const Task &b)
{ return a.getNextRun() < b.getNextRun(); });

// Now iterate over the sorted iterators to run the tasks
for (const auto &it : iters)
// Now iterate over the sorted tasks to run the tasks
for (const auto &it : mutableTasks)
{
it->get().run();
it.get().run();
}

if (conf::debug::ENABLE_TASK_LOGS && millis() % 1024 == 0)
Expand Down

0 comments on commit 77a4ec7

Please sign in to comment.