Skip to content

Commit

Permalink
Filter jobs by prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
lavrov committed Jan 6, 2024
1 parent 00a0aa9 commit d611867
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ async fn main() -> Result<(), kube::Error> {
}
}

const JOB_PREFIX: &str = "blackhole-torrent";

async fn run(job_api: &Api<Job>, blackhole: &Blackhole) -> Result<(), kube::Error> {
let mut files: Vec<DirEntry> = std::fs::read_dir("torrents").unwrap()
.map(|entry| entry.unwrap())
Expand All @@ -56,11 +58,22 @@ async fn run(job_api: &Api<Job>, blackhole: &Blackhole) -> Result<(), kube::Erro
InfoHashSource::from_file(entry)
}).collect();

let running_jobs: Vec<Job> = job_api.list(&ListParams::default()).await?.items;
let running_jobs: Vec<Job> = job_api.list(&ListParams::default()).await?
.items
.into_iter()
.filter(|job|
job.metadata.name
.iter()
.find(|name|
name.starts_with(JOB_PREFIX)
)
.is_some()
)
.collect();

for source in info_hashes.iter().as_ref() {
let info_hash = &source.info_hash;
let job_name: String = format!("blackhole-torrent-{}", info_hash[0..6].to_owned());
let job_name: String = format!("{}-{}", JOB_PREFIX, info_hash[0..6].to_owned());
let downloading_dir = format!("downloading/{}", source.file_name);
if let Some(job) = running_jobs.iter().find(|job| job.metadata.name.as_ref() == Some(&job_name)) {
if job.status.as_ref().is_some_and(|status| status.succeeded == Some(1)) {
Expand Down Expand Up @@ -168,5 +181,5 @@ impl InfoHashSource {
}

fn info_hash_to_job_name(info_hash: &String) -> String {
format!("blackhole-torrent-{}", info_hash[0..6].to_owned())
format!("{}-{}", JOB_PREFIX, info_hash[0..6].to_owned())
}

0 comments on commit d611867

Please sign in to comment.