use std::sync::Arc;
use std::time::Duration;
use crate::Credential;
#[derive(Clone, Debug)]
pub struct WorkerTarget {
address: String,
headless: bool,
pull: bool,
}
impl WorkerTarget {
pub fn new(address: impl Into<String>) -> Self {
Self {
address: address.into(),
headless: false,
pull: false,
}
}
pub fn pull() -> Self {
Self {
address: String::new(),
headless: false,
pull: true,
}
}
pub fn headless(mut self, value: bool) -> Self {
self.headless = value;
self
}
pub fn address(&self) -> &str {
&self.address
}
pub fn is_headless(&self) -> bool {
self.headless
}
pub fn is_pull(&self) -> bool {
self.pull
}
pub(crate) fn validate(&self) -> Result<(), crate::SdkError> {
if self.pull && (!self.address.is_empty() || self.headless) {
return Err(crate::SdkError::FlowDefinition {
message: "pull mode cannot be combined with address or headless mode".to_string(),
});
}
Ok(())
}
}
#[derive(Clone, Debug, Default)]
pub struct PullWorkerOptions {
pub(crate) max_concurrent_tasks: Option<usize>,
pub(crate) reserved_rpc_tasks: Option<usize>,
pub(crate) idle_streams_per_queue: Option<usize>,
pub(crate) app_worker_id: Option<String>,
pub(crate) stop_drain_timeout: Option<Duration>,
pub(crate) stop_on_permanent_stream_rejection: bool,
}
impl PullWorkerOptions {
pub fn new() -> Self {
Self::default()
}
pub fn max_concurrent_tasks(mut self, value: usize) -> Self {
self.max_concurrent_tasks = Some(value);
self
}
pub fn reserved_rpc_tasks(mut self, value: usize) -> Self {
self.reserved_rpc_tasks = Some(value);
self
}
pub fn idle_streams_per_queue(mut self, value: usize) -> Self {
self.idle_streams_per_queue = Some(value);
self
}
pub fn app_worker_id(mut self, value: impl Into<String>) -> Self {
self.app_worker_id = Some(value.into());
self
}
pub fn stop_on_permanent_stream_rejection(mut self, value: bool) -> Self {
self.stop_on_permanent_stream_rejection = value;
self
}
pub fn stop_drain_timeout(mut self, value: Duration) -> Self {
self.stop_drain_timeout = Some(value);
self
}
}
#[derive(Clone, Debug)]
pub struct WorkerOptions {
bind_address: Option<String>,
server_address: String,
worker_target: Option<WorkerTarget>,
pull: Option<PullWorkerOptions>,
attribute_index_sync_timeout: Duration,
credential: Option<Arc<dyn Credential>>,
}
impl WorkerOptions {
pub fn new() -> Self {
Self {
bind_address: None,
server_address: "127.0.0.1:8801".to_string(),
worker_target: None,
pull: None,
attribute_index_sync_timeout: Duration::from_secs(120),
credential: None,
}
}
pub fn bind_address(mut self, value: impl Into<String>) -> Self {
self.bind_address = Some(value.into());
self
}
pub fn server_address(mut self, value: impl Into<String>) -> Self {
self.server_address = value.into();
self
}
pub fn credential(mut self, value: Arc<dyn Credential>) -> Self {
self.credential = Some(value);
self
}
pub fn worker_target(mut self, value: WorkerTarget) -> Self {
self.worker_target = Some(value);
self
}
pub fn pull(mut self, value: PullWorkerOptions) -> Self {
self.pull = Some(value);
self
}
pub fn attribute_index_sync_timeout(mut self, value: Duration) -> Self {
self.attribute_index_sync_timeout = value;
self
}
pub(crate) fn bind_address_value(&self) -> &str {
self.bind_address.as_deref().unwrap_or("0.0.0.0:8803")
}
pub(crate) fn has_bind_address(&self) -> bool {
self.bind_address.is_some()
}
pub(crate) fn pull_value(&self) -> Option<&PullWorkerOptions> {
self.pull.as_ref()
}
pub(crate) fn server_address_value(&self) -> &str {
&self.server_address
}
pub(crate) fn worker_target_value(&self) -> Option<&WorkerTarget> {
self.worker_target.as_ref()
}
pub(crate) fn attribute_index_sync_timeout_value(&self) -> Duration {
self.attribute_index_sync_timeout
}
pub(crate) fn credential_value(&self) -> Option<Arc<dyn Credential>> {
self.credential.clone()
}
}
impl Default for WorkerOptions {
fn default() -> Self {
Self::new()
}
}