Compare commits

..

No commits in common. "0a459ee30b840fbc08ab85c1f04e04c7351c8a27" and "188fb3034335a4244fd754cc668090de90d8d5f2" have entirely different histories.

6 changed files with 21 additions and 61 deletions

View File

@ -1,3 +1,3 @@
[alias]
runs = "run -- run --config data/config --backup data/backups --world data/worlds --jar paper-1.19.4-550.jar --frequency 2"
runs = "run -- run paper 1.19.4-550 --config data/config --backup data/backups --world data/worlds --jar paper-1.19.4-550.jar"
runrs = "run --release -- paper 1.19.4-545 --config data/config --backup data/backups --world data/worlds --jar data/paper-1.19.4-525.jar"

View File

@ -1,5 +1,4 @@
use super::Backup;
use chrono::Utc;
use serde::Deserialize;
use serde::Serialize;
use std::fs::File;
@ -16,7 +15,6 @@ where
default_metadata: T,
chain_len: u64,
chains_to_keep: u64,
frequency: chrono::Duration,
chains: Vec<Vec<Backup<T>>>,
}
@ -27,23 +25,21 @@ where
const METADATA_FILE: &str = "alex.json";
/// Initialize a new instance of a `BackupManager`.
pub fn new<P1: Into<PathBuf>, P2: Into<PathBuf>, P3: Into<PathBuf>>(
backup_dir: P1,
config_dir: P2,
world_dir: P3,
pub fn new(
backup_dir: PathBuf,
config_dir: PathBuf,
world_dir: PathBuf,
metadata: T,
chain_len: u64,
chains_to_keep: u64,
frequency: chrono::Duration,
) -> Self {
Self {
backup_dir: backup_dir.into(),
config_dir: config_dir.into(),
world_dir: world_dir.into(),
backup_dir,
config_dir,
world_dir,
default_metadata: metadata,
chain_len,
chains_to_keep,
frequency,
chains: Vec::new(),
}
}
@ -141,16 +137,4 @@ where
Ok(())
}
/// Calculate the next time a backup should be created. If no backup has been created yet, it
/// will return now.
pub fn next_scheduled_time(&self) -> chrono::DateTime<Utc> {
if let Some(last_chain) = self.chains.last() {
if let Some(last_backup) = last_chain.last() {
return last_backup.start_time + self.frequency;
}
}
chrono::offset::Utc::now()
}
}

View File

@ -53,22 +53,11 @@ pub struct Cli {
global = true
)]
pub chains: u64,
/// How frequently to perform a backup, in minutes; 0 to disable.
#[arg(
short = 't',
long,
default_value_t = 0,
env = "ALEX_FREQUENCY",
global = true
)]
pub frequency: u32,
/// Type of server
#[arg(long, default_value = "unknown", env = "ALEX_SERVER", global = true)]
#[arg(long, default_value = "unknown", env = "ALEX_SERVER")]
pub server: ServerType,
/// Version string for the server, e.g. 1.19.4-545
#[arg(long, default_value = "", env = "ALEX_SERVER_VERSION", global = true)]
#[arg(long, default_value = "", env = "ALEX_SERVER_VERSION")]
pub server_version: String,
}
@ -103,6 +92,10 @@ pub struct RunArgs {
#[arg(long, default_value_t = 2048, env = "ALEX_XMX")]
pub xmx: u64,
/// How frequently to perform a backup, in minutes; 0 to disable.
#[arg(short = 't', long, default_value_t = 0, env = "ALEX_FREQUENCY")]
pub frequency: u64,
/// Don't actually run the server, but simply output the server configuration that would have
/// been ran
#[arg(short, long, default_value_t = false)]

View File

@ -9,17 +9,9 @@ use cli::{BackupArgs, Cli, Commands, RunArgs};
use std::io;
use std::sync::{Arc, Mutex};
fn backups_thread(counter: Arc<Mutex<server::ServerProcess>>) {
fn backups_thread(counter: Arc<Mutex<server::ServerProcess>>, frequency: u64) {
loop {
let next_scheduled_time = {
let server = counter.lock().unwrap();
server.backups.next_scheduled_time()
};
let now = chrono::offset::Utc::now();
if next_scheduled_time > now {
std::thread::sleep((next_scheduled_time - now).to_std().unwrap());
}
std::thread::sleep(std::time::Duration::from_secs(frequency * 60));
{
let mut server = counter.lock().unwrap();
@ -42,8 +34,7 @@ fn command_run(cli: &Cli, args: &RunArgs) -> io::Result<()> {
.xms(args.xms)
.xmx(args.xmx)
.chain_len(cli.chain_len)
.chains_to_keep(cli.chains)
.frequency(cli.frequency);
.chains_to_keep(cli.chains);
cmd.canonicalize()?;
if args.dry {
@ -54,9 +45,10 @@ fn command_run(cli: &Cli, args: &RunArgs) -> io::Result<()> {
let counter = Arc::new(Mutex::new(cmd.spawn()?));
if cli.frequency > 0 {
if args.frequency > 0 {
let clone = Arc::clone(&counter);
std::thread::spawn(move || backups_thread(clone));
let frequency = args.frequency;
std::thread::spawn(move || backups_thread(clone, frequency));
}
// Spawn thread that handles the main stdin loop
@ -80,7 +72,6 @@ fn commands_backup(cli: &Cli, _args: &BackupArgs) -> io::Result<()> {
metadata,
cli.chain_len,
cli.chains,
chrono::Duration::minutes(cli.frequency.into()),
);
manager.load()?;

View File

@ -47,7 +47,6 @@ pub struct ServerCommand {
xmx: u64,
chain_len: u64,
chains_to_keep: u64,
frequency: u32,
}
impl ServerCommand {
@ -64,7 +63,6 @@ impl ServerCommand {
xmx: 2048,
chain_len: 4,
chains_to_keep: 7,
frequency: 0,
}
}
@ -115,11 +113,6 @@ impl ServerCommand {
self
}
pub fn frequency(mut self, v: u32) -> Self {
self.frequency = v;
self
}
fn accept_eula(&self) -> std::io::Result<()> {
let mut eula_path = self.config_dir.clone();
eula_path.push("eula.txt");
@ -214,7 +207,6 @@ impl ServerCommand {
metadata,
self.chain_len,
self.chains_to_keep,
chrono::Duration::minutes(self.frequency.into()),
);
manager.load()?;

View File

@ -5,7 +5,7 @@ use std::process::Child;
pub struct ServerProcess {
child: Child,
pub backups: BackupManager<Metadata>,
backups: BackupManager<Metadata>,
}
impl ServerProcess {