[router] Worker Management Workflow Engine (#11868)
This commit is contained in:
@@ -22,8 +22,8 @@ use tracing::{error, info, warn, Level};
|
||||
use crate::{
|
||||
config::{ConnectionMode, HistoryBackend, RouterConfig, RoutingMode},
|
||||
core::{
|
||||
worker_to_info, Job, JobQueue, JobQueueConfig, LoadMonitor, WorkerManager, WorkerRegistry,
|
||||
WorkerType,
|
||||
worker_to_info, workflow::WorkflowEngine, Job, JobQueue, JobQueueConfig, LoadMonitor,
|
||||
WorkerManager, WorkerRegistry, WorkerType,
|
||||
},
|
||||
data_connector::{
|
||||
MemoryConversationItemStorage, MemoryConversationStorage, MemoryResponseStorage,
|
||||
@@ -77,6 +77,7 @@ pub struct AppContext {
|
||||
pub configured_reasoning_parser: Option<String>,
|
||||
pub configured_tool_parser: Option<String>,
|
||||
pub worker_job_queue: Arc<OnceLock<Arc<JobQueue>>>,
|
||||
pub workflow_engine: Arc<OnceLock<Arc<WorkflowEngine>>>,
|
||||
}
|
||||
|
||||
impl AppContext {
|
||||
@@ -95,6 +96,7 @@ impl AppContext {
|
||||
conversation_item_storage: SharedConversationItemStorage,
|
||||
load_monitor: Option<Arc<LoadMonitor>>,
|
||||
worker_job_queue: Arc<OnceLock<Arc<JobQueue>>>,
|
||||
workflow_engine: Arc<OnceLock<Arc<WorkflowEngine>>>,
|
||||
) -> Self {
|
||||
let configured_reasoning_parser = router_config.reasoning_parser.clone();
|
||||
let configured_tool_parser = router_config.tool_call_parser.clone();
|
||||
@@ -116,6 +118,7 @@ impl AppContext {
|
||||
configured_reasoning_parser,
|
||||
configured_tool_parser,
|
||||
worker_job_queue,
|
||||
workflow_engine,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -979,8 +982,9 @@ pub async fn startup(config: ServerConfig) -> Result<(), Box<dyn std::error::Err
|
||||
config.router_config.worker_startup_check_interval_secs,
|
||||
)));
|
||||
|
||||
// Create empty OnceLock for worker job queue (will be initialized below)
|
||||
// Create empty OnceLock for worker job queue and workflow engine (will be initialized below)
|
||||
let worker_job_queue = Arc::new(OnceLock::new());
|
||||
let workflow_engine = Arc::new(OnceLock::new());
|
||||
|
||||
// Create AppContext with all initialized components
|
||||
let app_context = AppContext::new(
|
||||
@@ -997,6 +1001,7 @@ pub async fn startup(config: ServerConfig) -> Result<(), Box<dyn std::error::Err
|
||||
conversation_item_storage,
|
||||
load_monitor,
|
||||
worker_job_queue,
|
||||
workflow_engine,
|
||||
);
|
||||
|
||||
let app_context = Arc::new(app_context);
|
||||
@@ -1008,17 +1013,38 @@ pub async fn startup(config: ServerConfig) -> Result<(), Box<dyn std::error::Err
|
||||
.set(worker_job_queue)
|
||||
.expect("JobQueue should only be initialized once");
|
||||
|
||||
// Initialize workflow engine and register workflows
|
||||
let engine = Arc::new(WorkflowEngine::new());
|
||||
|
||||
engine
|
||||
.event_bus()
|
||||
.subscribe(Arc::new(crate::core::workflow::LoggingSubscriber))
|
||||
.await;
|
||||
|
||||
engine.register_workflow(crate::core::workflow::create_worker_registration_workflow());
|
||||
app_context
|
||||
.workflow_engine
|
||||
.set(engine)
|
||||
.expect("WorkflowEngine should only be initialized once");
|
||||
info!("Workflow engine initialized with worker registration workflow");
|
||||
|
||||
info!(
|
||||
"Initializing workers for routing mode: {:?}",
|
||||
config.router_config.mode
|
||||
);
|
||||
WorkerManager::initialize_workers(
|
||||
&config.router_config,
|
||||
&app_context.worker_registry,
|
||||
Some(&app_context.policy_registry),
|
||||
)
|
||||
.await
|
||||
.map_err(|e| format!("Failed to initialize workers: {}", e))?;
|
||||
|
||||
// Submit worker initialization job to queue
|
||||
let job_queue = app_context
|
||||
.worker_job_queue
|
||||
.get()
|
||||
.expect("JobQueue should be initialized");
|
||||
let job = Job::InitializeWorkersFromConfig {
|
||||
router_config: Box::new(config.router_config.clone()),
|
||||
};
|
||||
job_queue
|
||||
.submit(job)
|
||||
.await
|
||||
.map_err(|e| format!("Failed to submit worker initialization job: {}", e))?;
|
||||
|
||||
let worker_stats = app_context.worker_registry.stats();
|
||||
info!(
|
||||
|
||||
Reference in New Issue
Block a user