Files
leleka/control-plane/tests/backend_worker_http/second_worker.rs
T

85 lines
2.8 KiB
Rust

//! A second backend worker next to the stack's own, fed directly through its command channel the way the HTTP
//! layer feeds it.
use crate::harness::{Stack, runtime_config};
use leleka_backend_api::scripted::{ScriptedEngine, ScriptedHandle};
use std::sync::Arc;
use std::time::Duration;
use leleka_control_plane::backend_worker::{BackendWorkerConfig, WorkerInbox, run_backend_worker};
use leleka_control_plane::wire::{Event, Generate};
use leleka_control_plane::worker::{
Control, GenerationCommand, GenerationResult, WorkerState, WorkerStatus,
};
use tokio::sync::{RwLock, mpsc, oneshot, watch};
/// The worker, its engine handle and the senders that keep it running.
pub(super) struct SecondWorker {
pub(super) requests: mpsc::Sender<GenerationCommand>,
pub(super) handle: ScriptedHandle,
_stop: watch::Sender<bool>,
_control: mpsc::Sender<Control>,
}
fn worker_config(stack: &Stack) -> BackendWorkerConfig {
BackendWorkerConfig {
tokenizer: stack.tokenizer.clone(),
model: ScriptedEngine::fixture_descriptor(),
runtime: runtime_config(),
max_context: 262_144,
max_output_tokens: 4096,
health_interval: Duration::from_millis(20),
generation_timeout: None,
stall_timeout: None,
attach_retry: Duration::from_millis(20),
cancel_drain_timeout: Duration::from_secs(1),
prefix_view: None,
memory_view: None,
}
}
/// Starts the worker over a fresh scripted engine and waits until it is ready.
pub(super) async fn start(stack: &Stack) -> SecondWorker {
let (requests, requests_rx) = mpsc::channel(1);
let (_control, control_rx) = mpsc::channel(1);
let (engine, handle) = ScriptedEngine::new(ScriptedEngine::fixture_descriptor());
let status = Arc::new(RwLock::new(WorkerStatus::default()));
let (_stop, stop_rx) = watch::channel(false);
tokio::spawn(run_backend_worker(
Box::new(engine),
worker_config(stack),
status.clone(),
stop_rx,
WorkerInbox {
requests: requests_rx,
control: control_rx,
},
));
while status.read().await.state != WorkerState::Ready {
tokio::time::sleep(Duration::from_millis(5)).await;
}
SecondWorker {
requests,
handle,
_stop,
_control,
}
}
/// Sends `request` and waits for the buffered outcome.
pub(super) async fn submit(
requests: &mpsc::Sender<GenerationCommand>,
request: Generate,
) -> Result<Vec<Event>, String> {
let (sender, receiver) = oneshot::channel();
requests
.send(GenerationCommand {
queue: None,
activity: None,
request,
result: GenerationResult::Buffered(sender),
})
.await
.unwrap();
receiver.await.unwrap()
}