Source commit: e570b80c3a0f4b152a9aa0e7bb8635b3eebcec27 Public tree identity: sha256:cff3388143eb2e9476804cfa1c1246e3a46d485d8aef90f2cdd4df45f2b69abf
47 lines
1.4 KiB
Rust
47 lines
1.4 KiB
Rust
use std::time::{Duration, Instant};
|
|
|
|
use serde_json::{json, Value};
|
|
|
|
use crate::coordinator_session::CoordinatorSession;
|
|
use crate::daemon::{Args, RuntimeTask};
|
|
use crate::node_identity::signed_node_request_json;
|
|
|
|
pub(crate) fn poll_task_cancellation(
|
|
session: &mut CoordinatorSession,
|
|
args: &Args,
|
|
task: &RuntimeTask,
|
|
node_private_key: &str,
|
|
) -> Result<bool, Box<dyn std::error::Error>> {
|
|
let deadline = Instant::now() + Duration::from_millis(args.control_poll_ms);
|
|
loop {
|
|
let control = session.request(signed_node_request_json(
|
|
args,
|
|
node_private_key,
|
|
"poll_task_control",
|
|
json!({
|
|
"type": "poll_task_control",
|
|
"tenant": &args.tenant,
|
|
"project": &args.project,
|
|
"process": &task.process,
|
|
"node": &args.node,
|
|
"task": &task.task,
|
|
}),
|
|
)?)?;
|
|
if control
|
|
.get("cancel_requested")
|
|
.and_then(Value::as_bool)
|
|
.unwrap_or(false)
|
|
|| control
|
|
.get("abort_requested")
|
|
.and_then(Value::as_bool)
|
|
.unwrap_or(false)
|
|
{
|
|
return Ok(true);
|
|
}
|
|
let now = Instant::now();
|
|
if now >= deadline {
|
|
return Ok(false);
|
|
}
|
|
std::thread::sleep((deadline - now).min(Duration::from_millis(100)));
|
|
}
|
|
}
|