|
| 1 | +use crate::error::TaskError; |
| 2 | +use crate::Command; |
| 3 | +use feos_proto::task_service::{ |
| 4 | + task_service_server::TaskService, CreateRequest, CreateResponse, DeleteRequest, DeleteResponse, |
| 5 | + KillRequest, KillResponse, StartRequest, StartResponse, WaitRequest, WaitResponse, |
| 6 | +}; |
| 7 | +use log::info; |
| 8 | +use tokio::sync::{mpsc, oneshot}; |
| 9 | +use tonic::{Request, Response, Status}; |
| 10 | + |
| 11 | +pub struct TaskApiHandler { |
| 12 | + dispatcher_tx: mpsc::Sender<Command>, |
| 13 | +} |
| 14 | + |
| 15 | +impl TaskApiHandler { |
| 16 | + pub fn new(dispatcher_tx: mpsc::Sender<Command>) -> Self { |
| 17 | + Self { dispatcher_tx } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +/// Helper function to create a command, send it to the dispatcher, and await the response. |
| 22 | +async fn dispatch_and_wait<T, F>( |
| 23 | + dispatcher: &mpsc::Sender<Command>, |
| 24 | + command_constructor: F, |
| 25 | +) -> Result<Response<T>, Status> |
| 26 | +where |
| 27 | + F: FnOnce(oneshot::Sender<Result<T, TaskError>>) -> Command, |
| 28 | +{ |
| 29 | + let (resp_tx, resp_rx) = oneshot::channel(); |
| 30 | + let cmd = command_constructor(resp_tx); |
| 31 | + |
| 32 | + dispatcher |
| 33 | + .send(cmd) |
| 34 | + .await |
| 35 | + .map_err(|e| Status::internal(format!("Failed to send command to dispatcher: {}", e)))?; |
| 36 | + |
| 37 | + match resp_rx.await { |
| 38 | + Ok(Ok(result)) => Ok(Response::new(result)), |
| 39 | + Ok(Err(e)) => Err(e.into()), |
| 40 | + Err(_) => Err(Status::internal( |
| 41 | + "Dispatcher task dropped response channel.", |
| 42 | + )), |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +#[tonic::async_trait] |
| 47 | +impl TaskService for TaskApiHandler { |
| 48 | + async fn create( |
| 49 | + &self, |
| 50 | + request: Request<CreateRequest>, |
| 51 | + ) -> Result<Response<CreateResponse>, Status> { |
| 52 | + info!( |
| 53 | + "API: Received Create request for {}", |
| 54 | + request.get_ref().container_id |
| 55 | + ); |
| 56 | + dispatch_and_wait(&self.dispatcher_tx, |responder| Command::Create { |
| 57 | + req: request.into_inner(), |
| 58 | + responder, |
| 59 | + }) |
| 60 | + .await |
| 61 | + } |
| 62 | + |
| 63 | + async fn start( |
| 64 | + &self, |
| 65 | + request: Request<StartRequest>, |
| 66 | + ) -> Result<Response<StartResponse>, Status> { |
| 67 | + info!( |
| 68 | + "API: Received Start request for {}", |
| 69 | + request.get_ref().container_id |
| 70 | + ); |
| 71 | + dispatch_and_wait(&self.dispatcher_tx, |responder| Command::Start { |
| 72 | + req: request.into_inner(), |
| 73 | + responder, |
| 74 | + }) |
| 75 | + .await |
| 76 | + } |
| 77 | + |
| 78 | + async fn kill(&self, request: Request<KillRequest>) -> Result<Response<KillResponse>, Status> { |
| 79 | + info!( |
| 80 | + "API: Received Kill request for {}", |
| 81 | + request.get_ref().container_id |
| 82 | + ); |
| 83 | + dispatch_and_wait(&self.dispatcher_tx, |responder| Command::Kill { |
| 84 | + req: request.into_inner(), |
| 85 | + responder, |
| 86 | + }) |
| 87 | + .await |
| 88 | + } |
| 89 | + |
| 90 | + async fn delete( |
| 91 | + &self, |
| 92 | + request: Request<DeleteRequest>, |
| 93 | + ) -> Result<Response<DeleteResponse>, Status> { |
| 94 | + info!( |
| 95 | + "API: Received Delete request for {}", |
| 96 | + request.get_ref().container_id |
| 97 | + ); |
| 98 | + dispatch_and_wait(&self.dispatcher_tx, |responder| Command::Delete { |
| 99 | + req: request.into_inner(), |
| 100 | + responder, |
| 101 | + }) |
| 102 | + .await |
| 103 | + } |
| 104 | + |
| 105 | + async fn wait(&self, request: Request<WaitRequest>) -> Result<Response<WaitResponse>, Status> { |
| 106 | + info!( |
| 107 | + "API: Received Wait request for {}", |
| 108 | + request.get_ref().container_id |
| 109 | + ); |
| 110 | + dispatch_and_wait(&self.dispatcher_tx, |responder| Command::Wait { |
| 111 | + req: request.into_inner(), |
| 112 | + responder, |
| 113 | + }) |
| 114 | + .await |
| 115 | + } |
| 116 | +} |
0 commit comments