update api, fix tests and examples
This commit is contained in:
@@ -14,7 +14,7 @@ use std::time::Duration;
|
||||
use tokio::time::sleep;
|
||||
use redis::AsyncCommands;
|
||||
use hero_supervisor::{
|
||||
job::{Job, JobStatus, JobType, keys},
|
||||
Job, JobStatus, JobError, client::{Client, ClientBuilder}
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -43,6 +43,14 @@ impl MockRunnerConfig {
|
||||
return Err("Missing value for --actor-id".into());
|
||||
}
|
||||
}
|
||||
"--db-path" => {
|
||||
if i + 1 < args.len() {
|
||||
db_path = Some(args[i + 1].clone());
|
||||
i += 2;
|
||||
} else {
|
||||
return Err("Missing value for --db-path".into());
|
||||
}
|
||||
}
|
||||
"--redis-url" => {
|
||||
if i + 1 < args.len() {
|
||||
redis_url = Some(args[i + 1].clone());
|
||||
@@ -65,16 +73,19 @@ impl MockRunnerConfig {
|
||||
|
||||
pub struct MockRunner {
|
||||
config: MockRunnerConfig,
|
||||
redis_client: redis::Client,
|
||||
client: Client,
|
||||
}
|
||||
|
||||
impl MockRunner {
|
||||
pub fn new(config: MockRunnerConfig) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let redis_client = redis::Client::open(config.redis_url.clone())?;
|
||||
pub async fn new(config: MockRunnerConfig) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let client = ClientBuilder::new()
|
||||
.redis_url(&config.redis_url)
|
||||
.build()
|
||||
.await?;
|
||||
|
||||
Ok(MockRunner {
|
||||
config,
|
||||
redis_client,
|
||||
client,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -83,53 +94,52 @@ impl MockRunner {
|
||||
println!("📂 DB Path: {}", self.config.db_path);
|
||||
println!("🔗 Redis URL: {}", self.config.redis_url);
|
||||
|
||||
let mut conn = self.redis_client.get_multiplexed_async_connection().await?;
|
||||
|
||||
// Use the proper Hero job queue key for this actor instance
|
||||
// Format: hero:q:work:type:{job_type}:group:{group}:inst:{instance}
|
||||
let work_queue_key = keys::work_instance(&JobType::OSIS, "default", &self.config.actor_id);
|
||||
let work_queue_key = format!("hero:q:work:type:osis:group:default:inst:{}", self.config.actor_id);
|
||||
|
||||
println!("👂 Listening for jobs on queue: {}", work_queue_key);
|
||||
|
||||
loop {
|
||||
// Try to pop a job ID from the work queue using the Hero protocol
|
||||
let result: redis::RedisResult<Option<String>> = conn.lpop(&work_queue_key, None).await;
|
||||
let job_id = self.client.get_job_id(&work_queue_key).await?;
|
||||
|
||||
match result {
|
||||
Ok(Some(job_id)) => {
|
||||
match job_id {
|
||||
Some(job_id) => {
|
||||
println!("📨 Received job ID: {}", job_id);
|
||||
if let Err(e) = self.process_job(&mut conn, &job_id).await {
|
||||
if let Err(e) = self.process_job(&job_id).await {
|
||||
eprintln!("❌ Error processing job {}: {}", job_id, e);
|
||||
// Mark job as error
|
||||
if let Err(e2) = Job::set_error(&mut conn, &job_id, &format!("Processing error: {}", e)).await {
|
||||
if let Err(e2) = self.client.set_job_status(&job_id, JobStatus::Error).await {
|
||||
eprintln!("❌ Failed to set job error status: {}", e2);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(None) => {
|
||||
None => {
|
||||
// No jobs available, wait a bit
|
||||
sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("❌ Redis error: {}", e);
|
||||
sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn process_job(&self, conn: &mut redis::aio::MultiplexedConnection, job_id: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
async fn process_job(&self, job_id: &str) -> Result<(), JobError> {
|
||||
// Load the job from Redis using the Hero job system
|
||||
let job = Job::load_from_redis(conn, job_id).await?;
|
||||
let job = self.client.get_job(job_id).await?;
|
||||
|
||||
println!("📝 Processing job: {}", job.id);
|
||||
println!("📝 Caller: {}", job.caller_id);
|
||||
println!("📝 Context: {}", job.context_id);
|
||||
println!("📝 Payload: {}", job.payload);
|
||||
println!("📝 Job Type: {:?}", job.job_type);
|
||||
self.process_job_internal(&self.client, job_id, &job).await
|
||||
}
|
||||
|
||||
async fn process_job_internal(
|
||||
&self,
|
||||
client: &Client,
|
||||
job_id: &str,
|
||||
job: &Job,
|
||||
) -> Result<(), JobError> {
|
||||
println!("🔄 Processing job {} with payload: {}", job_id, job.payload);
|
||||
|
||||
// Mark job as started
|
||||
Job::update_status(conn, job_id, JobStatus::Started).await?;
|
||||
client.set_job_status(job_id, JobStatus::Started).await?;
|
||||
println!("🚀 Job {} marked as Started", job_id);
|
||||
|
||||
// Simulate processing time
|
||||
@@ -140,10 +150,8 @@ impl MockRunner {
|
||||
println!("📤 Output: {}", output);
|
||||
|
||||
// Set the job result
|
||||
Job::set_result(conn, job_id, &output).await?;
|
||||
client.set_result(job_id, &output).await?;
|
||||
|
||||
// Mark job as finished
|
||||
Job::update_status(conn, job_id, JobStatus::Finished).await?;
|
||||
println!("✅ Job {} completed successfully", job_id);
|
||||
|
||||
Ok(())
|
||||
@@ -156,7 +164,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let config = MockRunnerConfig::from_args()?;
|
||||
|
||||
// Create and run the mock runner
|
||||
let runner = MockRunner::new(config)?;
|
||||
let runner = MockRunner::new(config).await?;
|
||||
runner.run().await?;
|
||||
|
||||
Ok(())
|
||||
|
Reference in New Issue
Block a user