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
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use anyhow::Context;

pub mod couch;
// pub mod couch2;
pub mod index;
pub mod jobs;
pub mod rss;
mod runtime;
pub mod server;
pub mod util;

use crate::rss::FeedManager;
use couch::{CouchDB, CouchManager};
pub use oas_common as common;
pub use oas_common::{types, Record, Reference, TypedValue, UntypedRecord};
pub use runtime::Runtime;

/// Main application state.
///
/// This struct has instances to the mostly stateless clients to other services (CouchDB,
/// Elasticsearch). It should be cheap clone.
#[derive(Clone, Debug)]
pub struct State {
    pub feed_manager: FeedManager,
    pub db_manager: CouchManager,
    pub db: couch::CouchDB,
    pub index_manager: index::IndexManager,
    pub jobs: jobs::JobManager,
    did_init: Arc<AtomicBool>,
}

impl State {
    pub fn new(
        db_manager: CouchManager,
        db: CouchDB,
        index_manager: index::IndexManager,
        feed_manager: FeedManager,
        jobs: jobs::JobManager,
    ) -> Self {
        Self {
            db_manager,
            db,
            index_manager,
            feed_manager,
            jobs,
            did_init: Arc::new(AtomicBool::new(false)),
        }
    }
    /// Asynchronously init all services.
    ///
    /// Currently errors on the first failing init.
    pub async fn init_all(&mut self) -> anyhow::Result<()> {
        if self
            .did_init
            .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
            .is_err()
        {
            return Ok(());
        }
        self.db_manager
            .init()
            .await
            .context("Failed to initialize CouchDB.")?;
        self.feed_manager
            .init(&self.db)
            .await
            .context("Failed to initialize RSS feed watcher")?;
        self.index_manager
            .init(Default::default())
            .await
            .context("Failed to initialize Elasticsearch.")?;
        Ok(())
    }
}