Skip to content

Commit

Permalink
🔀 📦️ Reduced package size ~120MB to ~13MB, added health-check for som…
Browse files Browse the repository at this point in the history
…e EnvVars.
  • Loading branch information
ThijmenGThN authored Mar 16, 2024
2 parents aabc58a + af47db7 commit 47fedd7
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 25 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/edge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Publish to GitHub Container Registry

on:
push:
branches: [edge]

jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Publish
run: |
docker login --username thijmengthn --password ${{ secrets.GH_PAT }} ghcr.io
docker build . --tag ghcr.io/thijmengthn/swaparr:edge --tag ghcr.io/thijmengthn/swaparr:edge-${{ github.run_number }} --label "org.opencontainers.image.source=https://github.com/ThijmenGThN/swaparr" --label "org.opencontainers.image.description=Unstable release based on the Edge branch." --label "org.opencontainers.image.licenses=MIT"
docker push ghcr.io/thijmengthn/swaparr:edge
docker push ghcr.io/thijmengthn/swaparr:edge-${{ github.run_number }}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ jobs:
- name: Publish
run: |
docker login --username thijmengthn --password ${{ secrets.GH_PAT }} ghcr.io
docker build . --tag ghcr.io/thijmengthn/swaparr:latest --tag ghcr.io/thijmengthn/swaparr:${{ steps.get_version.outputs.VERSION }}
docker build . --tag ghcr.io/thijmengthn/swaparr:latest --tag ghcr.io/thijmengthn/swaparr:${{ steps.get_version.outputs.VERSION }} --label "org.opencontainers.image.source=https://github.com/ThijmenGThN/swaparr" --label "org.opencontainers.image.description=A robust solution addressing stalled torrents in Radarr and Sonarr." --label "org.opencontainers.image.licenses=MIT"
docker push ghcr.io/thijmengthn/swaparr:latest
docker push ghcr.io/thijmengthn/swaparr:${{ steps.get_version.outputs.VERSION }}
19 changes: 11 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ WORKDIR /swaparr
COPY src ./src
COPY Cargo* ./

RUN cargo build --release
RUN apt update
RUN apt install -y libssl-dev musl-tools

RUN rustup target add x86_64-unknown-linux-musl

# ----- Package Stage -----
FROM debian:bookworm-slim
RUN echo "openssl = { version = \"0.10\", features = [\"vendored\"] }" >> Cargo.toml

RUN apt update
RUN apt install -y libssl-dev
RUN apt clean
RUN cargo build --release --target x86_64-unknown-linux-musl


# ----- Package Stage -----
FROM scratch

COPY --from=build /swaparr/target/release/swaparr /usr/local/bin/swaparr
COPY --from=build /swaparr/target/x86_64-unknown-linux-musl/release/swaparr /swaparr

CMD ["sh", "-c", "swaparr"]
CMD ["/swaparr"]
55 changes: 53 additions & 2 deletions src/health.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use reqwest::blocking::get;

use crate::{logger, system};
use crate::{logger, parser, system};

pub fn check(env: &system::Envs) {
// Check if the API can be reached.
Expand All @@ -15,7 +15,7 @@ pub fn check(env: &system::Envs) {
if res.status() != 200 {
logger::alert(
"FATAL",
format!("The provided \"APIKEY\" is not valid."),
"The provided \"APIKEY\" is not valid.".to_string(),
format!(
"Obtain the {} API key in Settings > General > API Key",
env.platform
Expand All @@ -39,4 +39,55 @@ pub fn check(env: &system::Envs) {
system::exit(1);
}
}

// Check if variable TIME_THRESHOLD is able to be parsed.
match parser::string_time_notation_to_ms(&env.time_threshold) {
// Variable can be parsed, thus valid.
Ok(_) => (),
// Variable could not be parsed, throw a fatal.
Err(_) => {
logger::alert(
"FATAL",
"Environment variable \"TIME_THRESHOLD\" is not valid.".to_string(),
"Must be a time-notation: \"1d\", \"6h\", \"30m\", etc.. by default: \"2h\""
.to_string(),
None,
);
system::exit(1);
}
}

// Check if variable SIZE_THRESHOLD is able to be parsed.
match parser::string_bytesize_to_bytes(&env.size_threshold) {
// Variable can be parsed, thus valid.
Ok(_) => (),
// Variable could not be parsed, throw a fatal.
Err(_) => {
logger::alert(
"FATAL",
"Environment variable \"SIZE_THRESHOLD\" is not valid.".to_string(),
"Must be a bytesize-notation: \"1TB\", \"1GB\", \"1MB\", etc.. by default: \"25GB\""
.to_string(),
None,
);
system::exit(1);
}
}

// Check if variable CHECK_INTERVAL is able to be parsed.
match parser::string_time_notation_to_ms(&env.check_interval) {
// Variable can be parsed, thus valid.
Ok(_) => (),
// Variable could not be parsed, throw a fatal.
Err(_) => {
logger::alert(
"FATAL",
"Environment variable \"CHECK_INTERVAL\" is not valid.".to_string(),
"Must be a time-notation: \"1d\", \"6h\", \"30m\", etc.. by default: \"10m\""
.to_string(),
None,
);
system::exit(1);
}
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn main() {

// CHECK_INTERVAL sleeper for the main thread.
sleep(Duration::from_millis(
match parser::string_to_ms(&env.check_interval) {
match parser::string_time_notation_to_ms(&env.check_interval) {
Ok(check_interval_ms) => check_interval_ms as u64,
Err(_) => 10 * 60 * 1000, // Using default, 10 minutes
},
Expand Down
16 changes: 6 additions & 10 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,14 @@ pub fn ms_to_eta_string(ms: &u64) -> String {
}
}

// Converts human-readable string (from radarr or sonarr API) to milliseconds.
pub fn string_to_ms(string: &String) -> Result<i64, ms_converter::Error> {
// Converts human-readable time notation to milliseconds.
pub fn string_time_notation_to_ms(string: &String) -> Result<i64, ms_converter::Error> {
ms_converter::ms(string)
}

// This will convert for example "1 TB", "512 MB", <"1.5 GB" to 1500000 (bytes)>.
pub fn string_bytesize_to_bytes(string: &String) -> u64 {
match string.parse::<ByteSize>() {
Ok(size) => size.as_u64(),
Err(_) => 0,
}
pub fn string_bytesize_to_bytes(string: &String) -> Result<ByteSize, String> {
string.parse::<ByteSize>()
}

// Converts human-readable string (from radarr or sonarr API) to milliseconds.
Expand Down Expand Up @@ -88,7 +85,6 @@ pub fn string_hms_to_ms(string: &String) -> u64 {
_ => return 0,
}

// Calculate total milliseconds
let total_ms = ((days * 24 + hours) * 3600 + minutes * 60 + seconds) * 1000;
total_ms
// Calculate total milliseconds and return.
((days * 24 + hours) * 3600 + minutes * 60 + seconds) * 1000
}
11 changes: 8 additions & 3 deletions src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ pub fn process(queue_items: Vec<Torrent>, strikelist: &mut HashMap<u32, u32>, en
bypass = true;
}

// Torrent is larger than set threshold.
let size_threshold_bytes = parser::string_bytesize_to_bytes(&env.size_threshold);
// Torrent is larger than set threshold. (Safe to unwrap, gets validated in health-check.)
let size_threshold_bytes = parser::string_bytesize_to_bytes(&env.size_threshold)
.unwrap()
.as_u64();
if torrent.size >= size_threshold_bytes {
status = String::from("Ignored");
bypass = true;
Expand All @@ -140,8 +142,11 @@ pub fn process(queue_items: Vec<Torrent>, strikelist: &mut HashMap<u32, u32>, en
// -- Strike rules -- Rules that define when to strike a torrent.

if !bypass {
// Extract timestamp from time notation. (Safe to unwrap, gets validated in health-check.)
let time_threshold_ms =
parser::string_time_notation_to_ms(&env.time_threshold).unwrap() as u64;

// Torrent will take longer than set threshold.
let time_threshold_ms = parser::string_hms_to_ms(&env.time_threshold);
if (torrent.eta >= time_threshold_ms) || (torrent.eta == 0 && env.aggresive_strikes) {
// Increment strikes if it's below set maximum.
if strikes < env.strike_threshold {
Expand Down

0 comments on commit 47fedd7

Please sign in to comment.