use super::store::{Job, State};
use super::{spec::JobSpec, SubmitError};
use crate::agent::core::Core;
use axum::extract::rejection::JsonRejection;
use axum::extract::{Path, State as AxState};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use axum::{Json, Router};
use std::sync::Arc;
pub fn routes() -> Router<Arc<Core>> {
Router::new()
.route(
"/v2/submissions",
post(post_submission).get(list_submissions),
)
.route(
"/v2/submissions/:id",
get(get_submission).delete(delete_submission),
)
}
fn error(status: u16, code: &str, message: &str) -> Response {
(
StatusCode::from_u16(status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
Json(serde_json::json!({ "error": { "code": code, "message": message } })),
)
.into_response()
}
fn announce(core: &Core, job: &Job) {
core.events.publish(
"submission.state",
job.summary(core.outbox.store.cancel_requested(&job.id)),
);
}
async fn post_submission(
AxState(core): AxState<Arc<Core>>,
body: Result<Json<JobSpec>, JsonRejection>,
) -> Response {
let spec = match body {
Ok(Json(s)) => s,
Err(e) => return error(400, "invalid_input", &e.body_text()),
};
let c = core.clone();
let res = tokio::task::spawn_blocking(move || c.outbox.submit(spec)).await;
match res {
Ok(Ok((job, duplicate))) => {
if !duplicate {
announce(&core, &job);
}
(
StatusCode::ACCEPTED,
Json(serde_json::json!({
"id": job.id,
"state": job.state.as_str(),
"duplicate": duplicate,
"submission": job.summary(false),
})),
)
.into_response()
}
Ok(Err(SubmitError::Spec(e))) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!({ "error": {
"code": "invalid_input",
"message": e.to_string(),
"field": e.field,
} })),
)
.into_response(),
Ok(Err(SubmitError::Io(m))) => error(500, "outbox_error", &m),
Err(e) => error(500, "outbox_error", &e.to_string()),
}
}
async fn list_submissions(AxState(core): AxState<Arc<Core>>) -> Response {
let store = &core.outbox.store;
let mut jobs = store.list();
jobs.reverse();
let rows: Vec<_> = jobs
.iter()
.map(|j| j.summary(store.cancel_requested(&j.id)))
.collect();
Json(serde_json::json!({ "submissions": rows })).into_response()
}
async fn get_submission(AxState(core): AxState<Arc<Core>>, Path(id): Path<String>) -> Response {
let store = &core.outbox.store;
match store.load(&id) {
Some(j) => {
Json(j.detail(store.cancel_requested(&id), store.results_root())).into_response()
}
None => error(404, "not_found", "no such submission"),
}
}
async fn delete_submission(AxState(core): AxState<Arc<Core>>, Path(id): Path<String>) -> Response {
let store = &core.outbox.store;
let Some(job) = store.load(&id) else {
return error(404, "not_found", "no such submission");
};
if job.state.terminal() {
return error(
409,
"conflict",
&format!("the submission is already {}", job.state.as_str()),
);
}
if job.state == State::Collecting {
return error(
409,
"conflict",
"the work is finished and its results are being collected; nothing is left to cancel",
);
}
if let Err(e) = store.request_cancel(&id) {
return error(500, "outbox_error", &e.to_string());
}
core.outbox.wake.notify_one();
announce(&core, &job);
(
StatusCode::ACCEPTED,
Json(serde_json::json!({
"id": id,
"state": job.state.as_str(),
"cancel_requested": true,
})),
)
.into_response()
}
#[cfg(test)]
mod tests {
use super::super::spec::{sample_spec, test_dir};
use super::{Arc, Core};
use crate::agent::core::tests::core_with;
use crate::agent::server::router;
use std::time::Duration;
fn serve(core: Arc<Core>, token: &str) -> u16 {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
listener.set_nonblocking(true).unwrap();
let app = router(core, token.to_string(), port);
std::thread::spawn(move || {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
rt.block_on(async move {
let l = tokio::net::TcpListener::from_std(listener).unwrap();
axum::serve(l, app).await.unwrap();
});
});
port
}
fn call(
method: &str,
port: u16,
path: &str,
token: Option<&str>,
body: Option<&serde_json::Value>,
) -> (u16, serde_json::Value) {
let agent = ureq::Agent::new_with_config(
ureq::Agent::config_builder()
.http_status_as_error(false)
.timeout_global(Some(Duration::from_secs(10)))
.build(),
);
let url = format!("http://127.0.0.1:{port}{path}");
let auth = token.map(|t| format!("Bearer {t}")).unwrap_or_default();
let resp = match (method, body) {
("GET", _) => agent
.get(&url)
.header("Authorization", auth.as_str())
.call(),
("DELETE", _) => agent
.delete(&url)
.header("Authorization", auth.as_str())
.call(),
(_, Some(b)) => agent
.post(&url)
.header("Authorization", auth.as_str())
.send_json(b),
(_, None) => agent
.post(&url)
.header("Authorization", auth.as_str())
.send_empty(),
}
.unwrap();
let status = resp.status().as_u16();
(
status,
resp.into_body()
.read_json()
.unwrap_or(serde_json::Value::Null),
)
}
#[test]
fn submissions_are_guarded_validated_stored_listed_and_cancelled() {
let core = core_with(0);
let port = serve(core.clone(), "tok");
let inputs = test_dir("routes-in");
let spec = serde_json::to_value(sample_spec(&inputs)).unwrap();
assert_eq!(
call("POST", port, "/v2/submissions", None, Some(&spec)).0,
401
);
assert_eq!(call("GET", port, "/v2/submissions", None, None).0, 401);
let mut bad = spec.clone();
bad["max_price_per_batch"] = serde_json::json!(0);
let (st, v) = call("POST", port, "/v2/submissions", Some("tok"), Some(&bad));
assert_eq!(st, 400);
assert_eq!(v["error"]["code"], "invalid_input");
assert_eq!(v["error"]["field"], "max_price_per_batch");
let (_, v) = call("GET", port, "/v2/submissions", Some("tok"), None);
assert_eq!(v["submissions"].as_array().unwrap().len(), 0);
let (st, _) = call(
"POST",
port,
"/v2/submissions",
Some("tok"),
Some(&serde_json::json!({"nope": 1})),
);
assert_eq!(st, 400);
let before = core.events.last_id();
let (st, v) = call("POST", port, "/v2/submissions", Some("tok"), Some(&spec));
assert_eq!(st, 202, "{v}");
let id = v["id"].as_str().unwrap().to_string();
assert_eq!(v["state"], "queued");
assert!(core.outbox.store.load(&id).is_some());
assert!(
core.events.last_id() > before,
"submission.state was published"
);
let (st, v) = call(
"GET",
port,
&format!("/v2/submissions/{id}"),
Some("tok"),
None,
);
assert_eq!(st, 200);
assert_eq!(v["batches"], 2);
assert_eq!(v["max_price_per_batch"], 50.0);
let (_, v) = call("GET", port, "/v2/submissions", Some("tok"), None);
assert_eq!(v["submissions"][0]["id"], id.as_str());
assert_eq!(
call(
"GET",
port,
"/v2/submissions/j_000000000000",
Some("tok"),
None
)
.0,
404
);
assert_eq!(
call("GET", port, "/v2/submissions/..%2Fx", Some("tok"), None).0,
404
);
let (st, v) = call(
"DELETE",
port,
&format!("/v2/submissions/{id}"),
Some("tok"),
None,
);
assert_eq!(
(st, v["cancel_requested"].clone()),
(202, serde_json::json!(true))
);
assert!(core.outbox.store.cancel_requested(&id));
assert_eq!(
call(
"DELETE",
port,
"/v2/submissions/j_000000000000",
Some("tok"),
None
)
.0,
404
);
let mut j = core.outbox.store.load(&id).unwrap();
j.state = super::State::Done;
core.outbox.store.save(&j).unwrap();
assert_eq!(
call(
"DELETE",
port,
&format!("/v2/submissions/{id}"),
Some("tok"),
None
)
.0,
409
);
}
#[test]
fn a_repeated_request_id_returns_the_job_already_made() {
let core = core_with(0);
let port = serve(core.clone(), "tok");
let inputs = test_dir("routes-rid");
let mut s = sample_spec(&inputs);
s.request_id = Some("once".into());
let spec = serde_json::to_value(&s).unwrap();
let (_, a) = call("POST", port, "/v2/submissions", Some("tok"), Some(&spec));
let (_, b) = call("POST", port, "/v2/submissions", Some("tok"), Some(&spec));
assert_eq!(a["id"], b["id"]);
assert_eq!(
(a["duplicate"].clone(), b["duplicate"].clone()),
(serde_json::json!(false), serde_json::json!(true))
);
assert_eq!(core.outbox.store.list().len(), 1);
}
#[test]
fn the_cli_submits_lists_and_cancels_through_the_agent() {
use crate::agent::client::AgentClient;
use crate::agent::files::{save_json, AgentFile, AGENT_FILE};
use crate::agent::outbox::cli::{execute, Cmd};
let core = core_with(0);
let port = serve(core.clone(), "tok");
let dir = test_dir("routes-cli");
save_json(
&dir.join(AGENT_FILE),
&AgentFile {
v: 1,
port,
token: "tok".into(),
pid: 1,
version: "t".into(),
rev: 0,
},
)
.unwrap();
let client = AgentClient::from_dir(&dir).unwrap();
let specs = test_dir("routes-cli-spec");
std::fs::create_dir_all(specs.join("data")).unwrap();
std::fs::write(specs.join("data/p.txt"), "hello").unwrap();
let file = specs.join("job.toml");
std::fs::write(
&file,
"name='cli'\nimage='r.io/a:1'\nmax_price_per_batch=7\n[resources]\ncpus=1\nmemory_gb=1\nmax_runtime_s=60\n[[batch]]\ninputs={ 'p.txt' = 'data/p.txt' }\n",
)
.unwrap();
let sub = Cmd::Submit {
spec: file.display().to_string(),
};
assert_eq!(execute(&client, &dir, sub, true), 0);
let jobs = core.outbox.store.list();
assert_eq!(jobs.len(), 1);
assert!(
jobs[0].files[0].path.ends_with("data/p.txt") && jobs[0].files[0].path.starts_with('/')
);
let id = jobs[0].id.clone();
assert_eq!(execute(&client, &dir, Cmd::Ls, false), 0);
assert_eq!(
execute(&client, &dir, Cmd::Status { id: id.clone() }, false),
0
);
assert_eq!(
execute(
&client,
&dir,
Cmd::Status {
id: "j_000000000000".into()
},
false
),
1
);
assert_eq!(
execute(
&client,
&dir,
Cmd::Results {
id: id.clone(),
out: None
},
false
),
1,
"no results yet"
);
assert_eq!(
execute(&client, &dir, Cmd::Cancel { id: id.clone() }, false),
0
);
assert!(core.outbox.store.cancel_requested(&id));
std::fs::write(
&file,
std::fs::read_to_string(&file).unwrap().replace("=7", "=0"),
)
.unwrap();
assert_eq!(
execute(
&client,
&dir,
Cmd::Submit {
spec: file.display().to_string()
},
false
),
1
);
}
}