fleche 6.19.2

Remote job runner for Slurm clusters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! Miscellaneous job operations - wait, ping, and stats.

use crate::config::Config;
use crate::error::{FlecheError, Result};
use crate::local;
use crate::ntfy;
use crate::output::OutputFormat;
use crate::registry::{JobStatus, LiveStatus, Registry};
use crate::runtime::{RuntimeCtx, send_notification};
use crate::slurm::{JobResourceUsage, get_job_resource_usage, get_job_status};
use console::style;
use serde::Serialize;
use std::path::PathBuf;
use std::time::Duration;

use super::get_remote_direct_job_status;
use super::job_path_from_workspace;
use super::status::resolve_job;

/// Waits for a job to complete.
///
/// Polls the job status until it reaches a terminal state (completed, failed, cancelled).
/// Poll intervals can be customized via the config settings.
pub async fn wait_for_job(
    job_id: Option<&str>,
    notify: bool,
    ntfy_topic: Option<&str>,
    tags: &[(String, String)],
    format: OutputFormat,
    ctx: RuntimeCtx,
) -> Result<()> {
    let registry = Registry::open()?;
    let job = resolve_job(&registry, job_id, tags, None)?;

    if format.is_human() {
        println!("Waiting for job {}...", style(&job.id).bold());
    }

    // Local job handling
    if job.remote_host == "local" {
        let project_path = PathBuf::from(&job.project_path);
        let mut prev_status: Option<JobStatus> = None;

        loop {
            let live = local::get_local_job_status(&project_path, &job.id)?;
            registry.update_status(&job.id, &live)?;

            if let Some(topic) = ntfy_topic {
                ntfy::notify_state_change(
                    topic,
                    &job.id,
                    prev_status,
                    live.status,
                    job.note.as_deref(),
                );
                prev_status = Some(live.status);
            }

            if is_terminal(live.status) {
                if ctx.should_notify(notify) {
                    send_notification(&format_terminal_message(&job.id, &live));
                }
                return print_wait_result(&registry, &job.id, &live, format);
            }

            tokio::time::sleep(Duration::from_secs(ctx.poll_interval_local_secs)).await;
        }
    }

    // Remote job handling
    let ssh = ctx.ssh(&job.remote_host);
    let mut prev_status: Option<JobStatus> = None;

    loop {
        let live = if let Some(ref slurm_id) = job.slurm_id {
            get_job_status(&ssh, slurm_id).await?
        } else {
            let job_dir = job_path_from_workspace(&job.remote_path, &job.id);
            get_remote_direct_job_status(&ssh, &job_dir).await?
        };

        registry.update_status(&job.id, &live)?;

        if let Some(topic) = ntfy_topic {
            ntfy::notify_state_change(
                topic,
                &job.id,
                prev_status,
                live.status,
                job.note.as_deref(),
            );
            prev_status = Some(live.status);
        }

        if is_terminal(live.status) {
            if ctx.should_notify(notify) {
                send_notification(&format_terminal_message(&job.id, &live));
            }
            return print_wait_result(&registry, &job.id, &live, format);
        }

        tokio::time::sleep(Duration::from_secs(ctx.poll_interval_remote_secs)).await;
    }
}

/// Returns whether a job status is terminal.
fn is_terminal(status: JobStatus) -> bool {
    matches!(
        status,
        JobStatus::Completed | JobStatus::Failed | JobStatus::Cancelled
    )
}

/// Builds the terminal status message string (used for both display and notifications).
fn format_terminal_message(job_id: &str, live: &LiveStatus) -> String {
    match live.status {
        JobStatus::Completed => format!("Job {job_id} completed successfully."),
        JobStatus::Failed => {
            let exit_str = live
                .sacct_exit_code
                .as_deref()
                .map(String::from)
                .or_else(|| live.exit_code.map(|c| c.to_string()));
            let detail = match (&live.slurm_state, &exit_str) {
                (Some(state), Some(code)) => format!(" ({state}, exit code: {code})"),
                (Some(state), None) => format!(" ({state})"),
                (None, Some(code)) => format!(" (exit code: {code})"),
                (None, None) => String::new(),
            };
            format!("Job {job_id} failed{detail}.")
        }
        JobStatus::Cancelled => format!("Job {job_id} was cancelled."),
        _ => String::new(),
    }
}

/// Prints the wait result: styled human message or JSON job record.
fn print_wait_result(
    registry: &Registry,
    job_id: &str,
    live: &LiveStatus,
    format: OutputFormat,
) -> Result<()> {
    let job = registry.get_job(job_id)?;
    format.print(&job, || {
        let msg = format_terminal_message(job_id, live);
        match live.status {
            JobStatus::Completed => println!("{}", style(&msg).green().bold()),
            JobStatus::Failed => println!("{}", style(&msg).red().bold()),
            JobStatus::Cancelled => println!("{}", style(&msg).yellow().bold()),
            _ => {}
        }
        Ok(())
    })
}

/// Pings the Slurm controller to check cluster health.
///
/// Runs `scontrol ping` on the remote host and reports the status of the
/// Slurm controller(s). Useful for diagnosing timeout issues.
pub async fn ping_cluster(config: &Config, ctx: RuntimeCtx) -> Result<()> {
    let ssh = ctx.ssh(&config.remote.host);

    println!(
        "Pinging Slurm controller on {}...",
        style(&config.remote.host).bold()
    );
    println!();

    let (success, stdout, stderr) = ssh.exec_allow_failure("scontrol ping").await?;

    if success {
        // Parse and display the output
        for line in stdout.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }
            // Color-code UP/DOWN status
            if line.contains("is UP") {
                println!("{}", style(line).green());
            } else if line.contains("is DOWN") {
                println!("{}", style(line).red());
            } else {
                println!("{line}");
            }
        }
        println!();

        if stdout.contains("is DOWN") {
            println!(
                "{}",
                style("Warning: One or more controllers are down. Jobs may be slow or fail.")
                    .yellow()
            );
        } else {
            println!("{}", style("Cluster is healthy.").green().bold());
        }
    } else {
        // scontrol ping failed entirely
        eprintln!("{}", style("Failed to ping Slurm controller.").red());
        if !stderr.is_empty() {
            eprintln!("{stderr}");
        }
        return Err(FlecheError::SlurmUnavailable);
    }

    Ok(())
}

/// Resource usage statistics for a single job, for JSON output.
#[derive(Serialize)]
struct JobStatsEntry {
    id: String,
    slurm_id: String,
    status: JobStatus,
    #[serde(flatten)]
    usage: Option<JobResourceUsage>,
    #[serde(skip_serializing_if = "Option::is_none")]
    error: Option<String>,
}

/// Shows resource usage statistics for completed jobs.
///
/// Queries Slurm's sacct to display elapsed time, CPU time, memory usage,
/// and allocated resources for jobs.
pub async fn show_stats(
    job_id: Option<&str>,
    last: usize,
    tags: &[(String, String)],
    format: OutputFormat,
    ctx: RuntimeCtx,
) -> Result<()> {
    let registry = Registry::open()?;

    let jobs = if let Some(id) = job_id {
        vec![registry.get_job(id)?]
    } else {
        registry.list_jobs_by_tags(tags, last)?
    };

    if jobs.is_empty() {
        let empty: Vec<JobStatsEntry> = Vec::new();
        return format.print(&empty, || {
            println!("No jobs found.");
            Ok(())
        });
    }

    // Filter to remote jobs with slurm IDs (local jobs don't have sacct stats)
    let remote_jobs: Vec<_> = jobs
        .iter()
        .filter(|j| j.remote_host != "local" && j.slurm_id.is_some())
        .collect();

    if remote_jobs.is_empty() {
        let empty: Vec<JobStatsEntry> = Vec::new();
        return format.print(&empty, || {
            println!("No remote Slurm jobs found. Stats are only available for Slurm jobs.");
            Ok(())
        });
    }

    let mut results: Vec<JobStatsEntry> = Vec::new();

    for job in remote_jobs {
        let slurm_id = job
            .slurm_id
            .as_ref()
            .expect("already filtered to jobs with slurm_id");
        let ssh = ctx.ssh(&job.remote_host);

        match get_job_resource_usage(&ssh, slurm_id).await {
            Ok(usage) => {
                results.push(JobStatsEntry {
                    id: job.id.clone(),
                    slurm_id: slurm_id.clone(),
                    status: job.status,
                    usage: Some(usage),
                    error: None,
                });
            }
            Err(e) => {
                results.push(JobStatsEntry {
                    id: job.id.clone(),
                    slurm_id: slurm_id.clone(),
                    status: job.status,
                    usage: None,
                    error: Some(e.to_string()),
                });
            }
        }
    }

    format.print(&results, || {
        print_stats_table(&results);
        Ok(())
    })
}

/// Prints a human-readable stats table.
fn print_stats_table(results: &[JobStatsEntry]) {
    println!(
        "{:<12} {:<10} {:<12} {:<12} {:<10} {:<16} {}",
        style("JOB ID").bold(),
        style("STATUS").bold(),
        style("ELAPSED").bold(),
        style("CPU TIME").bold(),
        style("MAX MEM").bold(),
        style("NODE").bold(),
        style("RESOURCES").bold()
    );
    println!("{}", "-".repeat(96));

    for entry in results {
        if let Some(ref usage) = entry.usage {
            let status_styled = match entry.status {
                JobStatus::Completed => style(entry.status.to_string()).green(),
                JobStatus::Failed => style(entry.status.to_string()).red(),
                JobStatus::Cancelled => style(entry.status.to_string()).yellow(),
                JobStatus::Running => style(entry.status.to_string()).cyan(),
                JobStatus::Pending => style(entry.status.to_string()).dim(),
            };

            let resources = parse_alloc_tres(&usage.alloc_tres);
            let elapsed = if usage.elapsed.is_empty() {
                "-"
            } else {
                &usage.elapsed
            };
            let total_cpu = if usage.total_cpu.is_empty() {
                "-"
            } else {
                &usage.total_cpu
            };
            let max_rss = if usage.max_rss.is_empty() {
                "-"
            } else {
                &usage.max_rss
            };
            let node = if usage.node_list.is_empty() {
                "-"
            } else {
                &usage.node_list
            };

            println!(
                "{:<12} {:<10} {:<12} {:<12} {:<10} {:<16} {}",
                truncate_id(&entry.id),
                status_styled,
                elapsed,
                total_cpu,
                max_rss,
                node,
                resources
            );
        } else {
            eprintln!(
                "{:<12} {} ({})",
                truncate_id(&entry.id),
                style("error").red(),
                entry.error.as_deref().unwrap_or("unknown error")
            );
        }
    }
}

// --- Private helper functions ---

/// Truncates a job ID for display (shows first 10 chars).
fn truncate_id(id: &str) -> &str {
    if id.len() <= 10 { id } else { &id[..10] }
}

/// Parses the `AllocTRES` string into a human-readable format.
///
/// Input: "billing=8,cpu=4,gres/gpu=1,mem=16G,node=1"
/// Output: "4 CPU, 1 GPU, 16G mem"
pub fn parse_alloc_tres(tres: &str) -> String {
    if tres.is_empty() {
        return "-".to_string();
    }

    let mut cpus = None;
    let mut gpus = None;
    let mut mem = None;

    for part in tres.split(',') {
        let mut kv = part.splitn(2, '=');
        let key = kv.next().unwrap_or("");
        let value = kv.next().unwrap_or("");

        match key {
            "cpu" => cpus = Some(value.to_string()),
            "gres/gpu" => gpus = Some(value.to_string()),
            "mem" => mem = Some(value.to_string()),
            _ => {}
        }
    }

    let mut parts = Vec::new();
    if let Some(c) = cpus {
        parts.push(format!("{c} CPU"));
    }
    if let Some(g) = gpus {
        if g != "0" {
            parts.push(format!("{g} GPU"));
        }
    }
    if let Some(m) = mem {
        parts.push(format!("{m} mem"));
    }

    if parts.is_empty() {
        "-".to_string()
    } else {
        parts.join(", ")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_alloc_tres_cpu_mem_node() {
        // Real sacct output from job without GPU
        let tres = "cpu=1,mem=1000M,node=1";
        let result = parse_alloc_tres(tres);
        assert_eq!(result, "1 CPU, 1000M mem");
    }

    #[test]
    fn test_parse_alloc_tres_with_gpu() {
        // Real sacct output from GPU job
        let tres = "cpu=1,gres/gpu=1,node=1";
        let result = parse_alloc_tres(tres);
        assert_eq!(result, "1 CPU, 1 GPU");
    }

    #[test]
    fn test_parse_alloc_tres_full() {
        // Full format with billing
        let tres = "billing=8,cpu=4,gres/gpu=1,mem=16G,node=1";
        let result = parse_alloc_tres(tres);
        assert_eq!(result, "4 CPU, 1 GPU, 16G mem");
    }

    #[test]
    fn test_parse_alloc_tres_empty() {
        assert_eq!(parse_alloc_tres(""), "-");
    }

    #[test]
    fn test_parse_alloc_tres_zero_gpu() {
        // GPU=0 should be omitted from output
        let tres = "cpu=2,gres/gpu=0,mem=8G,node=1";
        let result = parse_alloc_tres(tres);
        assert_eq!(result, "2 CPU, 8G mem");
    }

    #[test]
    fn test_truncate_id_short() {
        assert_eq!(truncate_id("abc123"), "abc123");
    }

    #[test]
    fn test_truncate_id_exact() {
        assert_eq!(truncate_id("1234567890"), "1234567890");
    }

    #[test]
    fn test_truncate_id_long() {
        assert_eq!(truncate_id("train-abc12345-xyz"), "train-abc1");
    }
}