appstruct-runtime 0.2.0

Stable request identity and service lifecycle contracts for AppStruct applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Stable contracts shared by generated `AppStruct` backends.

pub mod bigint;
mod lifecycle;
mod origin;
mod supervisor;

pub use appstruct_core::{query, resource};
pub use appstruct_core::{sha256_hex, sha256_tagged};

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::fmt;
use std::time::Duration;

/// Source files embedded into generated backend projects.
#[doc(hidden)]
pub mod __source {
    pub const LIB: &str = include_str!("lib.rs");
    pub const BIGINT: &str = include_str!("bigint.rs");
    pub const LIFECYCLE: &str = include_str!("lifecycle.rs");
    pub const ORIGIN: &str = include_str!("origin.rs");
    pub const QUERY: &str = appstruct_core::__source::QUERY;
    pub const RESOURCE: &str = appstruct_core::__source::RESOURCE;
    pub const SUPERVISOR: &str = include_str!("supervisor.rs");
}

/// Preserve the difference between an omitted patch field and explicit JSON null.
///
/// # Errors
/// Returns the field deserializer error when a supplied value has the wrong type.
pub fn deserialize_patch<'de, D, T>(deserializer: D) -> Result<Option<Option<T>>, D::Error>
where
    D: serde::Deserializer<'de>,
    T: Deserialize<'de>,
{
    Option::<T>::deserialize(deserializer).map(Some)
}

pub use lifecycle::{
    ModuleDescriptor, ModuleEvent, ModuleObserver, ModulePhase, ModulePlan, ModuleRuntime,
    ModuleStarter,
};
pub use origin::validate_browser_origin;
pub use query::{
    FilterExpression, FilterOperator, MAX_LIST_PAGE, MAX_LIST_PAGE_SIZE, MAX_SEARCH_CHARS,
    like_contains_pattern, list_page_is_valid, search_term_is_valid,
};
pub use resource::{
    BulkDeleteInput, BulkFailure, BulkResult, BulkUpdateInput, CSV_EXPORT_PAGE_SIZE, CsvError,
    ListMeta, ListQuery, ListResponse, MAX_BULK_ITEMS, MAX_CSV_EXPORT_ROWS, MAX_CSV_IMPORT_ROWS,
    bulk_failure, bulk_request_size_is_valid, csv_cell, csv_escape, csv_json_value, decode_cursor,
    encode_cursor, parse_csv_rows, parse_revision_etag, revision_etag,
};
pub use supervisor::{
    BackgroundTaskExit, BackgroundTaskExitKind, BackgroundTaskObserver, SupervisedTaskHandle,
};

/// Version of the generated-backend/runtime contract.
pub const RUNTIME_API_VERSION: u32 = appstruct_contracts::RUNTIME_API.current;

pub const DEFAULT_SERVICE_SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(10);

#[must_use]
pub const fn supports_runtime_api(version: u32) -> bool {
    appstruct_contracts::RUNTIME_API.supports(version)
}

pub type TenantId = uuid::Uuid;

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Actor {
    pub id: uuid::Uuid,
    pub email: String,
    pub roles: Vec<String>,
}

impl Actor {
    #[must_use]
    pub fn has_role(&self, role: &str) -> bool {
        self.roles.iter().any(|candidate| candidate == role)
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StartupError {
    service: String,
    message: String,
    dependency_chain: Vec<String>,
    rolled_back: Vec<String>,
    rollback_failures: Vec<ShutdownFailure>,
}

impl StartupError {
    #[must_use]
    pub fn configuration(service: impl Into<String>, error: impl fmt::Display) -> Self {
        Self {
            service: service.into(),
            message: error.to_string(),
            dependency_chain: Vec::new(),
            rolled_back: Vec::new(),
            rollback_failures: Vec::new(),
        }
    }

    #[must_use]
    pub fn dependency(service: impl Into<String>, dependency: impl fmt::Display) -> Self {
        Self::configuration(
            service,
            format_args!("runtime dependency `{dependency}` was not started"),
        )
    }

    #[must_use]
    pub fn service(&self) -> &str {
        &self.service
    }

    #[must_use]
    pub fn dependency_chain(&self) -> &[String] {
        &self.dependency_chain
    }

    #[must_use]
    pub fn rolled_back(&self) -> &[String] {
        &self.rolled_back
    }

    #[must_use]
    pub fn rollback_failures(&self) -> &[ShutdownFailure] {
        &self.rollback_failures
    }

    #[must_use]
    pub fn with_runtime_context(
        mut self,
        dependency_chain: Vec<String>,
        rollback: ShutdownReport,
    ) -> Self {
        self.dependency_chain = dependency_chain;
        self.rolled_back = rollback.attempted;
        self.rollback_failures = rollback.failures;
        self
    }
}

impl fmt::Display for StartupError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "failed to initialize {}: {}",
            self.service, self.message
        )?;
        if !self.dependency_chain.is_empty() {
            write!(
                formatter,
                "; dependency chain: {}",
                self.dependency_chain.join(" -> ")
            )?;
        }
        if !self.rolled_back.is_empty() {
            write!(formatter, "; rolled back: {}", self.rolled_back.join(", "))?;
        }
        if !self.rollback_failures.is_empty() {
            write!(
                formatter,
                "; rollback failures: {}",
                self.rollback_failures
                    .iter()
                    .map(ToString::to_string)
                    .collect::<Vec<_>>()
                    .join(", ")
            )?;
        }
        Ok(())
    }
}

impl Error for StartupError {}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ShutdownError {
    message: String,
}

impl ShutdownError {
    #[must_use]
    pub fn new(error: impl fmt::Display) -> Self {
        Self {
            message: error.to_string(),
        }
    }
}

impl fmt::Display for ShutdownError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.message)
    }
}

impl Error for ShutdownError {}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ShutdownFailureKind {
    Error,
    Timeout,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ShutdownFailure {
    pub service: String,
    pub kind: ShutdownFailureKind,
    pub message: String,
}

impl fmt::Display for ShutdownFailure {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{}: {}", self.service, self.message)
    }
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ShutdownReport {
    pub attempted: Vec<String>,
    pub failures: Vec<ShutdownFailure>,
}

impl ShutdownReport {
    #[must_use]
    pub fn is_success(&self) -> bool {
        self.failures.is_empty()
    }
}

impl fmt::Display for ShutdownReport {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.failures.is_empty() {
            formatter.write_str("shutdown completed")
        } else {
            write!(
                formatter,
                "shutdown failed: {}",
                self.failures
                    .iter()
                    .map(ToString::to_string)
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        }
    }
}

#[async_trait]
pub trait ServiceHandle: Send {
    fn service(&self) -> &'static str;
    async fn shutdown(self: Box<Self>) -> Result<(), ShutdownError>;
}

#[derive(Default)]
pub struct ServiceHandles {
    handles: Vec<Box<dyn ServiceHandle>>,
}

impl ServiceHandles {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    pub fn push(&mut self, handle: impl ServiceHandle + 'static) {
        self.handles.push(Box::new(handle));
    }

    pub fn push_boxed(&mut self, handle: Box<dyn ServiceHandle>) {
        self.handles.push(handle);
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.handles.len()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.handles.is_empty()
    }

    pub async fn shutdown_reverse(&mut self) -> ShutdownReport {
        self.shutdown_reverse_with_timeout(DEFAULT_SERVICE_SHUTDOWN_TIMEOUT)
            .await
    }

    pub async fn shutdown_reverse_with_timeout(&mut self, timeout: Duration) -> ShutdownReport {
        let mut report = ShutdownReport {
            attempted: Vec::with_capacity(self.handles.len()),
            failures: Vec::new(),
        };
        while let Some(handle) = self.handles.pop() {
            let service = handle.service().to_owned();
            report.attempted.push(service.clone());
            match tokio::time::timeout(timeout, handle.shutdown()).await {
                Ok(Ok(())) => {}
                Ok(Err(error)) => report.failures.push(ShutdownFailure {
                    service,
                    kind: ShutdownFailureKind::Error,
                    message: error.to_string(),
                }),
                Err(_) => report.failures.push(ShutdownFailure {
                    service,
                    kind: ShutdownFailureKind::Timeout,
                    message: format!("did not stop within {} ms", timeout.as_millis()),
                }),
            }
        }
        report
    }
}

#[cfg(test)]
mod tests {
    use super::{
        RUNTIME_API_VERSION, ServiceHandle, ServiceHandles, ShutdownError, ShutdownFailureKind,
        supports_runtime_api,
    };
    use async_trait::async_trait;
    use std::sync::{Arc, Mutex};

    struct RecordingHandle {
        service: &'static str,
        stopped: Arc<Mutex<Vec<&'static str>>>,
    }

    #[async_trait]
    impl ServiceHandle for RecordingHandle {
        fn service(&self) -> &'static str {
            self.service
        }

        async fn shutdown(self: Box<Self>) -> Result<(), ShutdownError> {
            self.stopped.lock().unwrap().push(self.service);
            Ok(())
        }
    }

    #[tokio::test]
    async fn stops_services_in_reverse_startup_order() {
        let stopped = Arc::new(Mutex::new(Vec::new()));
        let mut handles = ServiceHandles::new();
        for service in ["auth", "jobs", "web"] {
            handles.push(RecordingHandle {
                service,
                stopped: Arc::clone(&stopped),
            });
        }
        let report = handles.shutdown_reverse().await;
        assert_eq!(report.attempted, ["web", "jobs", "auth"]);
        assert!(report.is_success());
        assert_eq!(*stopped.lock().unwrap(), ["web", "jobs", "auth"]);
        assert!(handles.is_empty());
    }

    struct FailingHandle {
        service: &'static str,
        hangs: bool,
    }

    #[async_trait]
    impl ServiceHandle for FailingHandle {
        fn service(&self) -> &'static str {
            self.service
        }

        async fn shutdown(self: Box<Self>) -> Result<(), ShutdownError> {
            if self.hangs {
                std::future::pending().await
            } else {
                Err(ShutdownError::new("injected shutdown error"))
            }
        }
    }

    #[tokio::test]
    async fn reports_shutdown_errors_and_timeouts_without_skipping_services() {
        let mut handles = ServiceHandles::new();
        handles.push(FailingHandle {
            service: "failure",
            hangs: false,
        });
        handles.push(FailingHandle {
            service: "timeout",
            hangs: true,
        });

        let report = handles
            .shutdown_reverse_with_timeout(std::time::Duration::from_millis(1))
            .await;
        assert_eq!(report.attempted, ["timeout", "failure"]);
        assert_eq!(report.failures.len(), 2);
        assert_eq!(report.failures[0].kind, ShutdownFailureKind::Timeout);
        assert_eq!(report.failures[1].kind, ShutdownFailureKind::Error);
    }

    #[test]
    fn runtime_api_compatibility_is_explicit() {
        assert!(supports_runtime_api(RUNTIME_API_VERSION));
        assert!(!supports_runtime_api(RUNTIME_API_VERSION + 1));
    }
}