use super::{FactoryMessage, Job, JobKey, JobOptions, UpdateSettingsRequest};
use crate::concurrency::Duration;
use crate::rpc::CallResult;
use crate::{ActorRef, Message, MessagingErr, RpcReplyPort};
pub type FactoryRef<TKey, TMsg> = ActorRef<FactoryMessage<TKey, TMsg>>;
pub type FactoryMessagingErr<TKey, TMsg> = MessagingErr<FactoryMessage<TKey, TMsg>>;
pub type FactorySendResult<TKey, TMsg> = Result<(), Box<FactoryMessagingErr<TKey, TMsg>>>;
impl<TKey, TMsg> ActorRef<FactoryMessage<TKey, TMsg>>
where
TKey: JobKey,
TMsg: Message,
{
pub fn dispatch(&self, key: TKey, message: TMsg) -> FactorySendResult<TKey, TMsg> {
self.dispatch_job(Job::new(key, message))
}
pub fn dispatch_with_options(
&self,
key: TKey,
message: TMsg,
options: JobOptions,
) -> FactorySendResult<TKey, TMsg> {
self.dispatch_job(Job::with_options(key, message, options))
}
pub fn dispatch_job(&self, job: Job<TKey, TMsg>) -> FactorySendResult<TKey, TMsg> {
Ok(self.cast(FactoryMessage::Dispatch(job))?)
}
pub async fn call_job<TReply, TMessageBuilder>(
&self,
key: TKey,
message_builder: TMessageBuilder,
timeout: Option<Duration>,
) -> Result<CallResult<TReply>, FactoryMessagingErr<TKey, TMsg>>
where
TReply: Send + 'static,
TMessageBuilder: FnOnce(RpcReplyPort<TReply>) -> TMsg,
{
self.call(
|reply| FactoryMessage::Dispatch(Job::new(key, message_builder(reply))),
timeout,
)
.await
}
pub async fn call_job_with_options<TReply, TMessageBuilder>(
&self,
key: TKey,
message_builder: TMessageBuilder,
options: JobOptions,
timeout: Option<Duration>,
) -> Result<CallResult<TReply>, FactoryMessagingErr<TKey, TMsg>>
where
TReply: Send + 'static,
TMessageBuilder: FnOnce(RpcReplyPort<TReply>) -> TMsg,
{
self.call(
|reply| {
FactoryMessage::Dispatch(Job::with_options(key, message_builder(reply), options))
},
timeout,
)
.await
}
pub async fn queue_depth(
&self,
timeout: Option<Duration>,
) -> Result<CallResult<usize>, FactoryMessagingErr<TKey, TMsg>> {
self.call(FactoryMessage::GetQueueDepth, timeout).await
}
pub async fn available_capacity(
&self,
timeout: Option<Duration>,
) -> Result<CallResult<usize>, FactoryMessagingErr<TKey, TMsg>> {
self.call(FactoryMessage::GetAvailableCapacity, timeout)
.await
}
pub async fn active_workers(
&self,
timeout: Option<Duration>,
) -> Result<CallResult<usize>, FactoryMessagingErr<TKey, TMsg>> {
self.call(FactoryMessage::GetNumActiveWorkers, timeout)
.await
}
pub fn adjust_worker_pool(&self, worker_count: usize) -> FactorySendResult<TKey, TMsg> {
Ok(self.cast(FactoryMessage::AdjustWorkerPool(worker_count))?)
}
pub fn drain_requests(&self) -> FactorySendResult<TKey, TMsg> {
Ok(self.cast(FactoryMessage::DrainRequests)?)
}
pub fn update_settings(
&self,
settings: UpdateSettingsRequest<TKey, TMsg>,
) -> FactorySendResult<TKey, TMsg> {
Ok(self.cast(FactoryMessage::UpdateSettings(settings))?)
}
}