update api, fix tests and examples
This commit is contained in:
111
src/runner.rs
111
src/runner.rs
@@ -1,12 +1,7 @@
|
||||
//! Runner implementation for actor process management.
|
||||
|
||||
use crate::job::{Job};
|
||||
use log::{debug, info};
|
||||
use redis::AsyncCommands;
|
||||
use sal_service_manager::{ProcessManager, ProcessManagerError as ServiceProcessManagerError, ProcessStatus, ProcessConfig};
|
||||
use sal_service_manager::{ProcessManagerError as ServiceProcessManagerError, ProcessStatus, ProcessConfig};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
/// Represents the current status of an actor/runner (alias for ProcessStatus)
|
||||
pub type RunnerStatus = ProcessStatus;
|
||||
@@ -127,8 +122,17 @@ pub enum RunnerError {
|
||||
#[from]
|
||||
source: crate::JobError,
|
||||
},
|
||||
|
||||
#[error("Job '{job_id}' not found")]
|
||||
JobNotFound { job_id: String },
|
||||
|
||||
#[error("Authentication error: {message}")]
|
||||
AuthenticationError { message: String },
|
||||
}
|
||||
|
||||
// Type alias for backward compatibility
|
||||
pub type RunnerConfig = Runner;
|
||||
|
||||
/// Convert Runner to ProcessConfig
|
||||
pub fn runner_to_process_config(config: &Runner) -> ProcessConfig {
|
||||
ProcessConfig::new(config.id.clone(), config.command.clone())
|
||||
@@ -137,98 +141,3 @@ pub fn runner_to_process_config(config: &Runner) -> ProcessConfig {
|
||||
.with_arg("--redis-url".to_string())
|
||||
.with_arg(config.redis_url.clone())
|
||||
}
|
||||
|
||||
// Type alias for backward compatibility
|
||||
pub type RunnerConfig = Runner;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use sal_service_manager::{ProcessManagerError, SimpleProcessManager};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct MockProcessManager {
|
||||
processes: HashMap<String, ProcessStatus>,
|
||||
}
|
||||
|
||||
impl MockProcessManager {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
processes: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl ProcessManager for MockProcessManager {
|
||||
async fn start_process(&mut self, config: &ProcessConfig) -> Result<(), ProcessManagerError> {
|
||||
self.processes.insert(config.id.clone(), ProcessStatus::Running);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn stop_process(&mut self, process_id: &str, _force: bool) -> Result<(), ProcessManagerError> {
|
||||
self.processes.insert(process_id.to_string(), ProcessStatus::Stopped);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn process_status(&self, process_id: &str) -> Result<ProcessStatus, ProcessManagerError> {
|
||||
Ok(self.processes.get(process_id).cloned().unwrap_or(ProcessStatus::Stopped))
|
||||
}
|
||||
|
||||
async fn process_logs(&self, _process_id: &str, _lines: Option<usize>, _follow: bool) -> Result<Vec<LogInfo>, ProcessManagerError> {
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
async fn health_check(&self) -> Result<(), ProcessManagerError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn list_processes(&self) -> Result<Vec<String>, ProcessManagerError> {
|
||||
Ok(self.processes.keys().cloned().collect())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_runner_creation() {
|
||||
let runner = Runner::new(
|
||||
"test_actor".to_string(),
|
||||
"test_runner".to_string(),
|
||||
"".to_string(),
|
||||
PathBuf::from("/path/to/binary"),
|
||||
"redis://localhost:6379".to_string(),
|
||||
);
|
||||
|
||||
assert_eq!(runner.id, "test_actor");
|
||||
assert_eq!(runner.name, "test_runner");
|
||||
assert_eq!(runner.command, PathBuf::from("/path/to/binary"));
|
||||
assert_eq!(runner.redis_url, "redis://localhost:6379");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_runner_get_queue() {
|
||||
let runner = Runner::new(
|
||||
"test_actor".to_string(),
|
||||
"test_runner".to_string(),
|
||||
"".to_string(),
|
||||
PathBuf::from("/path/to/binary"),
|
||||
"redis://localhost:6379".to_string(),
|
||||
);
|
||||
|
||||
let queue_key = runner.get_queue();
|
||||
assert_eq!(queue_key, "runner:test_runner");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_runner_error_types() {
|
||||
let error = RunnerError::ActorNotFound {
|
||||
actor_id: "test".to_string(),
|
||||
};
|
||||
assert!(error.to_string().contains("test"));
|
||||
|
||||
let error = RunnerError::ActorAlreadyRunning {
|
||||
actor_id: "test".to_string(),
|
||||
};
|
||||
assert!(error.to_string().contains("already running"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user