Rename scheduler methods to be consistent with tokio, more obvious

This commit is contained in:
Filip Tibell 2023-08-21 09:52:38 -05:00
parent dbf5c989c2
commit 888f00dd8b
6 changed files with 19 additions and 20 deletions

View file

@ -170,7 +170,7 @@ where
// Start up our web server // Start up our web server
// TODO: Spawn a scheduler background task here, // TODO: Spawn a scheduler background task here,
// and communicate using an mpsc channel instead // and communicate using an mpsc channel instead
sched.schedule_future_background_local(async move { sched.spawn_local(async move {
bound bound
.http1_only(true) // Web sockets can only use http1 .http1_only(true) // Web sockets can only use http1
.http1_keepalive(true) // Web sockets must be kept alive .http1_keepalive(true) // Web sockets must be kept alive

View file

@ -53,7 +53,7 @@ impl Service<Request<Body>> for NetServiceInner {
let sched = lua let sched = lua
.app_data_ref::<&Scheduler>() .app_data_ref::<&Scheduler>()
.expect("Lua struct is missing scheduler"); .expect("Lua struct is missing scheduler");
sched.schedule_future_background_local(async move { sched.spawn_local(async move {
// Create our new full websocket object, then // Create our new full websocket object, then
// schedule our handler to get called asap // schedule our handler to get called asap
let res = async move { let res = async move {

View file

@ -175,10 +175,8 @@ async fn process_spawn(
.app_data_ref::<&Scheduler>() .app_data_ref::<&Scheduler>()
.expect("Lua struct is missing scheduler"); .expect("Lua struct is missing scheduler");
let fut = spawn_command(program, args, options); let (status, stdout, stderr) = sched
let recv = sched.schedule_future_background(fut); .spawn(spawn_command(program, args, options))
let (status, stdout, stderr) = recv
.await .await
.expect("Failed to receive result of spawned process")?; .expect("Failed to receive result of spawned process")?;

View file

@ -105,7 +105,7 @@ where
.expect("Lua struct is missing scheduler"); .expect("Lua struct is missing scheduler");
let thread2 = thread.clone(); let thread2 = thread.clone();
sched.schedule_future_thread(thread.clone(), async move { sched.spawn_thread(thread.clone(), async move {
let duration = Duration::from_secs_f64(secs); let duration = Duration::from_secs_f64(secs);
time::sleep(duration).await; time::sleep(duration).await;
sched.push_back(thread2, args)?; sched.push_back(thread2, args)?;

View file

@ -33,9 +33,8 @@ where
/** /**
Schedules a plain future to run in the background. Schedules a plain future to run in the background.
This will spawn the future both on the scheduler and This will potentially spawn the future on a different thread, using
potentially on a different thread using [`task::spawn`], [`task::spawn`], meaning the provided future must implement [`Send`].
meaning the provided future must implement [`Send`].
Returns a [`Receiver`] which may be `await`-ed Returns a [`Receiver`] which may be `await`-ed
to retrieve the result of the spawned future. to retrieve the result of the spawned future.
@ -43,10 +42,10 @@ where
This [`Receiver`] may be safely ignored if the result of the This [`Receiver`] may be safely ignored if the result of the
spawned future is not needed, the future will run either way. spawned future is not needed, the future will run either way.
*/ */
pub fn schedule_future_background<F, FR>(&self, fut: F) -> Receiver<FR> pub fn spawn<F>(&self, fut: F) -> Receiver<F::Output>
where where
F: Future<Output = FR> + Send + 'static, F: Future + Send + 'static,
FR: Send + 'static, F::Output: Send + 'static,
{ {
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
@ -55,6 +54,8 @@ where
tx.send(res).ok(); tx.send(res).ok();
}); });
// NOTE: We must spawn a future on our scheduler which awaits
// the handle from tokio to start driving our future properly
let futs = self let futs = self
.futures_background .futures_background
.try_lock() .try_lock()
@ -67,13 +68,13 @@ where
} }
/** /**
Equivalent to [`schedule_future_background`], except the Equivalent to [`spawn`], except the future is only
future is only spawned on the scheduler, on the main thread. spawned on the Lune scheduler, and on the main thread.
*/ */
pub fn schedule_future_background_local<F, FR>(&self, fut: F) -> Receiver<FR> pub fn spawn_local<F>(&self, fut: F) -> Receiver<F::Output>
where where
F: Future<Output = FR> + 'static, F: Future + 'static,
FR: 'static, F::Output: 'static,
{ {
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
@ -94,7 +95,7 @@ where
If the given future returns a [`LuaError`], that error will be passed to the given `thread`. If the given future returns a [`LuaError`], that error will be passed to the given `thread`.
*/ */
pub fn schedule_future_thread<F, FR>( pub fn spawn_thread<F, FR>(
&'fut self, &'fut self,
thread: impl IntoLuaThread<'fut>, thread: impl IntoLuaThread<'fut>,
fut: F, fut: F,

View file

@ -57,7 +57,7 @@ where
let sched = lua let sched = lua
.app_data_ref::<&Scheduler>() .app_data_ref::<&Scheduler>()
.expect("Lua struct is missing scheduler"); .expect("Lua struct is missing scheduler");
sched.schedule_future_thread(thread, future)?; sched.spawn_thread(thread, future)?;
Ok(()) Ok(())
}), }),
)?; )?;