Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/timer/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ pub struct TaskContext {
pub runtime_kind: RuntimeKind,
/// Event Sender for Timer Wheel Core.
pub(crate) timer_event_sender: Option<TimerEventSender>,
pub(crate) could_send_finish_event: Option<AsyncReceiver<()>>,
}

impl TaskContext {
Expand All @@ -496,6 +497,15 @@ impl TaskContext {
self
}

#[inline(always)]
pub(crate) fn could_send_finish_event(
&mut self,
could_send_finish_event: AsyncReceiver<()>,
) -> &mut Self {
self.could_send_finish_event = Some(could_send_finish_event);
self
}

#[inline(always)]
/// Get hook functions that may be used in the future.
pub fn then_fn(&mut self, then_fn: fn()) -> &mut Self {
Expand All @@ -512,6 +522,10 @@ impl TaskContext {
/// Send a task-Finish signal to EventHandle.
pub async fn finish_task(self, finish_output: Option<FinishOutput>) {
if let Some(timer_event_sender) = self.timer_event_sender {
// Wait for the TimerEvent::AppendTaskHandle event to be sent.
if let Some(ref wait) = self.could_send_finish_event {
let _ = wait.recv().await;
}
timer_event_sender
.send(TimerEvent::FinishTask(FinishTaskBody {
task_id: self.task_id,
Expand Down
5 changes: 5 additions & 0 deletions src/timer/timer_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,14 @@ impl Timer {
}
}

let (s, r) = channel::bounded::<()>(1);

let mut task_context = TaskContext::default();
task_context
.task_id(task_id)
.record_id(record_id)
.timer_event_sender(self.timer_event_sender.clone())
.could_send_finish_event(r)
.runtime_kind(self.shared_header.runtime_instance.kind);

let task_handler_box = self.routine_exec(&*(task.routine.0), task_context);
Expand All @@ -341,6 +344,8 @@ impl Timer {
.spawn(task_handler_box);

self.send_timer_event(task_id, tmp_task_handler_box).await;
// The TimerEvent::AppendTaskHandle event should always be sent before The TimerEvent::FinishTask event.
let _ = s.try_send(());

let task_valid = task.down_count_and_set_vaild();
if !task_valid {
Expand Down