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
use crate::databases::redis::RedisClient;
use crate::errors::{handle_puff_result, PuffResult};

use crate::tasks::TaskQueue;
use crate::types::{Puff, Text};
use crate::web::client::PyHttpClient;
use futures_util::future::BoxFuture;

use std::cell::RefCell;
use std::collections::HashMap;

use crate::databases::postgres::PostgresClient;
use crate::databases::pubsub::PubSubClient;
use crate::graphql::PuffGraphqlConfig;
use crate::python::PythonDispatcher;
use std::sync::{Arc, Mutex};
use tokio::runtime::Handle;

use tracing::{error, info};

/// The central control structure for dispatching tasks onto coroutine workers.
/// All tasks in the same runtime will have access to the same dispatcher. The dispatcher contains
/// a reference to the parent Tokio Runtime as well as references to all coroutine workers.
pub type PuffContext = Arc<RealPuffContext>;

pub struct RealPuffContext {
    handle: Handle,
    http_client: HashMap<String, PyHttpClient>,
    redis: HashMap<String, RedisClient>,
    postgres: HashMap<String, PostgresClient>,
    python_dispatcher: Option<PythonDispatcher>,
    pubsub_client: HashMap<String, PubSubClient>,
    task_queue_client: HashMap<String, TaskQueue>,
    gql_roots: HashMap<String, PuffGraphqlConfig>,
}

// Context consists of a hierarchy of Contexts. PUFF_CONTEXT is the primary thread local that holds
// the current context. PUFF_CONTEXT_WAITING is a Mutex used to bootstrap the variable into a tokio
// thread. Since certain parts of the context require the tokio runtime, we have to start threads
// with no PUFF_CONTEXT set, build the PuffContext, and then set the PUFF_CONTEXT_WAITING mutex.
// If PUFF_CONTEXT is not set when trying to access it, it will look at the PUFF_CONTEXT_WAITING
// and set PUFF_CONTEXT for the thread. This allows us to lazily set the context while avoiding
// a mutex lock every time trying to access it.
thread_local! {
    pub static PUFF_CONTEXT_WAITING: RefCell<Option<Arc<Mutex<Option<PuffContext>>>>> = RefCell::new(None);
    pub static PUFF_CONTEXT: RefCell<Option<PuffContext >> = RefCell::new(None);
}

pub fn set_puff_context(context: PuffContext) {
    PUFF_CONTEXT.with(|d| *d.borrow_mut() = Some(context.puff()));
}

pub fn set_puff_context_waiting(context: Arc<Mutex<Option<PuffContext>>>) {
    PUFF_CONTEXT_WAITING.with(|d| *d.borrow_mut() = Some(context.clone()));
}

pub fn is_puff_context_ready() -> bool {
    PUFF_CONTEXT_WAITING.with(|d| d.borrow().is_some())
}

pub fn with_puff_context<F: FnOnce(PuffContext) -> R, R>(f: F) -> R {
    let maybe_context = PUFF_CONTEXT.with(|d| d.borrow().clone());

    let context = match maybe_context {
        Some(v) => v,
        None => PUFF_CONTEXT_WAITING.with(|w| match w.borrow().clone() {
            Some(r) => {
                let locked = r.lock().unwrap();
                match &*locked {
                    Some(v) => {
                        set_puff_context(v.puff());
                        v.puff()
                    }
                    None => {
                        panic!("Accessed puff context before context was set.")
                    }
                }
            }
            None => {
                panic!("Context can only be used from a puff context.")
            }
        }),
    };
    f(context)
}

pub fn with_context(context: PuffContext) {
    PUFF_CONTEXT.with(|d| *d.borrow_mut() = Some(context.puff()));
}

impl RealPuffContext {
    /// Creates an empty RuntimeDispatcher with no active threads for testing.
    pub fn empty(handle: Handle) -> PuffContext {
        Arc::new(Self {
            handle,
            redis: HashMap::new(),
            postgres: HashMap::new(),
            python_dispatcher: None,
            pubsub_client: HashMap::new(),
            task_queue_client: HashMap::new(),
            gql_roots: HashMap::new(),
            http_client: HashMap::new(),
        })
    }

    /// Creates a new RuntimeDispatcher using the supplied `RuntimeConfig`.
    pub fn new(handle: Handle) -> PuffContext {
        Self::new_with_options(
            handle,
            HashMap::new(),
            HashMap::new(),
            None,
            HashMap::new(),
            HashMap::new(),
            HashMap::new(),
            HashMap::new(),
        )
    }

    /// Creates a new RuntimeDispatcher using the supplied `RuntimeConfig`. This function will start
    /// the number of `coroutine_threads` specified in your config. Includes options.
    pub fn new_with_options(
        handle: Handle,
        redis: HashMap<String, RedisClient>,
        postgres: HashMap<String, PostgresClient>,
        python_dispatcher: Option<PythonDispatcher>,
        pubsub_client: HashMap<String, PubSubClient>,
        task_queue_client: HashMap<String, TaskQueue>,
        gql_roots: HashMap<String, PuffGraphqlConfig>,
        http_client: HashMap<String, PyHttpClient>,
    ) -> PuffContext {
        let ctx = Self {
            handle,
            redis,
            postgres,
            python_dispatcher,
            pubsub_client,
            task_queue_client,
            gql_roots,
            http_client,
        };

        Arc::new(ctx)
    }

    /// A Handle into the multi-threaded async runtime
    pub fn handle(&self) -> Handle {
        self.handle.clone()
    }

    /// The global configured PubSubClient
    pub fn pubsub(&self) -> PubSubClient {
        self.pubsub_named("default")
    }

    /// The named configured PubSubClient
    pub fn pubsub_named(&self, key: &str) -> PubSubClient {
        self.pubsub_client
            .get(key)
            .expect("PubSub is not configured for this runtime.")
            .clone()
    }

    /// The global configured reqwest::Client
    pub fn http_client(&self) -> PyHttpClient {
        self.http_client_named("default")
    }

    /// The global configured reqwest::Client
    pub fn http_client_named(&self, key: &str) -> PyHttpClient {
        self.http_client
            .get(key)
            .expect("HttpClient is not configured for this runtime.")
            .clone()
    }

    /// The global configured PubSubClient
    pub fn task_queue(&self) -> TaskQueue {
        self.task_queue_named("default")
    }

    /// The global configured PubSubClient
    pub fn task_queue_named(&self, key: &str) -> TaskQueue {
        self.task_queue_client
            .get(key)
            .expect("TaskQueue is not configured for this runtime.")
            .clone()
    }

    /// A Handle into the multi-threaded async runtime
    pub fn python_dispatcher(&self) -> PythonDispatcher {
        self.python_dispatcher
            .clone()
            .expect("Python is not configured for this runtime.")
    }

    /// The configured redis client. Panics if not enabled.
    pub fn redis(&self) -> RedisClient {
        self.redis_named("default")
    }

    /// The configured redis client. Panics if not enabled.
    pub fn redis_named(&self, key: &str) -> RedisClient {
        self.redis
            .get(key)
            .expect(&format!(
                "Redis named {} is not configured for this runtime.",
                key
            ))
            .clone()
    }

    /// The configured postgres client. Panics if not enabled.
    pub fn postgres(&self) -> PostgresClient {
        self.postgres_named("default")
    }

    /// The configured postgres client.
    pub fn postgres_safe(&self) -> Option<PostgresClient> {
        self.postgres_safe_named("default")
    }

    /// The configured postgres client. Panics if not enabled.
    pub fn postgres_named(&self, key: &str) -> PostgresClient {
        self.postgres
            .get(key)
            .expect(&format!(
                "Postgres named {} is not configured for this runtime.",
                key
            ))
            .clone()
    }

    /// The configured postgres client.
    pub fn postgres_safe_named(&self, key: &str) -> Option<PostgresClient> {
        self.postgres.get(key).map(|c| c.clone())
    }

    /// The configured graphql root node. Panics if not enabled.
    pub fn gql(&self) -> PuffGraphqlConfig {
        self.gql_named("default")
    }

    /// The configured named graphql root node. Panics if not enabled.
    pub fn gql_named(&self, key: &str) -> PuffGraphqlConfig {
        self.gql_roots
            .get(key)
            .expect(&format!(
                "Graphql named {} is not configured for this runtime.",
                key
            ))
            .clone()
    }
}

impl Puff for PuffContext {}

pub fn supervised_task<F: Fn() -> BoxFuture<'static, PuffResult<()>> + Send + Sync + 'static>(
    context: PuffContext,
    _task_name: Text,
    f: F,
) {
    let handle = context.handle();
    let inner_handle = handle.clone();
    handle.spawn(async move {
        loop {
            info!("Supervising {_task_name}");
            let result = inner_handle.spawn(f()).await;
            match result {
                Ok(r) => {
                    let label = format!("Task {}", _task_name);
                    handle_puff_result(label.as_str(), r)
                }
                Err(_e) => {
                    error!("Task {_task_name} unexpected error : {_e}")
                }
            }
        }
    });
}