Add sleep_on_start option

This commit is contained in:
timvisee 2021-11-08 17:41:02 +01:00
parent a1b64a2b24
commit e115bba827
No known key found for this signature in database
GPG Key ID: B8DB720BC383E172
5 changed files with 35 additions and 9 deletions

View File

@ -19,6 +19,9 @@ command = "java -Xmx1G -Xms1G -jar server.jar --nogui"
# Internal IP and port of server started by lazymc to proxy to. # Internal IP and port of server started by lazymc to proxy to.
address = "127.0.0.1:25566" address = "127.0.0.1:25566"
# Start sleeping when starting lazymc.
sleep_on_start = true
[time] [time]
# Sleep after number of seconds. # Sleep after number of seconds.
sleep_after = 60 sleep_after = 60

View File

@ -99,6 +99,9 @@ pub struct Server {
/// Ingress address. /// Ingress address.
#[serde(alias = "address_ingress")] #[serde(alias = "address_ingress")]
pub address: SocketAddr, pub address: SocketAddr,
/// Immediately start sleeping when starting lazymc.
pub sleep_on_start: bool,
} }
/// Time configuration. /// Time configuration.

View File

@ -2,6 +2,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use futures::FutureExt;
use minecraft_protocol::data::server_status::ServerStatus; use minecraft_protocol::data::server_status::ServerStatus;
use tokio::process::Command; use tokio::process::Command;
@ -146,8 +147,27 @@ impl ServerState {
} }
} }
/// Start Minecraft server. /// Try to start the server.
pub async fn start( ///
/// 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>, config: Arc<Config>,
state: Arc<ServerState>, state: Arc<ServerState>,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {

View File

@ -6,6 +6,7 @@ use tokio::net::TcpListener;
use crate::config::Config; use crate::config::Config;
use crate::proto::Client; use crate::proto::Client;
use crate::proxy; use crate::proxy;
use crate::server;
use crate::server::ServerState; use crate::server::ServerState;
use crate::service; use crate::service;
use crate::status; use crate::status;
@ -40,6 +41,11 @@ pub async fn service(config: Arc<Config>) -> Result<(), ()> {
)); ));
tokio::spawn(service::signal::service(server_state.clone())); 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 // Proxy all incomming connections
while let Ok((inbound, _)) = listener.accept().await { while let Ok((inbound, _)) = listener.accept().await {
let client = Client::default(); let client = Client::default();

View File

@ -1,7 +1,6 @@
use std::sync::Arc; use std::sync::Arc;
use bytes::BytesMut; use bytes::BytesMut;
use futures::FutureExt;
use minecraft_protocol::data::chat::{Message, Payload}; use minecraft_protocol::data::chat::{Message, Payload};
use minecraft_protocol::data::server_status::*; use minecraft_protocol::data::server_status::*;
use minecraft_protocol::decoder::Decoder; use minecraft_protocol::decoder::Decoder;
@ -57,12 +56,7 @@ pub async fn serve(
writer.write_all(&response).await.map_err(|_| ())?; writer.write_all(&response).await.map_err(|_| ())?;
// Start server if not starting yet // Start server if not starting yet
// TODO: move this into server state? server::start_server(config, server);
if !server.starting() {
server.set_starting(true);
server.update_last_active_time();
tokio::spawn(server::start(config, server).map(|_| ()));
}
break; break;
} }