Files
AstralRinth/apps/labrinth/src/scheduler.rs
Josiah Glosson c998d2566e Allow multiple labrinth instances (#3360)
* Move a lot of scheduled tasks to be runnable from the command-line

* Use pubsub to handle sockets connected to multiple Labrinths

* Clippy fix

* Fix build and merge some stuff

* Fix build fmt
:

---------

Signed-off-by: Jai Agrawal <18202329+Geometrically@users.noreply.github.com>
Co-authored-by: Jai A <jaiagr+gpg@pm.me>
Co-authored-by: Jai Agrawal <18202329+Geometrically@users.noreply.github.com>
2025-03-15 07:28:20 -07:00

40 lines
805 B
Rust

use actix_rt::Arbiter;
use futures::StreamExt;
pub struct Scheduler {
arbiter: Arbiter,
}
impl Default for Scheduler {
fn default() -> Self {
Self::new()
}
}
impl Scheduler {
pub fn new() -> Self {
Scheduler {
arbiter: Arbiter::new(),
}
}
pub fn run<F, R>(&mut self, interval: std::time::Duration, mut task: F)
where
F: FnMut() -> R + Send + 'static,
R: std::future::Future<Output = ()> + Send + 'static,
{
let future = IntervalStream::new(actix_rt::time::interval(interval))
.for_each_concurrent(2, move |_| task());
self.arbiter.spawn(future);
}
}
impl Drop for Scheduler {
fn drop(&mut self) {
self.arbiter.stop();
}
}
use tokio_stream::wrappers::IntervalStream;