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
use std::any::Any;
use std::borrow::Cow;
use std::fmt::Display;
use std::panic::UnwindSafe;
use std::sync::Arc;

use backoff_strategy::constant_time::ConstantTimeBackoff;
use backoff_strategy::{BackoffStrategy, DefaultStrategyFactory, StrategyFactory};
use futures::{Future, FutureExt};
use tokio::task::JoinHandle;

pub mod backoff_strategy;

pub struct TaskRunner<T, SF = DefaultStrategyFactory<ConstantTimeBackoff>> {
    app: Arc<T>,
    backoff_strategy: SF,
}

impl<T> TaskRunner<T> {
    pub fn new(app: Arc<T>) -> Self {
        Self {
            app,
            backoff_strategy: DefaultStrategyFactory::new(),
        }
    }
}

impl<T, SF> TaskRunner<T, SF>
where
    T: Send + Sync + 'static,
    SF: StrategyFactory,
{
    pub fn with_default_strategy<NS>(self) -> TaskRunner<T, DefaultStrategyFactory<NS>>
    where
        NS: StrategyFactory,
    {
        TaskRunner {
            app: self.app,
            backoff_strategy: DefaultStrategyFactory::new(),
        }
    }

    pub fn with_strategy<NSF>(self, backoff_strategy: NSF) -> TaskRunner<T, NSF> {
        TaskRunner {
            app: self.app,
            backoff_strategy,
        }
    }
}

impl<T, SF> TaskRunner<T, SF>
where
    T: Send + Sync + 'static,
    SF: StrategyFactory,
{
    /// Spawns a task that will run until it returns Ok(()) or panics.
    /// If the task returns an error, it will be logged and the task will be retried with a backoff.
    ///
    /// If the task panics, the panic output will be returned as an error.
    pub fn spawn_task<S, C, F, E>(
        &self,
        label: S,
        task: C,
    ) -> JoinHandle<Result<(), Box<dyn Any + Send>>>
    where
        S: ToString,
        C: Fn(Arc<T>) -> F + Send + Sync + 'static,
        F: Future<Output = Result<(), E>> + Send + 'static + UnwindSafe,
        E: Display + Send + Sync,
    {
        let app = self.app.clone();
        let label = label.to_string();

        let mut backoff_strategy = self.backoff_strategy.create_strategy();

        tokio::spawn(async move {
            loop {
                tracing::info!(task_label = label, "Running task");

                let result = task(app.clone()).catch_unwind().await;

                match result {
                    Ok(Ok(())) => {
                        tracing::info!(task_label = label, "Task finished");
                        break;
                    }
                    Ok(Err(err)) => {
                        tracing::error!(task_label = label, error = %err, "Task failed");
                        backoff_strategy.add_failure();
                        tokio::time::sleep(backoff_strategy.next_backoff()).await;
                    }
                    Err(err) => {
                        let reason = panic_helper(&err);
                        tracing::error!(task_label = label, error = %reason, "Task panicked");
                        return Err(err);
                    }
                }
            }

            Ok(())
        })
    }
}

pub fn panic_helper(err: &Box<dyn Any + Send>) -> Cow<'_, str> {
    if let Some(err) = err.downcast_ref::<&str>() {
        Cow::Borrowed(*err)
    } else if let Some(err) = err.downcast_ref::<String>() {
        Cow::Owned(err.clone())
    } else {
        Cow::Borrowed("unknown panic reason")
    }
}