use std::sync::Arc;
use std::time::Duration;
#[derive(Clone)]
#[non_exhaustive]
pub struct ClientConfig {
pub server_url: String,
pub namespace: String,
pub timeout: Duration,
pub identity: String,
pub use_payload_format: bool,
pub data_converter: Option<crate::client::client::DataConverterType>,
pub codec_chain: Option<Arc<orcher_sdk_core::payload::codec::CodecChain>>,
pub api_key: Option<String>,
pub organization_id: Option<String>,
pub tls: Option<ClientTlsConfig>,
}
#[derive(Clone, Default)]
#[non_exhaustive]
pub struct ClientTlsConfig {
pub ca_cert: Option<Vec<u8>>,
pub client_cert: Option<Vec<u8>>,
pub client_key: Option<Vec<u8>>,
pub domain_name: Option<String>,
}
impl Default for ClientConfig {
fn default() -> Self {
Self {
server_url: "http://localhost:50051".to_string(),
namespace: "default".to_string(),
timeout: Duration::from_secs(30),
identity: format!("orcher-client-{}", uuid::Uuid::new_v4()),
use_payload_format: true,
data_converter: None, codec_chain: None, api_key: None, organization_id: None, tls: None, }
}
}
impl ClientConfig {
pub fn new(server_url: impl Into<String>) -> Self {
Self {
server_url: server_url.into(),
..Self::default()
}
}
pub fn with_server_url(mut self, server_url: impl Into<String>) -> Self {
self.server_url = server_url.into();
self
}
pub fn with_namespace(mut self, namespace: impl Into<String>) -> Self {
self.namespace = namespace.into();
self
}
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
pub fn with_identity(mut self, identity: impl Into<String>) -> Self {
self.identity = identity.into();
self
}
pub fn with_payload_format(mut self, use_payload_format: bool) -> Self {
self.use_payload_format = use_payload_format;
self
}
pub fn with_data_converter(
mut self,
data_converter: crate::client::client::DataConverterType,
) -> Self {
self.data_converter = Some(data_converter);
self
}
pub fn with_codec_chain(
mut self,
codec_chain: Arc<orcher_sdk_core::payload::codec::CodecChain>,
) -> Self {
self.codec_chain = Some(codec_chain);
self
}
pub fn with_api_key(mut self, api_key: impl Into<String>) -> Self {
self.api_key = Some(api_key.into());
self
}
pub fn with_organization_id(mut self, organization_id: impl Into<String>) -> Self {
self.organization_id = Some(organization_id.into());
self
}
pub fn with_tls(mut self, tls: ClientTlsConfig) -> Self {
self.tls = Some(tls);
self
}
}
impl ClientTlsConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_ca_cert(mut self, ca_cert: impl Into<Vec<u8>>) -> Self {
self.ca_cert = Some(ca_cert.into());
self
}
pub fn with_client_identity(
mut self,
client_cert: impl Into<Vec<u8>>,
client_key: impl Into<Vec<u8>>,
) -> Self {
self.client_cert = Some(client_cert.into());
self.client_key = Some(client_key.into());
self
}
pub fn with_domain_name(mut self, domain_name: impl Into<String>) -> Self {
self.domain_name = Some(domain_name.into());
self
}
pub fn with_ca_cert_file(self, path: impl AsRef<std::path::Path>) -> std::io::Result<Self> {
Ok(self.with_ca_cert(read_pem(path.as_ref())?))
}
pub fn with_client_identity_files(
self,
cert_path: impl AsRef<std::path::Path>,
key_path: impl AsRef<std::path::Path>,
) -> std::io::Result<Self> {
let cert = read_pem(cert_path.as_ref())?;
let key = read_pem(key_path.as_ref())?;
Ok(self.with_client_identity(cert, key))
}
}
fn read_pem(path: &std::path::Path) -> std::io::Result<Vec<u8>> {
std::fs::read(path).map_err(|e| {
std::io::Error::new(e.kind(), format!("cannot read {}: {}", path.display(), e))
})
}
impl std::fmt::Debug for ClientTlsConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ClientTlsConfig")
.field("ca_cert", &self.ca_cert.as_ref().map(|_| "<pem>"))
.field("client_cert", &self.client_cert.as_ref().map(|_| "<pem>"))
.field(
"client_key",
&self.client_key.as_ref().map(|_| "<redacted>"),
)
.field("domain_name", &self.domain_name)
.finish()
}
}
impl std::fmt::Debug for ClientConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ClientConfig")
.field("server_url", &self.server_url)
.field("namespace", &self.namespace)
.field("timeout", &self.timeout)
.field("identity", &self.identity)
.field("use_payload_format", &self.use_payload_format)
.field("data_converter", &"<DataConverter>")
.field(
"codec_chain",
&if self.codec_chain.is_some() {
"<CodecChain>"
} else {
"None"
},
)
.field(
"api_key",
&if self.api_key.is_some() {
"<redacted>"
} else {
"None"
},
)
.field("organization_id", &self.organization_id)
.field(
"tls",
&if self.tls.is_some() {
"<configured>"
} else {
"None"
},
)
.finish()
}
}
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct StartWorkflowOptions {
pub workflow_id: Option<String>,
pub task_queue: String,
pub workflow_execution_timeout: Option<Duration>,
pub workflow_run_timeout: Option<Duration>,
pub workflow_task_timeout: Option<Duration>,
pub namespace: Option<String>,
pub cron_schedule: Option<String>,
pub retry_policy: Option<crate::task::RetryPolicy>,
pub id_reuse_policy: Option<WorkflowIdReusePolicy>,
}
impl StartWorkflowOptions {
pub fn new(task_queue: impl Into<String>) -> Self {
Self {
task_queue: task_queue.into(),
..Self::default()
}
}
pub fn with_workflow_id(mut self, workflow_id: impl Into<String>) -> Self {
self.workflow_id = Some(workflow_id.into());
self
}
pub fn with_task_queue(mut self, task_queue: impl Into<String>) -> Self {
self.task_queue = task_queue.into();
self
}
pub fn with_workflow_execution_timeout(mut self, timeout: Duration) -> Self {
self.workflow_execution_timeout = Some(timeout);
self
}
pub fn with_workflow_run_timeout(mut self, timeout: Duration) -> Self {
self.workflow_run_timeout = Some(timeout);
self
}
pub fn with_workflow_task_timeout(mut self, timeout: Duration) -> Self {
self.workflow_task_timeout = Some(timeout);
self
}
pub fn with_namespace(mut self, namespace: impl Into<String>) -> Self {
self.namespace = Some(namespace.into());
self
}
pub fn with_cron_schedule(mut self, cron_schedule: impl Into<String>) -> Self {
self.cron_schedule = Some(cron_schedule.into());
self
}
pub fn with_retry_policy(mut self, retry_policy: crate::task::RetryPolicy) -> Self {
self.retry_policy = Some(retry_policy);
self
}
pub fn with_id_reuse_policy(mut self, policy: WorkflowIdReusePolicy) -> Self {
self.id_reuse_policy = Some(policy);
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum WorkflowIdReusePolicy {
AllowDuplicate,
AllowDuplicateFailedOnly,
RejectDuplicate,
TerminateIfRunning,
}
impl WorkflowIdReusePolicy {
pub(crate) fn to_core(self) -> orcher_sdk_core::WorkflowIdReusePolicy {
use orcher_sdk_core::WorkflowIdReusePolicy as Proto;
match self {
Self::AllowDuplicate => Proto::AllowDuplicate,
Self::AllowDuplicateFailedOnly => Proto::AllowDuplicateFailedOnly,
Self::RejectDuplicate => Proto::RejectDuplicate,
Self::TerminateIfRunning => Proto::TerminateIfRunning,
}
}
}
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct QueryOptions {
pub timeout: Option<Duration>,
}
impl QueryOptions {
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}
}
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct EventOptions {
pub timeout: Option<Duration>,
}
impl EventOptions {
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}
}
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct CancelOptions {
pub cleanup_timeout: Option<Duration>,
}
impl CancelOptions {
pub fn with_cleanup_timeout(mut self, timeout: Duration) -> Self {
self.cleanup_timeout = Some(timeout);
self
}
pub(crate) fn to_core(&self) -> orcher_sdk_core::client::CancelWorkflowOpts {
let opts = orcher_sdk_core::client::CancelWorkflowOpts::default();
match self.cleanup_timeout {
Some(timeout) => opts.with_cleanup_timeout(timeout),
None => opts,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_cancellation_sets_no_cleanup_limit_by_default() {
assert_eq!(CancelOptions::default().to_core().cleanup_timeout, None);
}
#[test]
fn a_cancellation_passes_its_cleanup_limit_to_sdk_core() {
let options = CancelOptions::default().with_cleanup_timeout(Duration::from_millis(90_500));
assert_eq!(
options.to_core().cleanup_timeout,
Some(Duration::from_millis(90_500))
);
}
#[test]
fn test_client_config_defaults() {
let config = ClientConfig::default();
assert_eq!(config.server_url, "http://localhost:50051");
assert_eq!(config.namespace, "default");
assert_eq!(config.timeout, Duration::from_secs(30));
assert!(config.identity.starts_with("orcher-client-"));
assert!(config.use_payload_format);
assert!(config.data_converter.is_none());
}
#[test]
fn test_client_config_with_payload_format_disabled() {
let config = ClientConfig {
use_payload_format: false,
..Default::default()
};
assert!(!config.use_payload_format);
}
#[test]
fn test_client_config_debug() {
let config = ClientConfig::default();
let debug_str = format!("{:?}", config);
assert!(debug_str.contains("use_payload_format"));
assert!(debug_str.contains("data_converter"));
}
#[test]
fn test_start_workflow_options_defaults() {
let options = StartWorkflowOptions::default();
assert!(options.workflow_id.is_none());
assert_eq!(options.task_queue, "");
assert!(options.workflow_execution_timeout.is_none());
assert!(options.namespace.is_none());
}
#[test]
fn test_query_options_defaults() {
let options = QueryOptions::default();
assert!(options.timeout.is_none());
}
#[test]
fn test_event_options_defaults() {
let options = EventOptions::default();
assert!(options.timeout.is_none());
}
}