File tree 1 file changed +34
-0
lines changed
1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -92,4 +92,38 @@ impl RocketMQRuntime {
92
92
}
93
93
}
94
94
}
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
+ }
95
129
}
You can’t perform that action at this time.
0 commit comments