ya_runtime_sdk/
runtime.rs

1use 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
22/// Command handling interface for runtimes
23pub trait Runtime: RuntimeDef {
24    const MODE: RuntimeMode = RuntimeMode::Server;
25
26    /// Deploy and configure the runtime
27    fn deploy<'a>(&mut self, ctx: &mut Context<Self>) -> OutputResponse<'a>;
28
29    /// Start the runtime
30    fn start<'a>(&mut self, ctx: &mut Context<Self>) -> OutputResponse<'a>;
31
32    /// Stop the runtime
33    fn stop<'a>(&mut self, _ctx: &mut Context<Self>) -> EmptyResponse<'a> {
34        async move { Ok(()) }.boxed_local()
35    }
36
37    /// Start a runtime command
38    fn run_command<'a>(
39        &mut self,
40        command: RunProcess,
41        mode: RuntimeMode,
42        ctx: &mut Context<Self>,
43    ) -> ProcessIdResponse<'a>;
44
45    /// Stop runtime command execution
46    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    /// Output a market Offer template stub
55    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    /// Perform a self-test
66    fn test<'a>(&mut self, _ctx: &mut Context<Self>) -> EmptyResponse<'a> {
67        async move { Ok(()) }.boxed_local()
68    }
69
70    /// Join a VPN network
71    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
80/// Runtime definition trait.
81/// Auto-generated with `#[derive(RuntimeDef)]`
82pub 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/// Defines the mode of execution for commands within the runtime.
91#[derive(Clone, Copy, Debug)]
92pub enum RuntimeMode {
93    /// Server (blocking) mode
94    /// Uses Runtime API to communicate with the ExeUnit Supervisor.
95    /// `Command::Deploy` remains a one-shot command.
96    Server,
97    /// One-shot execution mode
98    /// Each command is a separate invocation of the runtime binary.
99    Command,
100}
101
102/// Runtime control helper
103#[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}