use crate::protos::temporal::api::enums::v1::{TaskQueueType, VersioningBehavior};
use std::{
fs::File,
io::{self, BufReader, Read},
sync::OnceLock,
};
pub use temporalio_common_wasm::worker::WorkerDeploymentVersion;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct WorkerTaskTypes {
pub enable_workflows: bool,
pub enable_local_activities: bool,
pub enable_remote_activities: bool,
pub enable_nexus: bool,
}
impl WorkerTaskTypes {
pub fn is_empty(&self) -> bool {
!self.enable_workflows
&& !self.enable_local_activities
&& !self.enable_remote_activities
&& !self.enable_nexus
}
pub fn all() -> WorkerTaskTypes {
WorkerTaskTypes {
enable_workflows: true,
enable_local_activities: true,
enable_remote_activities: true,
enable_nexus: true,
}
}
pub fn workflow_only() -> WorkerTaskTypes {
WorkerTaskTypes {
enable_workflows: true,
enable_local_activities: false,
enable_remote_activities: false,
enable_nexus: false,
}
}
pub fn activity_only() -> WorkerTaskTypes {
WorkerTaskTypes {
enable_workflows: false,
enable_local_activities: false,
enable_remote_activities: true,
enable_nexus: false,
}
}
pub fn nexus_only() -> WorkerTaskTypes {
WorkerTaskTypes {
enable_workflows: false,
enable_local_activities: false,
enable_remote_activities: false,
enable_nexus: true,
}
}
pub fn overlaps_with(&self, other: &WorkerTaskTypes) -> bool {
(self.enable_workflows && other.enable_workflows)
|| (self.enable_local_activities && other.enable_local_activities)
|| (self.enable_remote_activities && other.enable_remote_activities)
|| (self.enable_nexus && other.enable_nexus)
}
pub fn to_task_queue_types(&self) -> Vec<TaskQueueType> {
let mut types = Vec::new();
if self.enable_workflows {
types.push(TaskQueueType::Workflow);
}
if self.enable_remote_activities {
types.push(TaskQueueType::Activity);
}
if self.enable_nexus {
types.push(TaskQueueType::Nexus);
}
types
}
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct WorkerDeploymentOptions {
pub version: WorkerDeploymentVersion,
pub use_worker_versioning: bool,
pub default_versioning_behavior: Option<VersioningBehavior>,
}
impl WorkerDeploymentOptions {
pub fn from_build_id(build_id: String) -> Self {
Self {
version: WorkerDeploymentVersion {
deployment_name: "".to_owned(),
build_id,
},
use_worker_versioning: false,
default_versioning_behavior: None,
}
}
}
static CACHED_BUILD_ID: OnceLock<String> = OnceLock::new();
pub fn build_id_from_current_exe() -> &'static str {
CACHED_BUILD_ID
.get_or_init(|| compute_crc32_exe_id().unwrap_or_else(|_| "undetermined".to_owned()))
}
fn compute_crc32_exe_id() -> io::Result<String> {
let exe_path = std::env::current_exe()?;
let file = File::open(exe_path)?;
let mut reader = BufReader::new(file);
let mut hasher = crc32fast::Hasher::new();
let mut buf = [0u8; 128 * 1024];
loop {
let n = reader.read(&mut buf)?;
if n == 0 {
break;
}
hasher.update(&buf[..n]);
}
let crc = hasher.finalize();
Ok(format!("{:08x}", crc))
}