Skip to content

Commit a85fa0d

Browse files
committed
[ISSUE #1254]🚀RocketMQRuntime add schedule_at_fixed_rate_mut method
1 parent e9dcd9e commit a85fa0d

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

rocketmq-runtime/src/lib.rs

+34
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,38 @@ impl RocketMQRuntime {
9292
}
9393
}
9494
}
95+
96+
pub fn schedule_at_fixed_rate_mut<F>(
97+
&self,
98+
mut task: F,
99+
initial_delay: Option<Duration>,
100+
period: Duration,
101+
) where
102+
F: FnMut() + Send + 'static,
103+
{
104+
match self {
105+
RocketMQRuntime::Multi(runtime) => {
106+
runtime.handle().spawn(async move {
107+
// initial delay
108+
if let Some(initial_delay_inner) = initial_delay {
109+
tokio::time::sleep(initial_delay_inner).await;
110+
}
111+
112+
loop {
113+
// record current execution time
114+
let current_execution_time = tokio::time::Instant::now();
115+
// execute task
116+
task();
117+
// Calculate the time of the next execution
118+
let next_execution_time = current_execution_time + period;
119+
120+
// Wait until the next execution
121+
let delay = next_execution_time
122+
.saturating_duration_since(tokio::time::Instant::now());
123+
tokio::time::sleep(delay).await;
124+
}
125+
});
126+
}
127+
}
128+
}
95129
}

0 commit comments

Comments
 (0)