use std::sync::mpsc;
use std::thread;
use tracing::{error, info};
use crate::bridge;
use crate::zts;
pub fn spawn_zts_worker(
worker_id: u32,
script: String,
task_rx: mpsc::Receiver<bridge::TaskRequest>,
ready_tx: mpsc::SyncSender<()>,
) -> thread::JoinHandle<()> {
thread::Builder::new()
.name(format!("folk-worker-{worker_id}"))
.spawn(move || {
if let Err(e) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
run_zts_worker(worker_id, &script, task_rx, ready_tx);
})) {
error!(worker_id, "ZTS worker thread panicked: {e:?}");
}
})
.expect("failed to spawn worker thread")
}
fn run_zts_worker(
worker_id: u32,
script: &str,
task_rx: mpsc::Receiver<bridge::TaskRequest>,
ready_tx: mpsc::SyncSender<()>,
) {
let _guard = zts::ZtsThreadGuard::new();
if let Err(e) = zts::request_startup() {
error!(worker_id, error = ?e, "php_request_startup failed");
return;
}
bridge::init_worker_state(worker_id, task_rx, ready_tx);
if let Err(e) = zts::execute_script(script) {
error!(worker_id, error = ?e, "worker script failed");
}
bridge::cleanup_worker_state();
zts::request_shutdown();
info!(worker_id, "ZTS worker thread exiting");
}