diff --git a/src/lune/builtins/net/mod.rs b/src/lune/builtins/net/mod.rs index a18b3c0..b5733cb 100644 --- a/src/lune/builtins/net/mod.rs +++ b/src/lune/builtins/net/mod.rs @@ -170,7 +170,7 @@ where // Start up our web server // TODO: Spawn a scheduler background task here, // and communicate using an mpsc channel instead - sched.schedule_future_background_local(async move { + sched.spawn_local(async move { bound .http1_only(true) // Web sockets can only use http1 .http1_keepalive(true) // Web sockets must be kept alive diff --git a/src/lune/builtins/net/server.rs b/src/lune/builtins/net/server.rs index 0cb8ac6..971243d 100644 --- a/src/lune/builtins/net/server.rs +++ b/src/lune/builtins/net/server.rs @@ -53,7 +53,7 @@ impl Service> for NetServiceInner { let sched = lua .app_data_ref::<&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 // schedule our handler to get called asap let res = async move { diff --git a/src/lune/builtins/process/mod.rs b/src/lune/builtins/process/mod.rs index ac4070a..212d325 100644 --- a/src/lune/builtins/process/mod.rs +++ b/src/lune/builtins/process/mod.rs @@ -175,10 +175,8 @@ async fn process_spawn( .app_data_ref::<&Scheduler>() .expect("Lua struct is missing scheduler"); - let fut = spawn_command(program, args, options); - let recv = sched.schedule_future_background(fut); - - let (status, stdout, stderr) = recv + let (status, stdout, stderr) = sched + .spawn(spawn_command(program, args, options)) .await .expect("Failed to receive result of spawned process")?; diff --git a/src/lune/builtins/task/mod.rs b/src/lune/builtins/task/mod.rs index 71e74f4..f757d84 100644 --- a/src/lune/builtins/task/mod.rs +++ b/src/lune/builtins/task/mod.rs @@ -105,7 +105,7 @@ where .expect("Lua struct is missing scheduler"); 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); time::sleep(duration).await; sched.push_back(thread2, args)?; diff --git a/src/lune/scheduler/impl_async.rs b/src/lune/scheduler/impl_async.rs index f97b7ca..1a61414 100644 --- a/src/lune/scheduler/impl_async.rs +++ b/src/lune/scheduler/impl_async.rs @@ -33,9 +33,8 @@ where /** Schedules a plain future to run in the background. - This will spawn the future both on the scheduler and - potentially on a different thread using [`task::spawn`], - meaning the provided future must implement [`Send`]. + This will potentially spawn the future on a different thread, using + [`task::spawn`], meaning the provided future must implement [`Send`]. Returns a [`Receiver`] which may be `await`-ed to retrieve the result of the spawned future. @@ -43,10 +42,10 @@ where This [`Receiver`] may be safely ignored if the result of the spawned future is not needed, the future will run either way. */ - pub fn schedule_future_background(&self, fut: F) -> Receiver + pub fn spawn(&self, fut: F) -> Receiver where - F: Future + Send + 'static, - FR: Send + 'static, + F: Future + Send + 'static, + F::Output: Send + 'static, { let (tx, rx) = oneshot::channel(); @@ -55,6 +54,8 @@ where 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 .futures_background .try_lock() @@ -67,13 +68,13 @@ where } /** - Equivalent to [`schedule_future_background`], except the - future is only spawned on the scheduler, on the main thread. + Equivalent to [`spawn`], except the future is only + spawned on the Lune scheduler, and on the main thread. */ - pub fn schedule_future_background_local(&self, fut: F) -> Receiver + pub fn spawn_local(&self, fut: F) -> Receiver where - F: Future + 'static, - FR: 'static, + F: Future + 'static, + F::Output: 'static, { 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`. */ - pub fn schedule_future_thread( + pub fn spawn_thread( &'fut self, thread: impl IntoLuaThread<'fut>, fut: F, diff --git a/src/lune/scheduler/traits.rs b/src/lune/scheduler/traits.rs index 541f35e..c3f2c4b 100644 --- a/src/lune/scheduler/traits.rs +++ b/src/lune/scheduler/traits.rs @@ -57,7 +57,7 @@ where let sched = lua .app_data_ref::<&Scheduler>() .expect("Lua struct is missing scheduler"); - sched.schedule_future_thread(thread, future)?; + sched.spawn_thread(thread, future)?; Ok(()) }), )?;