fuel_core_services/
service.rs

1use crate::{
2    state::{
3        State,
4        StateWatcher,
5    },
6    Shared,
7};
8use anyhow::anyhow;
9use fuel_core_metrics::futures::{
10    future_tracker::FutureTracker,
11    FuturesMetrics,
12};
13use futures::FutureExt;
14use std::any::Any;
15use tokio::sync::watch;
16use tracing::Instrument;
17
18/// Used if services have no asynchronously shared data
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub struct EmptyShared;
21
22/// Trait for service runners, providing a minimal interface for managing
23/// the lifecycle of services such as start/stop and health status.
24#[async_trait::async_trait]
25pub trait Service {
26    /// Send a start signal to the service without waiting for it to start.
27    /// Returns an error if the service was already started.
28    fn start(&self) -> anyhow::Result<()>;
29
30    /// Send a start signal to the service and wait for it to start up.
31    /// Returns an error if the service was already started.
32    async fn start_and_await(&self) -> anyhow::Result<State>;
33
34    /// Wait for service to start or stop (without sending any signal).
35    async fn await_start_or_stop(&self) -> anyhow::Result<State>;
36
37    /// Send a stop signal to the service without waiting for it to shutdown.
38    /// Returns false if the service was already stopped, true if it is running.
39    fn stop(&self) -> bool;
40
41    /// Send stop signal to service and wait for it to shutdown.
42    async fn stop_and_await(&self) -> anyhow::Result<State>;
43
44    /// Wait for service to stop (without sending a stop signal).
45    async fn await_stop(&self) -> anyhow::Result<State>;
46
47    /// The current state of the service (i.e. `Started`, `Stopped`, etc..)
48    fn state(&self) -> State;
49
50    /// Returns the state watcher of the service.
51    fn state_watcher(&self) -> StateWatcher;
52}
53
54/// Trait used by `ServiceRunner` to encapsulate the business logic tasks for a service.
55#[async_trait::async_trait]
56pub trait RunnableService: Send {
57    /// The name of the runnable service, used for namespacing error messages.
58    const NAME: &'static str;
59
60    /// Service specific shared data. This is used when you have data that needs to be shared by
61    /// one or more tasks. It is the implementors responsibility to ensure cloning this
62    /// type is shallow and doesn't provide a full duplication of data that is meant
63    /// to be shared between asynchronous processes.
64    type SharedData: Clone + Send + Sync;
65
66    /// The initialized runnable task type.
67    type Task: RunnableTask;
68
69    /// Optional parameters used to when initializing into task.
70    type TaskParams: Send;
71
72    /// A cloned instance of the shared data
73    fn shared_data(&self) -> Self::SharedData;
74
75    /// Converts the service into a runnable task before the main run loop.
76    ///
77    /// The `state` is a `State` watcher of the service. Some tasks may handle state changes
78    /// on their own.
79    async fn into_task(
80        self,
81        state_watcher: &StateWatcher,
82        params: Self::TaskParams,
83    ) -> anyhow::Result<Self::Task>;
84}
85
86/// The result of a single iteration of the service task
87#[derive(Debug)]
88#[must_use]
89pub enum TaskNextAction {
90    /// Request the task to be run again
91    Continue,
92    /// Request the task to be abandoned
93    Stop,
94    /// Request the task to be run again, but report an error
95    ErrorContinue(anyhow::Error),
96}
97
98impl TaskNextAction {
99    /// Creates a `TaskRunResult` from a `Result` where `Ok` means `Continue` and any error is reported
100    pub fn always_continue<T, E: Into<anyhow::Error>>(
101        res: Result<T, E>,
102    ) -> TaskNextAction {
103        match res {
104            Ok(_) => TaskNextAction::Continue,
105            Err(e) => TaskNextAction::ErrorContinue(e.into()),
106        }
107    }
108}
109
110impl From<Result<bool, anyhow::Error>> for TaskNextAction {
111    fn from(result: Result<bool, anyhow::Error>) -> Self {
112        match result {
113            Ok(should_continue) => {
114                if should_continue {
115                    TaskNextAction::Continue
116                } else {
117                    TaskNextAction::Stop
118                }
119            }
120            Err(e) => TaskNextAction::ErrorContinue(e),
121        }
122    }
123}
124
125/// A replacement for the `?` operator for tasks. It will return a `TaskNextAction::ErrorContinue` if the
126/// expression returns an error.
127#[macro_export]
128macro_rules! try_or_continue {
129    ($expr:expr, $custom:expr) => {{
130        match $expr {
131            Ok(val) => val,
132            Err(err) => {
133                $custom(&err);
134                return TaskNextAction::ErrorContinue(err.into());
135            }
136        }
137    }};
138    ($expr:expr) => {{
139        match $expr {
140            Ok(val) => val,
141            Err(err) => return TaskNextAction::ErrorContinue(err.into()),
142        }
143    }};
144}
145
146/// A replacement for the `?` operator for tasks. It will return a `TaskNextAction::Stop` if the
147/// expression returns an error.
148#[macro_export]
149macro_rules! try_or_stop {
150    ($expr:expr, $custom:expr) => {{
151        match $expr {
152            Ok(val) => val,
153            Err(err) => {
154                $custom(&err);
155                return TaskNextAction::Stop;
156            }
157        }
158    }};
159    ($expr:expr) => {{
160        match $expr {
161            Ok(val) => val,
162            Err(err) => return TaskNextAction::Stop,
163        }
164    }};
165}
166
167/// The trait is implemented by the service task and contains a single iteration of the infinity
168/// loop.
169pub trait RunnableTask: Send {
170    /// This function should contain the main business logic of the service task. It will run until
171    /// the service either returns false, panics or a stop signal is received.
172    /// If the service returns an error, it will be logged and execution will resume.
173    /// This is intended to be called only by the `ServiceRunner`.
174    ///
175    /// The `ServiceRunner` continue to call the `run` method in the loop while the state is
176    /// `State::Started`. So first, the `run` method should return a value, and after, the service
177    /// will stop. If the service should react to the state change earlier, it should handle it in
178    /// the `run` loop on its own. See [`StateWatcher::while_started`].
179    fn run(
180        &mut self,
181        watcher: &mut StateWatcher,
182    ) -> impl core::future::Future<Output = TaskNextAction> + Send;
183
184    /// Gracefully shutdowns the task after the end of the execution cycle.
185    fn shutdown(self) -> impl core::future::Future<Output = anyhow::Result<()>> + Send;
186}
187
188/// The service runner manages the lifecycle, execution and error handling of a `RunnableService`.
189/// It can be cloned and passed between threads.
190#[derive(Debug)]
191pub struct ServiceRunner<S>
192where
193    S: RunnableService + 'static,
194{
195    /// The shared state of the service
196    pub shared: S::SharedData,
197    state: Shared<watch::Sender<State>>,
198}
199
200impl<S> Drop for ServiceRunner<S>
201where
202    S: RunnableService + 'static,
203{
204    fn drop(&mut self) {
205        self.stop();
206    }
207}
208
209impl<S> ServiceRunner<S>
210where
211    S: RunnableService + 'static,
212    S::TaskParams: Default,
213{
214    /// Initializes a new `ServiceRunner` containing a `RunnableService`
215    pub fn new(service: S) -> Self {
216        Self::new_with_params(service, S::TaskParams::default())
217    }
218}
219
220impl<S> ServiceRunner<S>
221where
222    S: RunnableService + 'static,
223{
224    /// Initializes a new `ServiceRunner` containing a `RunnableService` with parameters for underlying `Task`
225    pub fn new_with_params(service: S, params: S::TaskParams) -> Self {
226        let shared = service.shared_data();
227        let metric = FuturesMetrics::obtain_futures_metrics(S::NAME);
228        let state = initialize_loop(service, params, metric);
229        Self { shared, state }
230    }
231
232    async fn _await_start_or_stop(
233        &self,
234        mut start: StateWatcher,
235    ) -> anyhow::Result<State> {
236        loop {
237            let state = start.borrow().clone();
238            if !state.starting() {
239                return Ok(state);
240            }
241            start.changed().await?;
242        }
243    }
244
245    async fn _await_stop(&self, mut stop: StateWatcher) -> anyhow::Result<State> {
246        loop {
247            let state = stop.borrow().clone();
248            if state.stopped() {
249                return Ok(state);
250            }
251            stop.changed().await?;
252        }
253    }
254}
255
256#[async_trait::async_trait]
257impl<S> Service for ServiceRunner<S>
258where
259    S: RunnableService + 'static,
260{
261    fn start(&self) -> anyhow::Result<()> {
262        let started = self.state.send_if_modified(|state| {
263            if state.not_started() {
264                *state = State::Starting;
265                true
266            } else {
267                false
268            }
269        });
270
271        if started {
272            Ok(())
273        } else {
274            Err(anyhow!(
275                "The service `{}` already has been started.",
276                S::NAME
277            ))
278        }
279    }
280
281    async fn start_and_await(&self) -> anyhow::Result<State> {
282        let start = self.state.subscribe().into();
283        self.start()?;
284        self._await_start_or_stop(start).await
285    }
286
287    async fn await_start_or_stop(&self) -> anyhow::Result<State> {
288        let start = self.state.subscribe().into();
289        self._await_start_or_stop(start).await
290    }
291
292    fn stop(&self) -> bool {
293        self.state.send_if_modified(|state| {
294            if state.not_started() || state.starting() || state.started() {
295                *state = State::Stopping;
296                true
297            } else {
298                false
299            }
300        })
301    }
302
303    async fn stop_and_await(&self) -> anyhow::Result<State> {
304        let stop = self.state.subscribe().into();
305        self.stop();
306        self._await_stop(stop).await
307    }
308
309    async fn await_stop(&self) -> anyhow::Result<State> {
310        let stop = self.state.subscribe().into();
311        self._await_stop(stop).await
312    }
313
314    fn state(&self) -> State {
315        self.state.borrow().clone()
316    }
317
318    fn state_watcher(&self) -> StateWatcher {
319        self.state.subscribe().into()
320    }
321}
322
323#[tracing::instrument(skip_all, fields(service = S::NAME))]
324/// Initialize the background loop as a spawned task.
325fn initialize_loop<S>(
326    service: S,
327    params: S::TaskParams,
328    metric: FuturesMetrics,
329) -> Shared<watch::Sender<State>>
330where
331    S: RunnableService + 'static,
332{
333    let (sender, _) = watch::channel(State::NotStarted);
334    let state = Shared::new(sender);
335    let stop_sender = state.clone();
336    // Spawned as a task to check if the service is already running and to capture any panics.
337    tokio::task::spawn(
338        async move {
339            tracing::debug!("running");
340            let run = std::panic::AssertUnwindSafe(run(
341                service,
342                stop_sender.clone(),
343                params,
344                metric,
345            ));
346            tracing::debug!("awaiting run");
347            let result = run.catch_unwind().await;
348
349            let stopped_state = if let Err(e) = result {
350                let panic_information = panic_to_string(e);
351                State::StoppedWithError(panic_information)
352            } else {
353                State::Stopped
354            };
355
356            tracing::debug!("shutting down {:?}", stopped_state);
357
358            let _ = stop_sender.send_if_modified(|state| {
359                if !state.stopped() {
360                    *state = stopped_state.clone();
361                    tracing::debug!("Wasn't stopped, so sent stop.");
362                    true
363                } else {
364                    tracing::debug!("Was already stopped.");
365                    false
366                }
367            });
368
369            tracing::info!("The service {} is shut down", S::NAME);
370
371            if let State::StoppedWithError(err) = stopped_state {
372                std::panic::resume_unwind(Box::new(err));
373            }
374        }
375        .in_current_span(),
376    );
377    state
378}
379
380/// Runs the main loop.
381async fn run<S>(
382    service: S,
383    sender: Shared<watch::Sender<State>>,
384    params: S::TaskParams,
385    metric: FuturesMetrics,
386) where
387    S: RunnableService + 'static,
388{
389    let mut state: StateWatcher = sender.subscribe().into();
390    if state.borrow_and_update().not_started() {
391        // We can panic here, because it is inside of the task.
392        state.changed().await.expect("The service is destroyed");
393    }
394
395    // If the state after update is not `Starting` then return to stop the service.
396    if !state.borrow().starting() {
397        return;
398    }
399
400    // We can panic here, because it is inside of the task.
401    tracing::info!("Starting {} service", S::NAME);
402    let mut task = service
403        .into_task(&state, params)
404        .await
405        .unwrap_or_else(|_| panic!("The initialization of {} failed", S::NAME));
406
407    sender.send_if_modified(|s| {
408        if s.starting() {
409            *s = State::Started;
410            true
411        } else {
412            false
413        }
414    });
415
416    let got_panic = run_task(&mut task, state, &metric).await;
417
418    let got_panic = shutdown_task(S::NAME, task, got_panic).await;
419
420    if let Some(panic) = got_panic {
421        std::panic::resume_unwind(panic)
422    }
423}
424
425async fn run_task<S: RunnableTask>(
426    task: &mut S,
427    mut state: StateWatcher,
428    metric: &FuturesMetrics,
429) -> Option<Box<dyn Any + Send>> {
430    let mut got_panic = None;
431
432    while state.borrow_and_update().started() {
433        let tracked_task = FutureTracker::new(task.run(&mut state));
434        let task = std::panic::AssertUnwindSafe(tracked_task);
435        let panic_result = task.catch_unwind().await;
436
437        if let Err(panic) = panic_result {
438            tracing::debug!("got a panic");
439            got_panic = Some(panic);
440            break;
441        }
442
443        let tracked_result = panic_result.expect("Checked the panic above");
444        let result = tracked_result.extract(metric);
445
446        match result {
447            TaskNextAction::Continue => {
448                tracing::debug!("run loop");
449            }
450            TaskNextAction::Stop => {
451                tracing::debug!("stopping");
452                break;
453            }
454            TaskNextAction::ErrorContinue(e) => {
455                let e: &dyn std::error::Error = &*e;
456                tracing::error!(e);
457            }
458        }
459    }
460    got_panic
461}
462
463async fn shutdown_task<S>(
464    name: &str,
465    task: S,
466    mut got_panic: Option<Box<dyn Any + Send>>,
467) -> Option<Box<dyn Any + Send>>
468where
469    S: RunnableTask,
470{
471    tracing::info!("Shutting down {} service", name);
472    let shutdown = std::panic::AssertUnwindSafe(task.shutdown());
473    match shutdown.catch_unwind().await {
474        Ok(Ok(_)) => {}
475        Ok(Err(e)) => {
476            tracing::error!("Got an error during shutdown of the task: {e}");
477        }
478        Err(e) => {
479            if got_panic.is_some() {
480                let panic_information = panic_to_string(e);
481                tracing::error!(
482                    "Go a panic during execution and shutdown of the task. \
483                    The error during shutdown: {panic_information}"
484                );
485            } else {
486                got_panic = Some(e);
487            }
488        }
489    }
490    got_panic
491}
492
493fn panic_to_string(e: Box<dyn core::any::Any + Send>) -> String {
494    match e.downcast::<String>() {
495        Ok(v) => *v,
496        Err(e) => match e.downcast::<&str>() {
497            Ok(v) => v.to_string(),
498            _ => "Unknown Source of Error".to_owned(),
499        },
500    }
501}
502
503#[cfg(test)]
504mod tests {
505    use super::*;
506
507    mockall::mock! {
508        Service {}
509
510        #[async_trait::async_trait]
511        impl RunnableService for Service {
512            const NAME: &'static str = "MockService";
513
514            type SharedData = EmptyShared;
515            type Task = MockTask;
516            type TaskParams = ();
517
518            fn shared_data(&self) -> EmptyShared;
519
520            async fn into_task(self, state: &StateWatcher, params: <MockService as RunnableService>::TaskParams) -> anyhow::Result<MockTask>;
521        }
522    }
523
524    mockall::mock! {
525        Task {}
526
527        impl RunnableTask for Task {
528            fn run(
529                &mut self,
530                state: &mut StateWatcher
531            ) -> impl core::future::Future<Output = TaskNextAction> + Send;
532
533            async fn shutdown(self) -> anyhow::Result<()>;
534        }
535    }
536
537    impl MockService {
538        fn new_empty() -> Self {
539            let mut mock = MockService::default();
540            mock.expect_shared_data().returning(|| EmptyShared);
541            mock.expect_into_task().returning(|_, _| {
542                let mut mock = MockTask::default();
543                mock.expect_run().returning(|watcher| {
544                    let mut watcher = watcher.clone();
545                    Box::pin(async move {
546                        watcher.while_started().await.unwrap();
547                        TaskNextAction::Stop
548                    })
549                });
550                mock.expect_shutdown().times(1).returning(|| Ok(()));
551                Ok(mock)
552            });
553            mock
554        }
555    }
556
557    #[tokio::test]
558    async fn start_and_await_stop_and_await_works() {
559        let service = ServiceRunner::new(MockService::new_empty());
560        let state = service.start_and_await().await.unwrap();
561        assert!(state.started());
562        let state = service.stop_and_await().await.unwrap();
563        assert!(matches!(state, State::Stopped));
564    }
565
566    #[tokio::test]
567    async fn double_start_fails() {
568        let service = ServiceRunner::new(MockService::new_empty());
569        assert!(service.start().is_ok());
570        assert!(service.start().is_err());
571    }
572
573    #[tokio::test]
574    async fn double_start_and_await_fails() {
575        let service = ServiceRunner::new(MockService::new_empty());
576        assert!(service.start_and_await().await.is_ok());
577        assert!(service.start_and_await().await.is_err());
578    }
579
580    #[tokio::test]
581    async fn stop_without_start() {
582        let service = ServiceRunner::new(MockService::new_empty());
583        service.stop_and_await().await.unwrap();
584        assert!(matches!(service.state(), State::Stopped));
585    }
586
587    #[tokio::test]
588    async fn panic_during_run() {
589        let mut mock = MockService::default();
590        mock.expect_shared_data().returning(|| EmptyShared);
591        mock.expect_into_task().returning(|_, _| {
592            let mut mock = MockTask::default();
593            mock.expect_run().returning(|_| panic!("Should fail"));
594            mock.expect_shutdown().times(1).returning(|| Ok(()));
595            Ok(mock)
596        });
597        let service = ServiceRunner::new(mock);
598        let state = service.start_and_await().await.unwrap();
599        assert!(matches!(state, State::StoppedWithError(s) if s.contains("Should fail")));
600
601        let state = service.await_stop().await.unwrap();
602        assert!(matches!(state, State::StoppedWithError(s) if s.contains("Should fail")));
603    }
604
605    #[tokio::test]
606    async fn panic_during_shutdown() {
607        let mut mock = MockService::default();
608        mock.expect_shared_data().returning(|| EmptyShared);
609        mock.expect_into_task().returning(|_, _| {
610            let mut mock = MockTask::default();
611            mock.expect_run()
612                .returning(|_| Box::pin(async move { TaskNextAction::Stop }));
613            mock.expect_shutdown()
614                .times(1)
615                .returning(|| panic!("Shutdown should fail"));
616            Ok(mock)
617        });
618        let service = ServiceRunner::new(mock);
619        let state = service.start_and_await().await.unwrap();
620        assert!(
621            matches!(state, State::StoppedWithError(s) if s.contains("Shutdown should fail"))
622        );
623
624        let state = service.await_stop().await.unwrap();
625        assert!(
626            matches!(state, State::StoppedWithError(s) if s.contains("Shutdown should fail"))
627        );
628    }
629
630    #[tokio::test]
631    async fn double_await_stop_works() {
632        let service = ServiceRunner::new(MockService::new_empty());
633        service.start().unwrap();
634        service.stop();
635
636        let state = service.await_stop().await.unwrap();
637        assert!(matches!(state, State::Stopped));
638        let state = service.await_stop().await.unwrap();
639        assert!(matches!(state, State::Stopped));
640    }
641
642    #[tokio::test]
643    async fn double_stop_and_await_works() {
644        let service = ServiceRunner::new(MockService::new_empty());
645        service.start().unwrap();
646
647        let state = service.stop_and_await().await.unwrap();
648        assert!(matches!(state, State::Stopped));
649        let state = service.stop_and_await().await.unwrap();
650        assert!(matches!(state, State::Stopped));
651    }
652
653    #[tokio::test]
654    async fn stop_unused_service() {
655        let mut receiver;
656        {
657            let service = ServiceRunner::new(MockService::new_empty());
658            service.start().unwrap();
659            receiver = service.state.subscribe();
660        }
661
662        receiver.changed().await.unwrap();
663        assert!(matches!(receiver.borrow().clone(), State::Stopping));
664        receiver.changed().await.unwrap();
665        assert!(matches!(receiver.borrow().clone(), State::Stopped));
666    }
667}