Skip to content

Commit

Permalink
Fix #97
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars T Hansen committed Aug 21, 2023
1 parent 4782876 commit aa108d7
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/slurm.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Abstraction of jobs::JobManager for SLURM.

use crate::command;
use crate::jobs;
use crate::process;

use std::fs::File;
use std::io::{BufRead,BufReader};

pub struct SlurmJobManager {}

impl jobs::JobManager for SlurmJobManager {
Expand All @@ -14,21 +16,31 @@ impl jobs::JobManager for SlurmJobManager {
}

fn get_slurm_job_id(pid: usize) -> Option<String> {
let path = format!("/proc/{}/cgroup", pid);

if !std::path::Path::new(&path).exists() {
return None;
}
match File::open(format!("/proc/{pid}/cgroup")) {
Ok(f) => {

let command = format!(
"cat /proc/{}/cgroup | grep -oP '(?<=job_).*?(?=/)' | head -n 1",
pid
);
let timeout_seconds = 2;
// We want \1 of the first line that matches "/job_(.*?)/"
//
// The reason is that there are several lines in that file that look roughly like this,
// with different contents (except for the job info) but with the pattern the same:
//
// 10:devices:/slurm/uid_2101171/job_280678/step_interactive/task_0

// TODO: Maybe propagate an error here?
match command::safe_command(&command, timeout_seconds) {
Ok(s) => Some(s),
Err(_) => None,
for l in BufReader::new(f).lines() {
if let Ok(l) = l {
if let Some(x) = l.find("/job_") {
if let Some(y) = l[x+5..].find('/') {
return Some(l[x+5..x+5+y].to_string())
}
}
} else {
return None;
}
}
return None;
}
Err(_) => {
return None;
}
}
}

0 comments on commit aa108d7

Please sign in to comment.