Implement Trivy adapter, contain job panics, fail interrupted runs on startup
The Trivy scanner still had unimplemented stubs, which panicked the scan task in the deployed test instance and left the run in 'running' forever. JobRunner now runs handlers in their own task and marks a panic as a failed run; on startup runs left 'running' by a previous process are marked failed. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@ -7,6 +7,7 @@ use uuid::Uuid;
|
||||
|
||||
use crate::jobs::{JobHandler, JobLog, JobRunner};
|
||||
use crate::test_fakes::MemJobRuns;
|
||||
use domain::ports::JobRunRepository;
|
||||
|
||||
struct Echo;
|
||||
#[async_trait]
|
||||
@ -102,3 +103,48 @@ async fn list_returns_newest_first_with_limit() {
|
||||
assert_eq!(list.len(), 2);
|
||||
assert!(list[0].started_at >= list[1].started_at);
|
||||
}
|
||||
|
||||
struct Panics;
|
||||
#[async_trait]
|
||||
impl JobHandler for Panics {
|
||||
async fn run(&self, _: Option<String>, log: &dyn JobLog) -> Result<(), String> {
|
||||
log.line("about to panic").await;
|
||||
panic!("handler bug");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn panicking_handler_marks_run_failed() {
|
||||
let runs = Arc::new(MemJobRuns::default());
|
||||
let r = JobRunner::new(runs.clone()).register(JobKind::Backup, Arc::new(Panics));
|
||||
let run = r.start(JobKind::Backup, None, "test").await.unwrap();
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
let run = r.get(run.id).await.unwrap();
|
||||
assert_eq!(run.status, JobStatus::Failed);
|
||||
assert!(run.log.contains("panicked"), "{}", run.log);
|
||||
// the kind is free again
|
||||
assert!(r.start(JobKind::Backup, None, "test").await.is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn stale_running_runs_are_failed_on_recovery() {
|
||||
let runs = Arc::new(MemJobRuns::default());
|
||||
runs.insert(&domain::jobs::JobRun {
|
||||
id: Uuid::new_v4(),
|
||||
kind: JobKind::PackageRefresh,
|
||||
params: None,
|
||||
status: JobStatus::Running,
|
||||
started_at: chrono::Utc::now(),
|
||||
finished_at: None,
|
||||
log: String::new(),
|
||||
triggered_by: "old process".into(),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let r = JobRunner::new(runs.clone()).register(JobKind::PackageRefresh, Arc::new(Echo));
|
||||
assert_eq!(r.recover().await.unwrap(), 1);
|
||||
let list = r.list(10).await.unwrap();
|
||||
assert_eq!(list[0].status, JobStatus::Failed);
|
||||
assert!(list[0].log.contains("interrupted"));
|
||||
assert!(r.start(JobKind::PackageRefresh, None, "test").await.is_ok());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user