posemesh_compute_node_runner_api/
runner.rs1use crate::types::LeaseEnvelope;
2use anyhow::Result;
3use async_trait::async_trait;
4use std::path::PathBuf;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct MaterializedInput {
9 pub cid: String,
10 pub path: PathBuf,
11 pub data_id: Option<String>,
12 pub name: Option<String>,
13 pub data_type: Option<String>,
14 pub domain_id: Option<String>,
15 pub root_dir: PathBuf,
16 pub related_files: Vec<PathBuf>,
17 pub extracted_paths: Vec<PathBuf>,
18}
19
20impl MaterializedInput {
21 pub fn new(cid: impl Into<String>, path: PathBuf) -> Self {
22 let root_dir = path
23 .parent()
24 .map(|p| p.to_path_buf())
25 .unwrap_or_else(|| path.clone());
26 Self {
27 cid: cid.into(),
28 path,
29 data_id: None,
30 name: None,
31 data_type: None,
32 domain_id: None,
33 root_dir,
34 related_files: Vec::new(),
35 extracted_paths: Vec::new(),
36 }
37 }
38}
39
40#[async_trait]
42pub trait InputSource: Send + Sync {
43 async fn get_bytes_by_cid(&self, cid: &str) -> Result<Vec<u8>>;
45
46 async fn materialize_cid_to_temp(&self, cid: &str) -> Result<std::path::PathBuf>;
48
49 async fn materialize_cid_with_meta(&self, cid: &str) -> Result<MaterializedInput> {
51 let path = self.materialize_cid_to_temp(cid).await?;
52 Ok(MaterializedInput::new(cid, path))
53 }
54}
55
56#[async_trait]
58pub trait ArtifactSink: Send + Sync {
59 async fn put_bytes(&self, rel_path: &str, bytes: &[u8]) -> Result<()>;
61
62 async fn put_file(&self, rel_path: &str, file_path: &std::path::Path) -> Result<()>;
64
65 async fn open_multipart(&self, _rel_path: &str) -> Result<Box<dyn MultipartUpload>> {
67 Err(anyhow::anyhow!("multipart not supported"))
68 }
69}
70
71#[async_trait]
73pub trait MultipartUpload: Send + Sync {
74 async fn write_chunk(&mut self, chunk: &[u8]) -> Result<()>;
76 async fn finish(self: Box<Self>) -> Result<()>;
78}
79
80#[async_trait]
82pub trait ControlPlane: Send + Sync {
83 async fn is_cancelled(&self) -> bool;
85
86 async fn progress(&self, value: serde_json::Value) -> Result<()>;
88
89 async fn log_event(&self, fields: serde_json::Value) -> Result<()>;
91}
92
93pub struct TaskCtx<'a> {
95 pub lease: &'a LeaseEnvelope,
96 pub input: &'a dyn InputSource,
97 pub output: &'a dyn ArtifactSink,
98 pub ctrl: &'a dyn ControlPlane,
99}
100
101#[async_trait]
103pub trait Runner: Send + Sync {
104 fn capability(&self) -> &'static str;
106
107 async fn run(&self, ctx: TaskCtx<'_>) -> Result<()>;
109}