ya_runtime_sdk/
runtime.rs1use std::cell::RefCell;
2use std::rc::Rc;
3
4use futures::channel::oneshot;
5use futures::future::LocalBoxFuture;
6use futures::FutureExt;
7use serde::{Deserialize, Serialize};
8
9use crate::cli::CommandCli;
10use crate::context::Context;
11use crate::error::Error;
12use crate::runtime_api::server::*;
13
14use ya_runtime_api::deploy::ContainerEndpoint;
15
16pub type ProcessId = u64;
17pub type EmptyResponse<'a> = LocalBoxFuture<'a, Result<(), Error>>;
18pub type OutputResponse<'a> = LocalBoxFuture<'a, Result<Option<serde_json::Value>, Error>>;
19pub type EndpointResponse<'a> = LocalBoxFuture<'a, Result<ContainerEndpoint, Error>>;
20pub type ProcessIdResponse<'a> = LocalBoxFuture<'a, Result<ProcessId, Error>>;
21
22pub trait Runtime: RuntimeDef {
24 const MODE: RuntimeMode = RuntimeMode::Server;
25
26 fn deploy<'a>(&mut self, ctx: &mut Context<Self>) -> OutputResponse<'a>;
28
29 fn start<'a>(&mut self, ctx: &mut Context<Self>) -> OutputResponse<'a>;
31
32 fn stop<'a>(&mut self, _ctx: &mut Context<Self>) -> EmptyResponse<'a> {
34 async move { Ok(()) }.boxed_local()
35 }
36
37 fn run_command<'a>(
39 &mut self,
40 command: RunProcess,
41 mode: RuntimeMode,
42 ctx: &mut Context<Self>,
43 ) -> ProcessIdResponse<'a>;
44
45 fn kill_command<'a>(
47 &mut self,
48 _kill: KillProcess,
49 _ctx: &mut Context<Self>,
50 ) -> EmptyResponse<'a> {
51 async move { Err(Error::from_string("Not supported")) }.boxed_local()
52 }
53
54 fn offer<'a>(&mut self, _ctx: &mut Context<Self>) -> OutputResponse<'a> {
56 async move {
57 Ok(Some(crate::serialize::json::json!({
58 "constraints": "",
59 "properties": {}
60 })))
61 }
62 .boxed_local()
63 }
64
65 fn test<'a>(&mut self, _ctx: &mut Context<Self>) -> EmptyResponse<'a> {
67 async move { Ok(()) }.boxed_local()
68 }
69
70 fn join_network<'a>(
72 &mut self,
73 _network: CreateNetwork,
74 _ctx: &mut Context<Self>,
75 ) -> EndpointResponse<'a> {
76 async move { Err(Error::from_string("Not supported")) }.boxed_local()
77 }
78}
79
80pub trait RuntimeDef {
83 const NAME: &'static str;
84 const VERSION: &'static str;
85
86 type Cli: CommandCli;
87 type Conf: Default + Serialize + for<'de> Deserialize<'de>;
88}
89
90#[derive(Clone, Copy, Debug)]
92pub enum RuntimeMode {
93 Server,
97 Command,
100}
101
102#[derive(Clone, Default)]
104pub struct RuntimeControl {
105 pub(crate) shutdown_tx: Rc<RefCell<Option<oneshot::Sender<()>>>>,
106}
107
108impl RuntimeControl {
109 pub fn shutdown(&mut self) {
110 if let Some(tx) = self.shutdown_tx.borrow_mut().take() {
111 let _ = tx.send(());
112 }
113 }
114}