Skip to content

Commit

Permalink
feat(Core/Scripting): Implement TaskScheduler GetNextGroupOccurrence() (
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyeriah authored Nov 24, 2024
1 parent 735a5a5 commit ab7f49b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/common/Utilities/TaskScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ bool TaskScheduler::IsGroupScheduled(group_t const group)
return _task_holder.IsGroupQueued(group);
}

Milliseconds TaskScheduler::GetNextGroupOcurrence(group_t const group) const
{
return std::chrono::duration_cast<std::chrono::milliseconds>(_task_holder.GetNextGroupOcurrence(group) - clock_t::now());
}

void TaskScheduler::TaskQueue::Push(TaskContainer&& task)
{
container.insert(task);
Expand Down Expand Up @@ -189,6 +194,18 @@ bool TaskScheduler::TaskQueue::IsGroupQueued(group_t const group)
return false;
}

TaskScheduler::timepoint_t TaskScheduler::TaskQueue::GetNextGroupOcurrence(group_t const group) const
{
TaskScheduler::timepoint_t next = TaskScheduler::timepoint_t::max();
for (auto const& task : container)
{
if (task->IsInGroup(group) && task->_end < next)
next = task->_end;
}

return next;
}

bool TaskScheduler::TaskQueue::IsEmpty() const
{
return container.empty();
Expand Down Expand Up @@ -231,6 +248,11 @@ TaskScheduler::repeated_t TaskContext::GetRepeatCounter() const
return _task->_repeated;
}

TaskScheduler::timepoint_t TaskContext::GetNextOcurrence() const
{
return _task->_end;
}

TaskContext& TaskContext::Async(std::function<void()> const& callable)
{
return Dispatch(std::bind(&TaskScheduler::Async, std::placeholders::_1, callable));
Expand Down
8 changes: 8 additions & 0 deletions src/common/Utilities/TaskScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ class TaskScheduler
/// Check if the group exists and is currently scheduled.
bool IsGroupQueued(group_t const group);

// Returns the next group occurrence.
TaskScheduler::timepoint_t GetNextGroupOcurrence(group_t const group) const;

bool IsEmpty() const;
};

Expand Down Expand Up @@ -373,6 +376,9 @@ class TaskScheduler
return RescheduleGroup(group, RandomDurationBetween(min, max));
}

// Returns the next group occurrence.
Milliseconds GetNextGroupOcurrence(group_t const group) const;

private:
/// Insert a new task to the enqueued tasks.
TaskScheduler& InsertTask(TaskContainer task);
Expand Down Expand Up @@ -477,6 +483,8 @@ class TaskContext
/// Returns the repeat counter which increases every time the task is repeated.
TaskScheduler::repeated_t GetRepeatCounter() const;

TaskScheduler::timepoint_t GetNextOcurrence() const;

/// Repeats the event and sets a new duration.
/// std::chrono::seconds(5) for example.
/// This will consume the task context, its not possible to repeat the task again
Expand Down

0 comments on commit ab7f49b

Please sign in to comment.