mirror of
https://github.com/CompeyDev/lune-packaging.git
synced 2025-01-09 12:19:09 +00:00
Improve error for net serve failing to bind
This commit is contained in:
parent
a9261f889f
commit
f98400f753
3 changed files with 32 additions and 5 deletions
|
@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- Improve error messages when `net.serve` fails
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
- Fixed `task.delay` keeping the script running even if it was cancelled using `task.cancel`
|
- Fixed `task.delay` keeping the script running even if it was cancelled using `task.cancel`
|
||||||
|
|
|
@ -6,6 +6,7 @@ use std::{
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use console::style;
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
|
|
||||||
use hyper::{body::to_bytes, server::conn::AddrStream, service::Service};
|
use hyper::{body::to_bytes, server::conn::AddrStream, service::Service};
|
||||||
|
@ -175,11 +176,24 @@ async fn net_serve<'a>(
|
||||||
lua.create_registry_value(handler)
|
lua.create_registry_value(handler)
|
||||||
.expect("Failed to store websocket handler")
|
.expect("Failed to store websocket handler")
|
||||||
}));
|
}));
|
||||||
|
let sched = lua.app_data_mut::<&TaskScheduler>().unwrap();
|
||||||
|
// Bind first to make sure that we can bind to this address
|
||||||
|
let bound = match Server::try_bind(&([127, 0, 0, 1], port).into()) {
|
||||||
|
Err(e) => {
|
||||||
|
return Err(LuaError::external(format!(
|
||||||
|
"Failed to bind to localhost on port {port}\n{}",
|
||||||
|
format!("{e}").replace(
|
||||||
|
"error creating server listener: ",
|
||||||
|
&format!("{}", style("> ").dim())
|
||||||
|
)
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
Ok(bound) => bound,
|
||||||
|
};
|
||||||
// Register a background task to prevent
|
// Register a background task to prevent
|
||||||
// the task scheduler from exiting early
|
// the task scheduler from exiting early
|
||||||
let sched = lua.app_data_mut::<&TaskScheduler>().unwrap();
|
|
||||||
let task = sched.register_background_task();
|
let task = sched.register_background_task();
|
||||||
let server = Server::bind(&([127, 0, 0, 1], port).into())
|
let server = bound
|
||||||
.http1_only(true)
|
.http1_only(true)
|
||||||
.http1_keepalive(true)
|
.http1_keepalive(true)
|
||||||
.executor(LocalExec)
|
.executor(LocalExec)
|
||||||
|
@ -189,12 +203,12 @@ async fn net_serve<'a>(
|
||||||
server_websocket_callback,
|
server_websocket_callback,
|
||||||
))
|
))
|
||||||
.with_graceful_shutdown(async move {
|
.with_graceful_shutdown(async move {
|
||||||
|
task.unregister(Ok(()));
|
||||||
shutdown_rx
|
shutdown_rx
|
||||||
.recv()
|
.recv()
|
||||||
.await
|
.await
|
||||||
.expect("Server was stopped instantly");
|
.expect("Server was stopped instantly");
|
||||||
shutdown_rx.close();
|
shutdown_rx.close();
|
||||||
task.unregister(Ok(()));
|
|
||||||
});
|
});
|
||||||
// Spawn a new tokio task so we don't block
|
// Spawn a new tokio task so we don't block
|
||||||
task::spawn_local(server);
|
task::spawn_local(server);
|
||||||
|
|
|
@ -90,11 +90,20 @@ pub struct Task {
|
||||||
#[must_use = "Background tasks must be unregistered"]
|
#[must_use = "Background tasks must be unregistered"]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct TaskSchedulerBackgroundTaskHandle {
|
pub struct TaskSchedulerBackgroundTaskHandle {
|
||||||
|
unregistered: bool,
|
||||||
sender: mpsc::UnboundedSender<TaskSchedulerRegistrationMessage>,
|
sender: mpsc::UnboundedSender<TaskSchedulerRegistrationMessage>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TaskSchedulerBackgroundTaskHandle {
|
impl TaskSchedulerBackgroundTaskHandle {
|
||||||
pub fn unregister(self, result: LuaResult<()>) {
|
pub fn new(sender: mpsc::UnboundedSender<TaskSchedulerRegistrationMessage>) -> Self {
|
||||||
|
Self {
|
||||||
|
unregistered: false,
|
||||||
|
sender,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn unregister(mut self, result: LuaResult<()>) {
|
||||||
|
self.unregistered = true;
|
||||||
self.sender
|
self.sender
|
||||||
.send(TaskSchedulerRegistrationMessage::Terminated(result))
|
.send(TaskSchedulerRegistrationMessage::Terminated(result))
|
||||||
.unwrap_or_else(|_| {
|
.unwrap_or_else(|_| {
|
||||||
|
@ -770,7 +779,7 @@ impl<'fut> TaskScheduler<'fut> {
|
||||||
env!("CARGO_PKG_REPOSITORY")
|
env!("CARGO_PKG_REPOSITORY")
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
TaskSchedulerBackgroundTaskHandle { sender }
|
TaskSchedulerBackgroundTaskHandle::new(sender)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue