use tracing::debug;
use crate::runtime::{ActorType, SystemEvent}; use crate::{Context, ZmqError};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
pub(crate) struct ActorDropGuard {
context: Context,
handle_id: usize,
actor_type: ActorType,
parent_id: Option<usize>,
endpoint_uri: Option<String>,
stopped_normally: bool,
error: Option<ZmqError>,
}
impl ActorDropGuard {
pub fn new(context: Context, handle_id: usize, actor_type: ActorType, endpoint_uri: Option<String>, parent_handle_id: Option<usize>) -> Self {
context.publish_actor_started(handle_id, actor_type, parent_handle_id);
Self {
context,
handle_id,
parent_id: parent_handle_id,
actor_type,
endpoint_uri,
stopped_normally: false,
error: None,
}
}
pub fn set_error(&mut self, error: ZmqError) {
self.error = Some(error);
}
pub fn waive(&mut self) {
self.stopped_normally = true;
}
}
impl Drop for ActorDropGuard {
fn drop(&mut self) {
let error;
if !self.stopped_normally {
error = self.error.take().or_else(|| Some(ZmqError::Internal("Actor task cancelled/aborted".into())));
debug!(
"ActorDropGuard: Actor {} ({:?}) stopping abnormally (error: {}). Publishing stop.",
self.handle_id, self.actor_type, error.as_ref().unwrap(),
);
} else {
error = None;
}
self.context.publish_actor_stopping(
self.handle_id,
self.actor_type,
self.parent_id,
self.endpoint_uri.take(),
error,
);
}
}