schedr_model/ctx.rs
1//! Messages on a command task's context channel: `schedr dag-ctx` and `schedr.script` asking
2//! the agent for the run's parameters and information (decisions 143-148).
3//!
4//! A command task inherits one end of a datagram socket pair (`SCHEDR_CTX_FD`). Each call
5//! makes a fresh stream socket pair, sends one end to the agent over it (`SCM_RIGHTS`, one
6//! datagram) and talks on the other: a connection of its own, which no other process can
7//! join. On that connection the framing is the runner channel's (a 4-byte big-endian length,
8//! then UTF-8 JSON tagged by `type`), capped at [`MAX_CTX_MESSAGE_SIZE`]. The caller says
9//! [`CtxRequest::Hello`] first, with its protocol ([`CTX_PROTOCOL`]); the agent answers
10//! [`CtxAnswer::Welcome`] or [`CtxAnswer::Refused`], then one answer per request until the
11//! caller closes it.
12
13use schemars::JsonSchema;
14use serde::{Deserialize, Serialize};
15
16use crate::states::RunKind;
17
18#[cfg(test)]
19mod tests;
20
21/// The environment variable with a command task's descriptor of its context channel.
22pub const CTX_FD_ENV: &str = "SCHEDR_CTX_FD";
23
24/// The context channel's protocol (decision 148): the agent accepts a caller speaking the same
25/// one, whatever its schedr version. Within a protocol, changes are only additions (a new
26/// request, a new field), and either side ignores fields it doesn't know; anything else is a
27/// new protocol, a new value of an enumerated field included (a run kind: an older caller
28/// can't read it). `hello`'s shape never changes, so a refusal can always be told.
29pub const CTX_PROTOCOL: u32 = 1;
30
31/// The largest message on a context connection, in bytes (params are at most 64 KiB as JSON,
32/// decision 135; tags and escaping can grow them).
33pub const MAX_CTX_MESSAGE_SIZE: u32 = 1024 * 1024;
34
35/// A request from a process of the task to the agent.
36#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
37#[serde(tag = "type", rename_all = "snake_case")]
38pub enum CtxRequest {
39 /// The first message: the protocol the caller speaks ([`CTX_PROTOCOL`]), which must be
40 /// the agent's, and its schedr version (for the messages).
41 Hello {
42 /// The protocol.
43 protocol: u32,
44 /// The `schedr` binary's or Python package's version.
45 schedr_version: String,
46 },
47 /// The run's parameters.
48 ParamsGet,
49 /// The run information.
50 RunGet,
51 /// A secret the task declares (`secrets=`), or one under a declared prefix.
52 SecretGet {
53 /// Which secret.
54 secret: crate::secret::SecretName,
55 },
56 /// A request to schedr's REST API (`schedr task-ctx api`, M4.11h), run by the master as
57 /// the task.
58 ApiCall {
59 /// `GET`, `POST`, ...
60 method: String,
61 /// The path and its query, e.g. `/api/v1/events?name=paid`.
62 path: String,
63 /// The body, JSON; `None` for none.
64 #[serde(default, skip_serializing_if = "Option::is_none")]
65 body: Option<serde_json::Value>,
66 /// Its `Idempotency-Key`.
67 #[serde(default, skip_serializing_if = "Option::is_none")]
68 idempotency_key: Option<String>,
69 },
70 /// Registers a cleanup handle (`schedr task-ctx cleanup add`).
71 HandleAdd {
72 /// The handle.
73 handle: crate::runner::CleanupHandle,
74 },
75 /// The attempt's handles, in registration order (`schedr task-ctx cleanup list`).
76 HandleList,
77 /// A connection's values, rendered (decision 174): any connection of the task's root.
78 ConnectionGet {
79 /// The connection's name.
80 name: String,
81 },
82 /// A connection as a command gets it (decisions 184, 190): its files written in the
83 /// attempt's folder, and its variables (`task-ctx connection NAME --env PREFIX`).
84 ConnectionEnvGet {
85 /// The connection's name.
86 name: String,
87 /// The variables' prefix (`DB` gives `DB_URL`).
88 prefix: String,
89 },
90 /// Reports the value of the resource a `CommandResource` setup creates (`schedr task-ctx
91 /// resource set`), as JSON; checked against its declared type at once.
92 ResourceSet {
93 /// The value.
94 value: serde_json::Value,
95 },
96 /// The value of the resource a command cleanup cleans up (`schedr task-ctx resource get`).
97 ResourceGet,
98}
99
100/// The agent's answer to a [`CtxRequest`].
101#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
102#[serde(tag = "type", rename_all = "snake_case")]
103pub enum CtxAnswer {
104 /// The handshake succeeded.
105 Welcome,
106 /// The handshake failed (another protocol); the agent closes the connection.
107 Refused {
108 /// Why.
109 reason: String,
110 },
111 /// The run's parameters, as stored (tagged, decision 138); `{}` when the DAG takes none.
112 Params {
113 /// The values.
114 #[schemars(with = "serde_json::Map<String, serde_json::Value>")]
115 params: serde_json::Value,
116 },
117 /// The run information.
118 Run(CtxRun),
119 /// The value of the secret asked for: a key's value as a string, a whole secret as an
120 /// object of key to value.
121 Secret {
122 /// The value.
123 value: serde_json::Value,
124 },
125 /// Why the secret asked for can't be read; the connection stays open.
126 SecretFailed {
127 /// The error kind (`secret_unavailable`, `secret_denied`, `secret_not_found`).
128 kind: String,
129 /// What went wrong, naming the secret (never a value).
130 reason: String,
131 },
132 /// What the API answered an [`CtxRequest::ApiCall`].
133 ApiAnswer {
134 /// The HTTP status.
135 status: u16,
136 /// The body, JSON (`{"error": ...}` for a refusal).
137 body: serde_json::Value,
138 /// What the task is told of its request: a secret in it masked. Printed to its log.
139 #[serde(default, skip_serializing_if = "Option::is_none")]
140 note: Option<String>,
141 },
142 /// The handle is registered.
143 HandleAdded,
144 /// Why the handle isn't registered; the connection stays open.
145 HandleRefused {
146 /// What went wrong.
147 reason: String,
148 },
149 /// The attempt's handles, in registration order.
150 Handles {
151 /// The handles.
152 handles: Vec<crate::runner::CleanupHandle>,
153 },
154 /// The resource's value is reported.
155 ResourceReported,
156 /// The resource's value, as stored JSON (tagged).
157 Resource {
158 /// The value.
159 value: serde_json::Value,
160 },
161 /// Why the resource's value can't be reported or read; the connection stays open.
162 ResourceRefused {
163 /// What went wrong (never the value).
164 reason: String,
165 },
166 /// The values of the connection asked for, by parameter, as stored JSON (tagged).
167 Connection {
168 /// The values.
169 values: std::collections::BTreeMap<String, serde_json::Value>,
170 },
171 /// The variables of the connection asked for, by name (`DB_URL`), its files written.
172 ConnectionEnv {
173 /// The variables.
174 variables: std::collections::BTreeMap<String, String>,
175 },
176 /// Why the connection asked for can't be opened; the connection stays open.
177 ConnectionFailed {
178 /// The error kind (`connection_not_found`, `render_failed`, or a secret's).
179 kind: String,
180 /// What went wrong, naming the connection (never a value).
181 reason: String,
182 },
183 /// A request the agent can't answer (one it doesn't know, out of order); the connection
184 /// is closed after it.
185 Error {
186 /// Why.
187 reason: String,
188 },
189}
190
191/// The run information a task's processes can read (`schedr dag-ctx run get`).
192#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
193pub struct CtxRun {
194 /// The DAG's ID.
195 pub dag_id: String,
196 /// The run's ID (a `UUIDv7`).
197 pub id: String,
198 /// The run's kind.
199 pub kind: RunKind,
200 /// The moment the run is for.
201 pub planned_for: jiff::Timestamp,
202 /// The start of the run's window.
203 pub since: jiff::Timestamp,
204}