Skip to main content

tierkreis_runtime/workers/
mod.rs

1//! Workers manage connections to external processes that may run functions
2//! and/or (in the case of Tierkreis servers) graphs.
3//!
4//! A server is itself a *client* of its "children" (themselves servers),
5//! thus giving rise to terminology descendants, ancestors, etc. [Location]s
6//! identify nodes in this tree and we use the two interchangeably.  Note that
7//! the link between a server and its "parent" is transitory, i.e. "parent" (and
8//! equivalently "client") is meaningful only for the duration of a request.
9use std::collections::HashMap;
10
11use crate::operations::RuntimeOperation;
12use crate::RunGraphError;
13use tierkreis_core::graph::{Graph, Value};
14use tierkreis_core::symbol::{FunctionName, Label, Location};
15use tierkreis_proto::messages::Callback;
16use tierkreis_proto::protos_gen::v1alpha1::signature as ps;
17
18mod external;
19mod local;
20
21pub use external::{
22    AuthInjector, CallbackForwarder, ClientInterceptor, EscapeHatch, ExternalWorker,
23    FunctionForwarder,
24};
25pub use local::LocalWorker;
26use tonic::async_trait;
27
28/// All workers must be able to provide their signature
29#[async_trait]
30pub trait Worker: FunctionWorker + Send + Sync {
31    /// Returns the signature of the worker.
32    async fn signature(&self, location: Location) -> anyhow::Result<ps::ListFunctionsResponse>;
33
34    /// Return a runtime worker, if it is one
35    fn to_runtime_worker(&self) -> Option<&dyn RuntimeWorker>;
36}
37
38/// FunctionWorkers can run functions.
39pub trait FunctionWorker: Send + Sync {
40    /// Returns a [`RuntimeOperation`] that runs the named function;
41    /// the operation will return an error if the function is unknown.
42    fn spawn(&self, function: &FunctionName, loc: &Location) -> RuntimeOperation;
43}
44
45/// RuntimeWorkers are able to run graphs
46#[async_trait]
47pub trait RuntimeWorker: Send + Sync {
48    /// Executes a (sub)graph, i.e. as a single step, given a complete map of input values,
49    /// and producing a map of output values.
50    /// `location` is the location *inside* the target of this request, i.e. relative path.
51    /// `escape` allows the subgraph to request operations defined only in an ancestor location.
52    async fn execute_graph(
53        &self,
54        graph: Graph,
55        inputs: HashMap<Label, Value>,
56        location: Location,
57        type_check: bool,
58        escape: Option<Callback>,
59    ) -> Result<HashMap<Label, Value>, RunGraphError>;
60
61    /// Returns a [`RuntimeOperation`] for running a graph on a specified location.
62    fn spawn_graph(&self, graph: Graph, location: &Location) -> RuntimeOperation;
63
64    /// Infer a type on a worker
65    async fn infer_type(
66        &self,
67        to_check: Value,
68        location: Location,
69    ) -> anyhow::Result<ps::InferTypeResponse>;
70}