mirror of
https://github.com/timvisee/lazymc.git
synced 2025-05-19 12:50:23 -07:00
Add sleep_on_start option
This commit is contained in:
parent
a1b64a2b24
commit
e115bba827
@ -19,6 +19,9 @@ command = "java -Xmx1G -Xms1G -jar server.jar --nogui"
|
||||
# Internal IP and port of server started by lazymc to proxy to.
|
||||
address = "127.0.0.1:25566"
|
||||
|
||||
# Start sleeping when starting lazymc.
|
||||
sleep_on_start = true
|
||||
|
||||
[time]
|
||||
# Sleep after number of seconds.
|
||||
sleep_after = 60
|
||||
|
@ -99,6 +99,9 @@ pub struct Server {
|
||||
/// Ingress address.
|
||||
#[serde(alias = "address_ingress")]
|
||||
pub address: SocketAddr,
|
||||
|
||||
/// Immediately start sleeping when starting lazymc.
|
||||
pub sleep_on_start: bool,
|
||||
}
|
||||
|
||||
/// Time configuration.
|
||||
|
@ -2,6 +2,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use futures::FutureExt;
|
||||
use minecraft_protocol::data::server_status::ServerStatus;
|
||||
use tokio::process::Command;
|
||||
|
||||
@ -146,8 +147,27 @@ impl ServerState {
|
||||
}
|
||||
}
|
||||
|
||||
/// Start Minecraft server.
|
||||
pub async fn start(
|
||||
/// Try to start the server.
|
||||
///
|
||||
/// Does not start if alreayd starting.
|
||||
// TODO: move this into server state struct?
|
||||
pub fn start_server(config: Arc<Config>, server: Arc<ServerState>) {
|
||||
// Ensure it is not starting yet
|
||||
if server.starting() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Update starting states
|
||||
// TODO: this may data race, use single atomic operation
|
||||
server.set_starting(true);
|
||||
server.update_last_active_time();
|
||||
|
||||
// Spawn server in separate task
|
||||
tokio::spawn(invoke_server_command(config, server).map(|_| ()));
|
||||
}
|
||||
|
||||
/// Invoke server command, store PID and wait for it to quit.
|
||||
pub async fn invoke_server_command(
|
||||
config: Arc<Config>,
|
||||
state: Arc<ServerState>,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
@ -6,6 +6,7 @@ use tokio::net::TcpListener;
|
||||
use crate::config::Config;
|
||||
use crate::proto::Client;
|
||||
use crate::proxy;
|
||||
use crate::server;
|
||||
use crate::server::ServerState;
|
||||
use crate::service;
|
||||
use crate::status;
|
||||
@ -40,6 +41,11 @@ pub async fn service(config: Arc<Config>) -> Result<(), ()> {
|
||||
));
|
||||
tokio::spawn(service::signal::service(server_state.clone()));
|
||||
|
||||
// Initiate server start
|
||||
if !config.server.sleep_on_start {
|
||||
server::start_server(config.clone(), server_state.clone());
|
||||
}
|
||||
|
||||
// Proxy all incomming connections
|
||||
while let Ok((inbound, _)) = listener.accept().await {
|
||||
let client = Client::default();
|
||||
|
@ -1,7 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use bytes::BytesMut;
|
||||
use futures::FutureExt;
|
||||
use minecraft_protocol::data::chat::{Message, Payload};
|
||||
use minecraft_protocol::data::server_status::*;
|
||||
use minecraft_protocol::decoder::Decoder;
|
||||
@ -57,12 +56,7 @@ pub async fn serve(
|
||||
writer.write_all(&response).await.map_err(|_| ())?;
|
||||
|
||||
// Start server if not starting yet
|
||||
// TODO: move this into server state?
|
||||
if !server.starting() {
|
||||
server.set_starting(true);
|
||||
server.update_last_active_time();
|
||||
tokio::spawn(server::start(config, server).map(|_| ()));
|
||||
}
|
||||
server::start_server(config, server);
|
||||
|
||||
break;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user