2020-07-27 18:54:10 -05:00
|
|
|
use actix_rt::Arbiter;
|
|
|
|
|
use futures::StreamExt;
|
|
|
|
|
|
|
|
|
|
pub struct Scheduler {
|
|
|
|
|
arbiter: Arbiter,
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 15:55:01 -07:00
|
|
|
impl Default for Scheduler {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::new()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 18:54:10 -05:00
|
|
|
impl Scheduler {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Scheduler {
|
|
|
|
|
arbiter: Arbiter::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 20:18:23 -05:00
|
|
|
pub fn run<F, R>(&mut self, interval: std::time::Duration, mut task: F)
|
2024-04-22 13:09:35 -07:00
|
|
|
where
|
|
|
|
|
F: FnMut() -> R + Send + 'static,
|
|
|
|
|
R: std::future::Future<Output = ()> + Send + 'static,
|
2020-07-27 18:54:10 -05:00
|
|
|
{
|
2022-03-29 19:35:09 -07:00
|
|
|
let future = IntervalStream::new(actix_rt::time::interval(interval))
|
2022-02-27 21:44:00 -07:00
|
|
|
.for_each_concurrent(2, move |_| task());
|
2022-02-05 23:08:30 -07:00
|
|
|
|
|
|
|
|
self.arbiter.spawn(future);
|
2020-07-27 18:54:10 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for Scheduler {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
self.arbiter.stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-28 11:11:49 -05:00
|
|
|
|
2022-02-05 23:08:30 -07:00
|
|
|
use tokio_stream::wrappers::IntervalStream;
|