kernable-sdk 0.1.0

Write event-sourced business logic in Rust; the kernable kernel does the rest.
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
410
411
412
413
414
//! Apps: one service made of whole domains.

use std::{
    sync::{Arc, Mutex},
    time::{Duration, Instant},
};

use tokio::task::JoinSet;

use crate::{
    Domain, Error, Kernable, ListenerHandle, Manifest, Process, Switch, Worker,
    domain::Runnable,
    log::{self, Level, Overview, Row},
};

/// An app: one service made of whole domains, for [`Kernable::app`]. Every aggregate, read
/// model and reactor created through its domains runs in it. Built with methods that each
/// return the changed app:
///
/// `let app = kernable.app(App::new().service("shop").version("1.0.0").domain(&shop_domain))?;`
/// then `app.run().await?`. To do something once everything is connected (e.g. in a test),
/// spawn it and wait: `let running = tokio::spawn({ let app = app.clone(); async move {
/// app.run().await } }); app.ready().await;`.
#[derive(Clone, Default)]
pub struct App {
    service: Option<String>,
    version: Option<String>,
    description: Option<String>,
    metadata: Vec<(String, String)>,
    domains: Vec<Domain>,
    switch: Option<Switch>,
}

impl App {
    /// An app without domains for the connection's service; add them with [`App::domain`].
    pub fn new() -> App {
        App::default()
    }

    /// The service it is. Default: the connection's ([`crate::Options::service`]).
    pub fn service(mut self, service: impl Into<String>) -> App {
        self.service = Some(service.into());
        self
    }

    /// Goes into the membership's metadata as `version`.
    pub fn version(mut self, version: impl Into<String>) -> App {
        self.version = Some(version.into());
        self
    }

    /// Goes into the membership's metadata as `description`.
    pub fn description(mut self, description: impl Into<String>) -> App {
        self.description = Some(description.into());
        self
    }

    /// More metadata, after `version` and `description`; kept in the order given.
    pub fn metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> App {
        self.metadata.push((key.into(), value.into()));
        self
    }

    /// A domain of the app: its aggregates, read models and reactors run in it. Call it once per
    /// domain.
    pub fn domain(mut self, domain: &Domain) -> App {
        self.domains.push(domain.clone());
        self
    }

    /// When the app's deployment takes the service over from another one that serves it (see
    /// [`Switch`]): by default once its read models have caught up, if the deployment never
    /// served before; meanwhile it runs beside the serving deployment, its workers idle and its
    /// read models building. One that served before (an older deployment's app restarting) runs
    /// beside until an operator rolls back. Default:
    /// `KERNABLE_SWITCH` (`caught-up`, `now`, `never`), else [`Switch::CaughtUp`].
    pub fn switch(mut self, switch: Switch) -> App {
        self.switch = Some(switch);
        self
    }
}

struct AppInner {
    kernable: Kernable,
    /// The names of the app's domains.
    domains: Vec<String>,
    aggregates: Vec<Arc<dyn Runnable>>,
    manifest: Manifest,
    worker: Option<Worker>,
    listeners: Vec<ListenerHandle>,
    /// The domains' business processes, registered once the worker is ready.
    processes: Vec<Process>,
    /// Whether they are registered (also when there are none).
    registered: tokio::sync::watch::Sender<bool>,
    /// When it switches the service to its deployment.
    switch: Switch,
    /// Whether its membership is applied: its deployment serves the service.
    serving: tokio::sync::watch::Sender<bool>,
}

/// An [`App`] on a connection; see [`Kernable::app`]. Cloning gives another handle to the same
/// app, e.g. to call `stop()` or `ready()` while another task awaits `run()`.
#[derive(Clone)]
pub struct AppHandle {
    inner: Arc<AppInner>,
}

impl AppHandle {
    pub(crate) fn new(kernable: &Kernable, app: App) -> Result<AppHandle, Error> {
        let switch = match app.switch {
            Some(switch) => switch,
            None => Switch::from_env()?,
        };
        let service = app.service.unwrap_or_else(|| kernable.service.clone());
        if service.is_empty() {
            return Err(Error::Config(
                "an app needs a service: set it with App::service or Options::service".into(),
            ));
        }
        let aggregates: Vec<_> = app.domains.iter().flat_map(Domain::aggregates).collect();
        let listeners: Vec<_> = app.domains.iter().flat_map(Domain::listeners).collect();
        let mut manifest = Manifest::new().service(&service);
        manifest.workers = aggregates
            .iter()
            .map(|a| format!("{}.{}", a.domain().name(), a.name()))
            .collect();
        for listener in listeners.iter().filter(|l| !l.is_live()) {
            manifest = if listener.is_middleware() {
                manifest.middleware(listener)
            } else {
                manifest.listener(listener)
            };
        }
        let described = [("version", app.version), ("description", app.description)];
        for (key, value) in described {
            if let Some(value) = value {
                manifest = manifest.metadata(key, value);
            }
        }
        manifest.metadata.extend(app.metadata);
        let processes: Vec<Process> = app.domains.iter().flat_map(Domain::processes).collect();
        for process in &processes {
            // A typo stops the start, not a case.
            process.model()?;
            manifest = manifest.process(process);
        }
        let worker = (!aggregates.is_empty()).then(|| {
            let worker = Worker::new(kernable.client.clone(), service.clone());
            for aggregate in &aggregates {
                worker.runnable(Arc::clone(aggregate));
            }
            worker
        });
        let listeners = listeners
            .iter()
            .map(|l| ListenerHandle::new(kernable, l, service.clone()))
            .collect();
        let mut domains: Vec<String> = Vec::new();
        for domain in &app.domains {
            if !domains.iter().any(|d| d == domain.name()) {
                domains.push(domain.name().to_owned());
            }
        }
        Ok(AppHandle {
            inner: Arc::new(AppInner {
                kernable: kernable.clone(),
                domains,
                aggregates,
                manifest,
                worker,
                listeners,
                registered: tokio::sync::watch::Sender::new(processes.is_empty()),
                processes,
                switch,
                serving: tokio::sync::watch::Sender::new(false),
            }),
        })
    }

    /// The membership `run()` applies.
    pub fn manifest(&self) -> &Manifest {
        &self.inner.manifest
    }

    /// The worker of all the app's aggregates; none if it has none.
    pub fn worker(&self) -> Option<&Worker> {
        self.inner.worker.as_ref()
    }

    /// Every read model and reactor of the app, live ones too.
    pub fn listeners(&self) -> &[ListenerHandle] {
        &self.inner.listeners
    }

    /// Returns once the worker and every listener of the app are connected, its deployment
    /// serves the service ([`AppHandle::serving`]) and the domains' processes are registered;
    /// waits forever if one never connects, so wrap it in `tokio::time::timeout` where that
    /// matters.
    pub async fn ready(&self) {
        self.connected().await;
        self.serving().await;
        let mut registered = self.inner.registered.subscribe();
        let _ = registered.wait_for(|done| *done).await;
    }

    /// Returns once the worker and every listener of the app are connected; while another
    /// deployment serves, they are warming up beside it.
    pub async fn connected(&self) {
        if let Some(worker) = &self.inner.worker {
            worker.ready().await;
        }
        for listener in &self.inner.listeners {
            listener.ready().await;
        }
    }

    /// Returns once the app's membership is applied: its deployment serves the service. Right
    /// at the start, unless another deployment serves; then once the app's deployment has
    /// caught up and taken over ([`App::switch`]), or an operator switched to it.
    pub async fn serving(&self) {
        let mut serving = self.inner.serving.subscribe();
        let _ = serving.wait_for(|s| *s).await;
    }

    /// Ends `run`: stops the worker and every listener.
    pub fn stop(&self) {
        if let Some(worker) = &self.inner.worker {
            worker.stop();
        }
        for listener in &self.inner.listeners {
            listener.stop();
        }
    }

    /// Applies the membership (see [`Kernable::apply`]; it lists the domains' processes), then
    /// runs the worker and every listener until `stop()`. If another deployment serves the
    /// service, the kernel switches to this one only as [`App::switch`] says: until then the app
    /// runs beside it (workers idle, read models building) and applies its membership again
    /// every second. Once it serves and the worker is ready, it registers the domains' processes
    /// ([`Kernable::register_process`]): a process whose commands no worker runs yet would be
    /// refused. Once all are connected and it serves, it prints an overview of the app (unless
    /// `KERNABLE_LOG` is below `info`) instead of their connection lines. Fails only if the
    /// kernel refuses the membership for another reason than the switch ([`Error::Status`]); a
    /// failing handler does not end it (a command handler's error rejects the command, a
    /// listener handler's error makes the listener reconnect and get the event again), or the
    /// kernel refuses a process (then it stops). A panic in a worker or listener task is passed
    /// on.
    pub async fn run(&self) -> Result<(), Error> {
        let started = Instant::now();
        let manifest = self.inner.manifest.clone().switch(self.inner.switch);
        let applied = match self.inner.kernable.apply(manifest.clone()).await {
            Ok(()) => true,
            Err(e) if e.waits_for_switch() => {
                self.beside(&e);
                false
            }
            Err(e) => return Err(e),
        };
        if let Some(worker) = &self.inner.worker {
            worker.announce(false);
        }
        for listener in &self.inner.listeners {
            listener.announce(false);
        }
        let overview = {
            let app = self.clone();
            tokio::spawn(async move {
                app.ready().await;
                app.overview(started.elapsed().as_millis());
            })
        };
        let mut running = JoinSet::new();
        if let Some(worker) = self.inner.worker.clone() {
            running.spawn(async move { worker.run().await });
        }
        for listener in &self.inner.listeners {
            let listener = listener.clone();
            running.spawn(async move { listener.run().await });
        }
        let failed = Arc::new(Mutex::new(None));
        let setup = {
            let (app, failed) = (self.clone(), Arc::clone(&failed));
            tokio::spawn(async move {
                if !applied {
                    app.switch_later(manifest).await;
                }
                app.inner.serving.send_replace(true);
                if let Err(e) = app.register().await {
                    *failed.lock().unwrap() = Some(e);
                    app.stop();
                }
            })
        };
        while let Some(done) = running.join_next().await {
            if let Err(e) = done
                && e.is_panic()
            {
                std::panic::resume_unwind(e.into_panic());
            }
        }
        overview.abort();
        setup.abort();
        let failed = failed.lock().unwrap().take();
        failed.map_or(Ok(()), Err)
    }

    /// Logs why the app runs beside the deployment that serves.
    fn beside(&self, refused: &Error) {
        let deployment = self.inner.kernable.client.deployment.clone();
        let why = match refused {
            Error::Status(s) => s.message().to_owned(),
            other => other.to_string(),
        };
        let log = &self.inner.kernable.client.log;
        log.info("app", || {
            format!("deployment {deployment} runs beside the one that serves ({why})")
        });
    }

    /// Applies the membership every second until the kernel takes it.
    async fn switch_later(&self, manifest: Manifest) {
        loop {
            tokio::time::sleep(Duration::from_secs(1)).await;
            match self.inner.kernable.apply(manifest.clone()).await {
                Ok(()) => break,
                Err(e) if e.waits_for_switch() => {}
                Err(e) => {
                    let log = &self.inner.kernable.client.log;
                    log.warn("app", || format!("applying the membership failed: {e}"));
                }
            }
        }
        let deployment = self.inner.kernable.client.deployment.clone();
        let service = self.inner.manifest.service.clone().unwrap_or_default();
        let log = &self.inner.kernable.client.log;
        log.info("app", || {
            format!("deployment {deployment} serves {service} now")
        });
    }

    /// Registers the domains' processes once the worker is ready.
    async fn register(&self) -> Result<(), Error> {
        if self.inner.processes.is_empty() {
            return Ok(());
        }
        if let Some(worker) = &self.inner.worker {
            worker.ready().await;
        }
        for process in &self.inner.processes {
            self.inner.kernable.register_process(process).await?;
        }
        self.inner.registered.send_replace(true);
        Ok(())
    }

    /// Prints the app's overview, `ms` after `run()` started, if `info` lines are enabled.
    fn overview(&self, ms: u128) {
        let inner = &self.inner;
        let log = &inner.kernable.client.log;
        if !log.enabled(Level::Info) {
            return;
        }
        let domains = &inner.domains;
        // A single domain's own names go without it; with several, every name is qualified.
        let short = |domain: &str, name: &str| {
            if domains.len() == 1 && domains[0] == domain {
                name.to_owned()
            } else {
                format!("{domain}.{name}")
            }
        };
        let mut rows = Vec::new();
        for aggregate in &inner.aggregates {
            let mut types = aggregate.command_types();
            types.sort_unstable();
            rows.push(Row {
                kind: "worker",
                name: short(aggregate.domain().name(), aggregate.name()),
                details: vec![format!("runs {}", types.join(", "))],
            });
        }
        for handle in &inner.listeners {
            let listener = handle.listener();
            rows.push(Row {
                kind: if listener.is_live() {
                    "live"
                } else {
                    "listener"
                },
                name: short(listener.domain().name(), listener.name()),
                details: listener.details(handle.started_at(), &|t| log::relative(t, domains)),
            });
        }
        let m = &inner.manifest;
        let meta = |key: &str| {
            m.metadata
                .iter()
                .find(|(k, _)| k == key)
                .map(|(_, v)| v.clone())
        };
        let overview = Overview {
            service: m.service.clone().unwrap_or_default(),
            version: meta("version"),
            description: meta("description"),
            peer: log.peer.to_string(),
            workers: m.workers.len(),
            listeners: m.listeners.len(),
            middlewares: m.middlewares.len(),
            rows,
            ms,
        };
        log::write(&log::render_overview(&overview, log.color));
    }
}