Compare commits

...

10 Commits
1.0.1 ... main

Author SHA1 Message Date
Ian Wijma e3623f4ecc Refactor argument handling in main.rs for increased readability
The argument handling in main.rs has been adjusted to utilize argument destructuring, making the code more readable and organized. Additionally, the unnecessary new lines were removed, and commas were added to the last elements of the 'Arguments' struct to follow Rust norms and increase consistency.
2024-01-06 19:57:04 +11:00
Ian Wijma 806d17328c Update kr package version to 1.0.3
Updated the version number of the kr package in both Cargo.toml and Cargo.lock files from version 1.0.2 to 1.0.3. This was necessary for incorporating new features and fixes from the updated version.
2023-12-02 21:52:49 +11:00
Ian Wijma ef1e2c56c0 Refactor 'Retry' instance creation and modify 'run_command' function
Changed the approach for creating 'Retry' instance in main() to avoid mutation behavior by setting 'max_retries', 'timespan' and 'restart_name' initially. The 'per_minute' and 'per_hour' arguments now directly modify these values. Additionally, the 'run_command' function and related functions were refactored to pass 'Retry' instance by value, not reference and to handle 'retry' history separately for each command execution. These changes were made to improve code readability and maintainability.
2023-12-02 21:52:09 +11:00
Ian Wijma 5c19c27136 Add readme.md file
A new readme.md file is added to provide detailed information about usage of Keep Running application. The readme describes the purpose of the application, how to use it, its default settings, arguments and options. It also provides a use case example for better understanding.
2023-11-23 21:27:15 +11:00
Ian Wijma aad5010257 Update workflow run-name and remove id from 'Compile and release'
The run-name in the Github actions workflow 'release.yaml' was updated for clarity, now indicating that the files are being built for release. An unnecessary id tag was removed from the 'Compile and release' step, simplifying the workflow. This makes the functioning of the workflow easier to understand and manage.
2023-11-21 01:13:08 +11:00
Ian Wijma ffa6d18e5e Update GitHub CI action trigger in release.yaml
Changed the event trigger of the GitHub Actions workflow "release.yaml" from a generic 'release' event to specifically when a 'release' is 'created'. This provides the detailed triggering condition for the workflow, ensuring it only runs when needed, improving workflow optimization.
2023-11-21 01:10:07 +11:00
Ian Wijma 049a75e300 Update version and fix CI action name
Updated kr package's version to 1.0.2 in Cargo.toml and Cargo.lock files. Also, fixed the wrong property reference in the GitHub Actions release workflow. From "gitea.actor" to "github.actor" which might have caused incorrect workflow run name in GitHub CI/CD.
2023-11-21 01:07:37 +11:00
Ian Wijma dcf83a351f Update version and fix CI action name
Updated kr package's version to 1.0.2 in Cargo.toml and Cargo.lock files. Also, fixed the wrong property reference in the GitHub Actions release workflow. From "gitea.actor" to "github.actor" which might have caused incorrect workflow run name in GitHub CI/CD.
2023-11-21 01:03:51 +11:00
Ian Wijma 66ab232c9d Updated .gitignore, Cargo.toml and release.yaml files
This commit adds newline at the end of the .gitignore and release.yaml files to adhere to posix standard while removing unnecessary whitespace in Cargo.toml file. Changes are made for better code health and adhering to standards.
2023-11-21 00:57:18 +11:00
Ian Wijma 99947dbefd Refactor spawn_process fn to handle spawn error
This commit modifies the spawn_process function to explicitly return a Result type, handling the error of the spawn() method. This change improves the readability of the error handling and makes it more idiomatic in Rust. As a secondary correction, it fixes a typo in a print statement.
2023-11-21 00:34:53 +11:00
7 changed files with 84 additions and 47 deletions

View File

@ -1,9 +1,12 @@
name: Releasing
run-name: ${{ gitea.actor }} is building a release releasing 🚀
on: [release]
run-name: ${{ github.actor }} is building the files for a release 🚀
on:
release:
types: [created]
jobs:
release:
name: release ${{ matrix.target }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
@ -18,19 +21,9 @@ jobs:
steps:
- uses: actions/checkout@master
- name: Compile and release
id: compile
uses: rust-build/rust-build.action@v1.4.4
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
RUSTTARGET: ${{ matrix.target }}
ARCHIVE_TYPES: ${{ matrix.archive }}
UPLOAD_MODE: 'none'
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: Binary
path: |
${{ steps.compile.outputs.BUILT_ARCHIVE }}
${{ steps.compile.outputs.BUILT_CHECKSUM }}

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
/target
/.idea
/.idea

2
Cargo.lock generated
View File

@ -123,7 +123,7 @@ checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "kr"
version = "1.0.0"
version = "1.0.3"
dependencies = [
"clap",
]

View File

@ -1,6 +1,6 @@
[package]
name = "kr"
version = "1.0.1"
version = "1.0.3"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@ -13,4 +13,3 @@ strip = true # Automatically strip symbols from the binary.
opt-level = "z" # Optimize for size.
lto = true
codegen-units = 1

Binary file not shown.

After

Width:  |  Height:  |  Size: 119 KiB

35
readme.md Normal file
View File

@ -0,0 +1,35 @@
Keep Running
---
Keep running is a quick and easy way to **keep** a process **running** during development or other **non-production** tasks.
> Please for the love of sanity, do not use this is production...
# Usage
Below you will see the output of the `--help` command.
Do note that if you don't specify any `--per-*` flag, it defaults to `--per-minute=4`.
```
Usage: kr [OPTIONS] <COMMAND>
Arguments:
<COMMAND>
Options:
--per-minute <PER_MINUTE> [default: 0]
--per-hour <PER_HOUR> [default: 0]
--delay <DELAY> [default: 0]
-h, --help Print help
-V, --version Print version
```
# Use cases
## Game servers
/keep-running/ is a great tool to run, for example, a Modded minecraft server.
As it automatically restarts the server if a crash occurs.
![img.png](assets/vault-hunters-3rd.png)

View File

@ -17,7 +17,7 @@ struct Arguments {
delay: u8,
#[arg()]
command: String
command: String,
}
const SECONDS_IN_A_MINUTE: u16 = 60;
@ -29,30 +29,36 @@ struct Retry {
max_retries: u8,
timespan: u16,
restart_delay: u8,
restart_name: String
restart_name: String,
}
fn main() {
let arguments = Arguments::parse();
let mut retry: Retry = Retry {
command: arguments.command,
history: Vec::new(),
max_retries: 4,
timespan: SECONDS_IN_A_MINUTE,
restart_delay: arguments.delay,
restart_name: "minute".to_string(),
};
let mut max_retries = 4;
let mut timespan = SECONDS_IN_A_MINUTE;
let mut restart_name = String::from("minute");
if arguments.per_minute > 0 {
retry.max_retries = arguments.per_minute;
} else if arguments.per_hour > 0 {
retry.max_retries = arguments.per_hour;
retry.timespan = SECONDS_IN_A_HOUR;
retry.restart_name = "hour".to_string();
let Arguments { per_minute, per_hour, .. } = arguments;
if per_minute > 0 {
max_retries = per_minute;
} else if per_hour > 0 {
max_retries = per_hour;
timespan = SECONDS_IN_A_HOUR;
restart_name = String::from("hour");
}
run_command(&mut retry)
let Arguments { command, delay: restart_delay, .. } = arguments;
let retry: Retry = Retry {
max_retries,
timespan,
restart_name,
command,
restart_delay,
history: Vec::new(),
};
run_command(retry)
}
fn get_now() -> u64 {
@ -62,26 +68,29 @@ fn get_now() -> u64 {
.as_secs();
}
fn push_history(retry: &mut Retry) {
retry.history.push(get_now() + u64::from(retry.timespan));
fn push_history(mut history: Vec<u64>, timespan: u16) -> Vec<u64> {
history.push(get_now() + u64::from(timespan));
return history.to_owned()
}
fn update_history(retry: &mut Retry) {
fn update_history(mut history: Vec<u64>) -> Vec<u64> {
let now = get_now();
let clear_times: Vec<u64> = retry
.history
let clear_times: Vec<u64> = history
.iter()
.filter(|&time| time <= &now)
.map(|&time| time)
.collect();
for time in clear_times {
retry.history.retain(|&h| h != time);
history.retain(|&h| h != time);
}
return history.to_owned();
}
fn check_history(retry: &Retry) -> bool {
return retry.history.len().lt(&usize::from(retry.max_retries))
fn check_history(history: &Vec<u64>, max_retries: u8) -> bool {
return history.len().lt(&usize::from(max_retries))
}
fn spawn_process(retry: &Retry) -> Result<Child, std::io::Error> {
@ -101,8 +110,8 @@ fn spawn_process(retry: &Retry) -> Result<Child, std::io::Error> {
}
}
fn run_command(retry: &mut Retry) {
let mut process = spawn_process(retry)
fn run_command(mut retry: Retry) {
let mut process = spawn_process(&retry)
.expect("Process failed on startup");
let exit_code = match process.wait() {
@ -117,9 +126,10 @@ fn run_command(retry: &mut Retry) {
println!("Exit code: {}", exit_code);
} else {
println!("[CRASH] exit code: {}", exit_code);
push_history(retry);
update_history(retry);
if check_history(retry) {
let pushed_history = push_history(retry.history, retry.timespan);
let updated_history = update_history(pushed_history);
if check_history(&updated_history, retry.max_retries) {
retry.history = updated_history;
println!("Restarting...");
restart(retry);
} else {
@ -128,7 +138,7 @@ fn run_command(retry: &mut Retry) {
}
}
fn restart(retry: &mut Retry) {
fn restart(retry: Retry) {
sleep(Duration::from_secs(u64::from(retry.restart_delay)));
run_command(retry);