Skip to content

Commit 3a1ca87

Browse files
committed
feat: allow disabling restart and launch wrapper functionality
adds a new config option `restart`, and makes `run` optional. these allow the wrapper to be used both as a pre-launch command (by not setting the `run` option), or as a wrapper that is managed by a tool that handles restarting, such as pterodactyl
1 parent a3b0a2a commit 3a1ca87

2 files changed

Lines changed: 49 additions & 25 deletions

File tree

src/config.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ mod destinations;
1313

1414
#[derive(Debug, Clone, Serialize, Deserialize)]
1515
pub struct Config {
16-
pub run: Vec<String>,
16+
#[serde(default)]
17+
pub run: Option<Vec<String>>,
1718
#[serde(default = "Default::default")]
1819
pub status: Status,
1920
#[serde(default = "Default::default")]
2021
pub tokens: Tokens,
2122
pub triggers: HashMap<String, Trigger>,
2223
#[serde(default = "default_min_restart_interval")]
2324
pub min_restart_interval_seconds: u64,
25+
#[serde()]
26+
pub restart: bool,
2427
}
2528

2629
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
@@ -45,7 +48,7 @@ pub enum Trigger {
4548
impl Default for Config {
4649
fn default() -> Self {
4750
Config {
48-
run: vec!["java -jar fabric-server-launch.jar".to_owned()],
51+
run: Some(vec!["java -jar fabric-server-launch.jar".to_owned()]),
4952
tokens: Tokens::default(),
5053
status: Status::default(),
5154
triggers: {
@@ -54,6 +57,7 @@ impl Default for Config {
5457
triggers
5558
},
5659
min_restart_interval_seconds: default_min_restart_interval(),
60+
restart: default_restart(),
5761
}
5862
}
5963
}
@@ -62,21 +66,27 @@ fn default_min_restart_interval() -> u64 {
6266
240
6367
}
6468

69+
fn default_restart() -> bool {
70+
true
71+
}
72+
6573
pub async fn load<P, T>(path: P) -> T
6674
where
6775
P: AsRef<Path>,
6876
T: Serialize + DeserializeOwned + Default,
6977
{
7078
let path = path.as_ref();
71-
if path.exists() {
79+
let mut config = if path.exists() {
7280
read_config(path).await.expect("failed to read config")
7381
} else {
7482
let config = T::default();
7583
write_config(path, &config)
7684
.await
7785
.expect("failed to write default config");
7886
config
79-
}
87+
};
88+
89+
config
8090
}
8191

8292
async fn write_config<T: Serialize>(path: &Path, config: &T) -> io::Result<()> {

src/main.rs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,17 @@ pub async fn main() {
3434
let destinations_path = std::env::args().nth(2).unwrap_or_else(|| "destinations.toml".to_owned());
3535

3636
loop {
37-
let config: Config = config::load(&config_path).await;
37+
let mut config: Config = config::load(&config_path).await;
3838
let destinations: config::Destinations = config::load(&destinations_path).await;
3939

40+
if config.run.is_none() && config.restart {
41+
eprintln!("warning: `restart = true` has no effect if `run` is not set");
42+
config.restart = false;
43+
}
44+
45+
// make immutable just to be sure
46+
let config = config;
47+
4048
let min_restart_interval = Duration::from_secs(config.min_restart_interval_seconds);
4149

4250
let status = match config.status.webhook {
@@ -102,28 +110,34 @@ pub async fn main() {
102110

103111
ctx.status.write(payload);
104112

105-
let start = Instant::now();
106-
107-
let mut executor = Executor::new(config.run);
108-
if let Err(err) = executor.run().await {
109-
eprintln!("server exited with error: {:?}", err);
110-
} else {
111-
println!("server closed");
112-
}
113-
114-
let interval = Instant::now() - start;
115-
if interval < min_restart_interval {
116-
println!("server restarted very quickly! waiting a bit...");
113+
if let Some(run) = config.run {
114+
let start = Instant::now();
117115

118-
let delay = min_restart_interval - interval;
119-
ctx.status.write(format!(
120-
"Server restarted too quickly! Waiting for {} seconds...",
121-
delay.as_secs()
122-
));
116+
let mut executor = Executor::new(run);
117+
if let Err(err) = executor.run().await {
118+
eprintln!("server exited with error: {:?}", err);
119+
} else {
120+
println!("server closed");
121+
}
123122

124-
tokio::time::sleep(delay.into()).await;
125-
} else {
126-
ctx.status.write("Server closed! Restarting...");
123+
let interval = Instant::now() - start;
124+
if config.restart && interval < min_restart_interval {
125+
println!("server restarted very quickly! waiting a bit...");
126+
127+
let delay = min_restart_interval - interval;
128+
ctx.status.write(format!(
129+
"Server restarted too quickly! Waiting for {} seconds...",
130+
delay.as_secs()
131+
));
132+
133+
tokio::time::sleep(delay.into()).await;
134+
} else {
135+
ctx.status.write(if config.restart {
136+
"Server closed! Restarting..."
137+
} else {
138+
"Server closed!"
139+
});
140+
}
127141
}
128142
}
129143
}

0 commit comments

Comments
 (0)