Update runner to use engine factory pattern with Rhai packages

- Reverted from generic Package approach to factory function pattern
- Factory creates Engine::new_raw() + registers package (efficient)
- AsyncRunner and SyncRunner use engine_factory: Arc<dyn Fn() -> Engine>
- Each job gets a fresh engine instance (cheap with factory pattern)
- Package is created once inside factory, engines reuse it
This commit is contained in:
Timur Gordon
2025-10-28 12:20:45 +01:00
parent 0c918a8f5f
commit 564b81b708
3 changed files with 8 additions and 9 deletions

View File

@@ -1,6 +1,6 @@
use crate::job::Job; use crate::job::Job;
use log::{debug, error, info}; use log::{debug, error, info};
use rhai::Engine; use rhai::{Engine, packages::Package};
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
@@ -167,7 +167,7 @@ impl Runner for AsyncRunner {
// Spawn the job execution task // Spawn the job execution task
let job_handle = tokio::spawn(async move { let job_handle = tokio::spawn(async move {
// Create a new engine instance for this job // Create a new engine instance (cheap with factory pattern)
let mut engine = engine_factory(); let mut engine = engine_factory();
let mut db_config = rhai::Map::new(); let mut db_config = rhai::Map::new();
db_config.insert("DB_PATH".into(), db_path_clone.into()); db_config.insert("DB_PATH".into(), db_path_clone.into());

View File

@@ -1,9 +1,8 @@
use crate::job::Job; use crate::job::Job;
use log::{debug, error, info};
use rhai::{Dynamic, Engine};
use std::sync::Arc;
use crate::runner_trait::Runner; use crate::runner_trait::Runner;
use log::{debug, error, info};
use rhai::{Engine, Dynamic};
use std::sync::Arc;
/// Configuration for sync runner instances /// Configuration for sync runner instances
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@@ -73,7 +72,7 @@ impl Runner for SyncRunner {
debug!("Sync Runner '{}', Job {}: Processing started.", runner_id, job_id); debug!("Sync Runner '{}', Job {}: Processing started.", runner_id, job_id);
info!("Sync Runner '{}' processing job_id: {}. Script: {:.50}...", job.context_id, job_id, job.payload); info!("Sync Runner '{}' processing job_id: {}. Script: {:.50}...", job.context_id, job_id, job.payload);
// Create a new engine instance for this job execution // Create a new engine instance (cheap with factory pattern)
let mut engine = (self.engine_factory)(); let mut engine = (self.engine_factory)();
// Execute the script // Execute the script