lib.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c)2023 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. pub mod ext;
  13. use serde::{Deserialize, Serialize};
  14. use std::str::FromStr;
  15. use std::time::Duration;
  16. use temporal_client::{Client, ClientOptionsBuilder, RetryClient, WorkflowClientTrait, WorkflowOptions};
  17. use temporal_sdk_core_protos::{coresdk::AsJsonPayloadExt, temporal::api::enums::v1::WorkflowIdReusePolicy};
  18. use url::Url;
  19. use uuid::Uuid;
  20. const CLIENT_NAME: &str = "SmeeClient-Rust";
  21. const CLIENT_VERSION: &str = "0.1";
  22. const NETWORK_JOINED_WORKFLOW: &str = "NetworkJoinedWorkflow";
  23. #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
  24. pub struct NetworkJoinedParams {
  25. #[serde(rename = "NetworkID")]
  26. pub network_id: String,
  27. #[serde(rename = "MemberID")]
  28. pub member_id: String,
  29. }
  30. impl NetworkJoinedParams {
  31. fn new(network_id: &str, member_id: &str) -> Self {
  32. Self {
  33. network_id: network_id.to_string(),
  34. member_id: member_id.to_string(),
  35. }
  36. }
  37. }
  38. pub struct SmeeClient {
  39. tokio_rt: tokio::runtime::Runtime,
  40. client: RetryClient<Client>,
  41. task_queue: String,
  42. }
  43. impl SmeeClient {
  44. pub fn new(temporal_url: &str, namespace: &str, task_queue: &str) -> Result<Self, Box<dyn std::error::Error>> {
  45. // start tokio runtime. Required by temporal
  46. let rt = tokio::runtime::Runtime::new()?;
  47. let c = ClientOptionsBuilder::default()
  48. .target_url(Url::from_str(temporal_url).unwrap())
  49. .client_name(CLIENT_NAME)
  50. .client_version(CLIENT_VERSION)
  51. .build()?;
  52. let con = rt.block_on(async { c.connect(namespace.to_string(), None).await })?;
  53. Ok(Self {
  54. tokio_rt: rt,
  55. client: con,
  56. task_queue: task_queue.to_string(),
  57. })
  58. }
  59. pub fn notify_network_joined(&self, params: NetworkJoinedParams) -> Result<(), Box<dyn std::error::Error>> {
  60. println!("notifying network joined");
  61. let options = WorkflowOptions {
  62. id_reuse_policy: WorkflowIdReusePolicy::RejectDuplicate,
  63. execution_timeout: None,
  64. run_timeout: None,
  65. task_timeout: None,
  66. cron_schedule: None,
  67. search_attributes: None,
  68. enable_eager_workflow_start: false,
  69. retry_policy: Some(Default::default()),
  70. };
  71. let payload = vec![params.as_json_payload()?];
  72. let workflow_id = Uuid::new_v4();
  73. self.tokio_rt.block_on(async {
  74. println!("calilng start_workflow");
  75. self.client
  76. .start_workflow(
  77. payload,
  78. self.task_queue.clone(),
  79. workflow_id.hyphenated().to_string(),
  80. String::from(NETWORK_JOINED_WORKFLOW),
  81. None,
  82. options,
  83. )
  84. .await
  85. })?;
  86. Ok(())
  87. }
  88. pub fn shutdown(self) {
  89. self.tokio_rt.shutdown_timeout(Duration::from_secs(5))
  90. }
  91. }