Skip to main content

harmos_domain/
observers.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10// The built-in observe control `harmos.observe`. A sidecar that declares
11// observers serves it: the host submits it with the observer's identity, the
12// sidecar initializes and ticks that observer for as long as the invocation
13// runs, and cancelling the invocation stops and cleans it up through the
14// ordinary operation lifecycle.
15
16/* === Modules === */
17
18/* === Re-Exports === */
19
20/* === Imports === */
21
22// Standard Library Imports
23use std::borrow::Cow;
24// Third Party Imports
25
26// Local Imports
27use crate::operations::{Contract, ContractError, OperationPolicy, ReplaySafety, RestartPolicy, RetryPolicy, UNBOUNDED, Visibility};
28use crate::schema::declaration::{Field, Kind};
29use crate::schema::{Cardinality, Described};
30
31/* === Constants === */
32
33// The observe control runs until the host cancels it.
34pub const POLICY: OperationPolicy =
35    OperationPolicy { timeout: UNBOUNDED, attempt_timeout: UNBOUNDED, retries: RetryPolicy::Never, restart: RestartPolicy::Never };
36
37/* === Macros === */
38
39crate::declare!(Control {
40    id: "harmos.observe",
41    version: 1,
42    policy: POLICY,
43    response: (),
44    visibility: Visibility::Host,
45    replay: ReplaySafety::Idempotent,
46});
47
48/* === Type Aliases === */
49
50/* === Structs === */
51
52#[derive(Clone, PartialEq, Eq, Debug, Default)]
53pub struct Control {
54    pub observer: String,
55    pub generation: u64,
56}
57
58/* === Enums === */
59
60/* === Traits === */
61
62/* === Implementations === */
63
64crate::message::postcard_product!(Control { observer: String, generation: u64 });
65
66impl crate::MemorySize for Control {
67    fn memory_size(&self) -> usize {
68        std::mem::size_of::<Self>().saturating_add(self.observer.capacity())
69    }
70}
71
72impl Described for Control {
73    const NAME: &'static str = concat!(module_path!(), "::", stringify!(Control));
74    const FIELDS: &'static [Field] = &[
75        Field { name: Cow::Borrowed("observer"), tag: 1, kind: Kind::String, cardinality: Cardinality::Required },
76        Field { name: Cow::Borrowed("generation"), tag: 2, kind: Kind::U64, cardinality: Cardinality::Required },
77    ];
78    const MARKER: &'static [u8] = &crate::schema::declaration::encode::<
79        { crate::schema::declaration::encoded_len(Self::NAME, Self::FIELDS) },
80    >(Self::NAME, Self::FIELDS);
81}
82
83/* === Public Functions === */
84
85// The Contract Every Observer-Declaring Sidecar Serves.
86pub fn contract() -> Result<Contract, ContractError> {
87    Contract::reserved::<Control>(POLICY)
88}
89
90/* === Private Functions === */
91
92/* === Tests === */