use anyhow::Result;
use async_trait::async_trait;
use folk_core::runtime::{Runtime, WorkerHandle};
use tracing::info;
use crate::handle::EmbedWorkerHandle;
use crate::php::PhpInstance;
use crate::worker::spawn_worker_thread;
#[derive(Debug, Clone)]
pub struct EmbedConfig {
pub script: Option<String>,
}
pub struct EmbedRuntime {
config: EmbedConfig,
_php_module: PhpInstance,
}
impl EmbedRuntime {
pub fn new(config: EmbedConfig) -> Result<Self> {
info!("initializing PHP embed module");
let php = PhpInstance::boot_custom_sapi()?;
info!("PHP embed module ready");
Ok(Self {
config,
_php_module: php,
})
}
}
#[async_trait]
impl Runtime for EmbedRuntime {
async fn spawn(&self) -> Result<Box<dyn WorkerHandle>> {
let (thread, cmd_tx, task_resp_rx, control_rx, worker_id) =
spawn_worker_thread(self.config.script.clone());
Ok(Box::new(EmbedWorkerHandle::new(
worker_id,
cmd_tx,
task_resp_rx,
control_rx,
thread,
)))
}
}