use std::sync::Arc;
use tokio::sync::oneshot;
use crate::{
RuntimeError,
controller::spec::ControllerSpec,
core::{OutcomeTx, RemovalCompletion},
identity::TaskId,
};
pub(super) enum ControllerCommand {
Submit(Submission),
ManageIdentity {
id: TaskId,
operation: IdentityOperation,
reply: oneshot::Sender<Result<bool, RuntimeError>>,
},
}
#[derive(Clone, Copy, Debug)]
pub(super) enum IdentityOperation {
Remove,
TryRemove,
Cancel,
TryCancel,
CancelWithTimeout(std::time::Duration),
TryCancelWithTimeout(std::time::Duration),
}
impl IdentityOperation {
pub(super) fn request_reason(self) -> Option<&'static str> {
match self {
Self::Remove | Self::TryRemove => None,
Self::Cancel
| Self::TryCancel
| Self::CancelWithTimeout(_)
| Self::TryCancelWithTimeout(_) => Some("manual_cancel"),
}
}
}
pub(super) struct IdentityReply {
sender: Option<oneshot::Sender<Result<bool, RuntimeError>>>,
}
impl IdentityReply {
pub(super) fn new(sender: oneshot::Sender<Result<bool, RuntimeError>>) -> Self {
Self {
sender: Some(sender),
}
}
pub(super) fn send(mut self, result: Result<bool, RuntimeError>) {
if let Some(sender) = self.sender.take() {
let _ = sender.send(result);
}
}
}
impl Drop for IdentityReply {
fn drop(&mut self) {
if let Some(sender) = self.sender.take() {
let _ = sender.send(Err(RuntimeError::ShuttingDown));
}
}
}
pub(super) struct Submission {
pub(super) id: TaskId,
pub(super) spec: ControllerSpec,
pub(super) done: Option<OutcomeTx>,
}
pub(super) struct AdmissionResult {
pub(super) id: TaskId,
pub(super) slot_name: Arc<str>,
pub(super) decision: Result<RemovalCompletion, RuntimeError>,
}
pub(super) struct CompletionResult {
pub(super) id: TaskId,
pub(super) slot_name: Arc<str>,
}
pub(super) struct RemovalResult {
pub(super) id: TaskId,
pub(super) slot_name: Arc<str>,
pub(super) decision: Result<bool, RuntimeError>,
}