use std::collections::HashMap;
#[cfg(feature = "auto-register")]
use std::sync::RwLock;
#[cfg(feature = "auto-register")]
use crate::worker::registration::{
ActorHandlerRegistration, TaskRegistration, WorkflowRegistration,
};
#[cfg(feature = "auto-register")]
use once_cell::sync::Lazy;
#[cfg(feature = "auto-register")]
pub static GLOBAL_REGISTRY: Lazy<RwLock<GlobalRegistry>> = Lazy::new(|| {
tracing::debug!("Initializing global auto-registration registry");
RwLock::new(GlobalRegistry::new())
});
#[cfg(feature = "auto-register")]
#[derive(Debug, Default)]
pub struct GlobalRegistry {
tasks: Vec<TaskRegistration>,
workflows: Vec<WorkflowRegistration>,
actor_handlers: Vec<ActorHandlerRegistration>,
}
#[cfg(feature = "auto-register")]
impl GlobalRegistry {
pub fn new() -> Self {
Self {
tasks: Vec::new(),
workflows: Vec::new(),
actor_handlers: Vec::new(),
}
}
#[deprecated(note = "Tasks are now registered via inventory")]
pub fn register_task_direct(&mut self, _registration: TaskRegistration) {
tracing::debug!("Task registration via GlobalRegistry is deprecated - use inventory");
}
#[deprecated(note = "Workflows are now registered via inventory")]
pub fn register_workflow_direct(&mut self, _registration: WorkflowRegistration) {
tracing::debug!("Workflow registration via GlobalRegistry is deprecated - use inventory");
}
pub fn register_actor_handler_direct(&mut self, registration: ActorHandlerRegistration) {
tracing::debug!(
actor = registration.actor_name,
operation = registration.operation_name,
"Auto-registering actor handler to global registry"
);
self.actor_handlers.push(registration);
}
pub fn tasks(&self) -> &[TaskRegistration] {
&self.tasks
}
pub fn workflows(&self) -> &[WorkflowRegistration] {
&self.workflows
}
pub fn task_count(&self) -> usize {
self.tasks.len()
}
pub fn workflow_count(&self) -> usize {
self.workflows.len()
}
pub fn actor_handlers(&self) -> &[ActorHandlerRegistration] {
&self.actor_handlers
}
pub fn actor_handler_count(&self) -> usize {
self.actor_handlers.len()
}
}
#[derive(Debug, Default)]
pub struct Registry {
pub workflows: HashMap<String, WorkflowHandler>,
pub tasks: HashMap<String, TaskHandler>,
pub updates: HashMap<String, UpdateHandler>,
#[cfg(feature = "auto-register")]
pub actors: HashMap<String, crate::actor::definition::ActorDefinition>,
#[cfg(feature = "auto-register")]
pub actor_handlers: HashMap<String, HashMap<String, ActorHandlerRegistration>>,
}
#[derive(Clone)]
pub struct WorkflowHandler {
pub name: String,
#[cfg(feature = "auto-register")]
pub handler: Option<crate::worker::registration::WorkflowHandlerFn>,
}
#[derive(Clone)]
pub struct TaskHandler {
pub name: String,
#[cfg(feature = "auto-register")]
pub handler: Option<crate::worker::registration::TaskHandlerFn>,
}
#[derive(Clone)]
pub struct UpdateHandler {
pub name: String,
#[cfg(feature = "auto-register")]
pub handler: Option<crate::worker::registration::UpdateHandlerFn>,
}
impl std::fmt::Debug for WorkflowHandler {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("WorkflowHandler")
.field("name", &self.name)
.finish()
}
}
impl std::fmt::Debug for TaskHandler {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TaskHandler")
.field("name", &self.name)
.finish()
}
}
impl std::fmt::Debug for UpdateHandler {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("UpdateHandler")
.field("name", &self.name)
.finish()
}
}
impl Registry {
pub fn new() -> Self {
Self {
workflows: HashMap::new(),
tasks: HashMap::new(),
updates: HashMap::new(),
#[cfg(feature = "auto-register")]
actors: HashMap::new(),
#[cfg(feature = "auto-register")]
actor_handlers: HashMap::new(),
}
}
pub fn register_workflow(&mut self, name: impl Into<String>, handler: WorkflowHandler) {
let name = name.into();
tracing::debug!(workflow = %name, "Registering workflow handler");
self.workflows.insert(name, handler);
}
pub fn register_task(&mut self, name: impl Into<String>, handler: TaskHandler) {
let name = name.into();
tracing::debug!(task = %name, "Registering task handler");
self.tasks.insert(name, handler);
}
pub fn get_workflow(&self, name: &str) -> Option<&WorkflowHandler> {
self.workflows.get(name)
}
pub fn get_task(&self, name: &str) -> Option<&TaskHandler> {
self.tasks.get(name)
}
pub fn workflow_names(&self) -> Vec<&str> {
self.workflows.keys().map(|s| s.as_str()).collect()
}
pub fn task_names(&self) -> Vec<&str> {
self.tasks.keys().map(|s| s.as_str()).collect()
}
pub fn has_workflow(&self, name: &str) -> bool {
self.workflows.contains_key(name)
}
pub fn has_task(&self, name: &str) -> bool {
self.tasks.contains_key(name)
}
pub fn register_update(&mut self, name: impl Into<String>, handler: UpdateHandler) {
let name = name.into();
tracing::debug!(update = %name, "Registering update handler");
self.updates.insert(name, handler);
}
pub fn get_update(&self, name: &str) -> Option<&UpdateHandler> {
self.updates.get(name)
}
pub fn update_names(&self) -> Vec<&str> {
self.updates.keys().map(|s| s.as_str()).collect()
}
pub fn has_update(&self, name: &str) -> bool {
self.updates.contains_key(name)
}
pub fn workflow_count(&self) -> usize {
self.workflows.len()
}
pub fn task_count(&self) -> usize {
self.tasks.len()
}
pub fn update_count(&self) -> usize {
self.updates.len()
}
#[cfg(feature = "auto-register")]
pub fn actor_count(&self) -> usize {
self.actors.len()
}
#[cfg(feature = "auto-register")]
pub fn actor_names(&self) -> Vec<&str> {
self.actors.keys().map(|s| s.as_str()).collect()
}
#[cfg(feature = "auto-register")]
pub fn get_actor(&self, name: &str) -> Option<&crate::actor::definition::ActorDefinition> {
self.actors.get(name)
}
#[cfg(feature = "auto-register")]
pub fn has_actor(&self, name: &str) -> bool {
self.actors.contains_key(name)
}
#[cfg(feature = "auto-register")]
pub fn actor_handlers(&self) -> &HashMap<String, HashMap<String, ActorHandlerRegistration>> {
&self.actor_handlers
}
#[cfg(feature = "auto-register")]
pub fn actor_handler_count(&self) -> usize {
self.actor_handlers.values().map(|ops| ops.len()).sum()
}
#[cfg(feature = "auto-register")]
pub fn collect_from_global_registry(&mut self) {
let task_registrations: Vec<_> =
inventory::iter::<crate::worker::registration::TaskRegistration>()
.copied()
.collect();
let task_count = task_registrations.len();
for registration in task_registrations {
tracing::debug!("Invoking task registration function");
registration.register(self);
}
let workflow_registrations: Vec<_> =
inventory::iter::<crate::worker::registration::WorkflowRegistration>()
.copied()
.collect();
let workflow_count = workflow_registrations.len();
for registration in workflow_registrations {
tracing::debug!("Invoking workflow registration function");
registration.register(self);
}
let update_registrations: Vec<_> =
inventory::iter::<crate::worker::registration::UpdateRegistration>()
.copied()
.collect();
let update_count = update_registrations.len();
for registration in update_registrations {
tracing::debug!("Invoking update handler registration function");
registration.register(self);
}
#[cfg(feature = "auto-register")]
{
let global = GLOBAL_REGISTRY
.read()
.expect("Failed to acquire read lock on GLOBAL_REGISTRY");
let _actor_handler_count = global.actor_handlers().len();
for handler_registration in global.actor_handlers() {
tracing::debug!(
actor = handler_registration.actor_name,
operation = handler_registration.operation_name,
"Collecting actor handler from global registry"
);
self.actor_handlers
.entry(handler_registration.actor_name.to_string())
.or_default()
.insert(
handler_registration.operation_name.to_string(),
handler_registration.clone(),
);
}
}
let actors: Vec<_> = inventory::iter::<crate::actor::definition::ActorDefinition>()
.cloned()
.collect();
let actor_count = actors.len();
for actor_def in actors {
tracing::debug!(
actor = actor_def.actor_name(),
operations = actor_def.operation_count(),
"Collecting actor from inventory"
);
self.actors
.insert(actor_def.actor_name().to_string(), actor_def);
}
tracing::info!(
tasks = task_count,
workflows = workflow_count,
updates = update_count,
actors = actor_count,
"Collected and registered handlers from inventory"
);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_registry_new() {
let registry = Registry::new();
assert_eq!(registry.workflow_count(), 0);
assert_eq!(registry.task_count(), 0);
}
#[test]
fn test_register_workflow() {
let mut registry = Registry::new();
let handler = WorkflowHandler {
name: "test_workflow".to_string(),
#[cfg(feature = "auto-register")]
handler: None,
};
registry.register_workflow("test_workflow", handler);
assert_eq!(registry.workflow_count(), 1);
assert!(registry.has_workflow("test_workflow"));
assert!(!registry.has_workflow("unknown"));
}
#[test]
fn test_register_task() {
let mut registry = Registry::new();
let handler = TaskHandler {
name: "test_task".to_string(),
#[cfg(feature = "auto-register")]
handler: None,
};
registry.register_task("test_task", handler);
assert_eq!(registry.task_count(), 1);
assert!(registry.has_task("test_task"));
assert!(!registry.has_task("unknown"));
}
#[test]
fn test_get_handlers() {
let mut registry = Registry::new();
let wf_handler = WorkflowHandler {
name: "my_workflow".to_string(),
#[cfg(feature = "auto-register")]
handler: None,
};
let task_handler = TaskHandler {
name: "my_task".to_string(),
#[cfg(feature = "auto-register")]
handler: None,
};
registry.register_workflow("my_workflow", wf_handler);
registry.register_task("my_task", task_handler);
assert!(registry.get_workflow("my_workflow").is_some());
assert!(registry.get_task("my_task").is_some());
assert!(registry.get_workflow("unknown").is_none());
assert!(registry.get_task("unknown").is_none());
}
#[test]
fn test_list_names() {
let mut registry = Registry::new();
registry.register_workflow(
"workflow1",
WorkflowHandler {
name: "workflow1".to_string(),
#[cfg(feature = "auto-register")]
handler: None,
},
);
registry.register_workflow(
"workflow2",
WorkflowHandler {
name: "workflow2".to_string(),
#[cfg(feature = "auto-register")]
handler: None,
},
);
registry.register_task(
"task1",
TaskHandler {
name: "task1".to_string(),
#[cfg(feature = "auto-register")]
handler: None,
},
);
let workflow_names = registry.workflow_names();
let task_names = registry.task_names();
assert_eq!(workflow_names.len(), 2);
assert_eq!(task_names.len(), 1);
assert!(workflow_names.contains(&"workflow1"));
assert!(workflow_names.contains(&"workflow2"));
assert!(task_names.contains(&"task1"));
}
#[cfg(feature = "auto-register")]
#[test]
fn test_collect_from_global_registry() {
let mut registry = Registry::new();
registry.collect_from_global_registry();
println!(
"Collected {} tasks and {} workflows from global registry",
registry.task_count(),
registry.workflow_count()
);
}
#[test]
fn test_registry_default() {
let registry = Registry::default();
assert_eq!(registry.workflow_count(), 0);
assert_eq!(registry.task_count(), 0);
}
}