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
use std::{collections::HashMap, fmt::Debug, time::Duration};

use chrono::Utc;
use futures::Future;
use tokio::time::{interval, interval_at, Instant};
use tower::{Layer, Service, ServiceExt};

use crate::{
    builder::{WorkerBuilder, WorkerFactory},
    error::{JobError, WorkerError},
    job::{Job, JobStream},
    request::{JobRequest, JobState},
    response::JobResult,
    storage::{JobStreamResult, Storage},
    worker::{Context, Handler, Message, Worker},
};

#[cfg(feature = "broker")]
use crate::worker::{
    broker::Broker,
    prelude::{WorkerEvent, WorkerMessage},
};

use super::{
    streams::{HeartbeatStream, KeepAliveStream},
    StorageWorkerPulse,
};

/// Controls how [StorageWorker] interacts with the [Storage]
#[derive(Debug)]
pub struct StorageWorkerConfig {
    keep_alive: Duration,
    fetch_interval: Duration,
    heartbeats: HashMap<StorageWorkerPulse, Duration>,
}

impl Default for StorageWorkerConfig {
    fn default() -> Self {
        let mut heartbeats = HashMap::new();
        heartbeats.insert(
            StorageWorkerPulse::RenqueueOrpharned { count: 10 },
            Duration::from_secs(60),
        );
        heartbeats.insert(
            StorageWorkerPulse::EnqueueScheduled { count: 10 },
            Duration::from_secs(60),
        );

        StorageWorkerConfig {
            keep_alive: Duration::from_secs(30),
            fetch_interval: Duration::from_millis(50),
            heartbeats,
        }
    }
}

/// A queue represents a consumer of a [Storage].
///
/// A [Service] must be provided to be called when a new job is detected.
#[derive(Debug)]
pub struct StorageWorker<T, S: Storage<Output = T>, H> {
    storage: S,
    handler: H,
    config: StorageWorkerConfig,
    id: uuid::Uuid,
}

impl<T, S, H> StorageWorker<T, S, H>
where
    T: 'static + Job,
    S: 'static + Storage<Output = T> + Unpin,
{
    /// Create a new Worker instance
    pub fn new(storage: S, handler: H) -> Self {
        let id = uuid::Uuid::new_v4();
        StorageWorker {
            storage,
            handler,
            config: Default::default(),
            id,
        }
    }
    /// Set a [WorkerConfig] for [Worker]
    pub fn config(self, config: StorageWorkerConfig) -> StorageWorker<T, S, H> {
        StorageWorker {
            storage: self.storage,
            handler: self.handler,
            config,
            id: self.id,
        }
    }
}

#[async_trait::async_trait]
impl<T, S, H, F> Worker for StorageWorker<T, S, H>
where
    S: JobStream<Job = T> + Storage + Unpin + Storage<Output = T> + Send + 'static + Sync,
    T: Job + Send + 'static,
    H: Service<JobRequest<T>, Response = JobResult, Error = JobError, Future = F>
        + Unpin
        + Send
        + 'static,
    F: Future<Output = Result<JobResult, JobError>> + Send,
{
    type Job = T;

    type Service = H;

    type Future = F;

    async fn on_start(&mut self, ctx: &mut Context<Self>) {
        // To change this just modify the controller then restart.
        ctx.notify_with(KeepAliveStream::new(interval(self.config.keep_alive)));
        // Sets up reactivate orphaned jobs
        // Setup scheduling for non_sql storages eg Redis
        for (pulse, duration) in self.config.heartbeats.iter() {
            let start = Instant::now() + Duration::from_millis(17);
            ctx.notify_with(HeartbeatStream::new(
                pulse.clone(),
                interval_at(start, *duration),
            ));
        }
    }

    async fn on_stop(&mut self, _ctx: &mut Context<Self>) {
        tracing::warn!("worker.stopped")
    }

    fn consume(&mut self) -> JobStreamResult<Self::Job> {
        self.storage
            .stream(self.id.to_string(), self.config.fetch_interval)
    }

    fn service(&mut self) -> &mut Self::Service {
        &mut self.handler
    }

    async fn handle_job(&mut self, mut job: JobRequest<Self::Job>) -> Result<JobResult, JobError> {
        let instant = Instant::now();
        let mut storage = self.storage.clone();
        let worker_id = self.id.to_string();
        let handle = self.service().ready().await?;
        let job_id = job.id();
        job.set_status(JobState::Running);
        job.set_lock_at(Some(Utc::now()));
        job.record_attempt();
        job.set_lock_by(Some(worker_id.clone()));
        if let Err(e) = storage.update_by_id(job_id.clone(), &job).await {
            #[cfg(feature = "broker")]
            Broker::global()
                .issue_send(WorkerMessage::new(
                    worker_id.clone(),
                    WorkerEvent::Error(format!("{e}")),
                ))
                .await;
            T::on_worker_error(job.inner(), &job, &WorkerError::Storage(e));
        };
        T::on_service_ready(job.inner(), &job, instant.elapsed());
        let res = handle.call(job).await;

        if let Ok(Some(mut job)) = storage.fetch_by_id(job_id.clone()).await {
            job.set_done_at(Some(Utc::now()));
            let finalize = match res {
                Ok(ref r) => match r {
                    JobResult::Success => {
                        job.set_status(JobState::Done);
                        storage.ack(worker_id.clone(), job_id.clone()).await
                    }
                    JobResult::Retry => {
                        job.set_status(JobState::Retry);
                        storage.retry(worker_id.clone(), job_id.clone()).await
                    }
                    JobResult::Kill => {
                        job.set_status(JobState::Killed);
                        storage.kill(worker_id.clone(), job_id.clone()).await
                    }

                    JobResult::Reschedule(wait) => {
                        job.set_status(JobState::Retry);
                        storage.reschedule(&job, *wait).await
                    }
                },
                Err(ref e) => {
                    job.set_status(JobState::Failed);
                    job.set_last_error(format!("{e}"));

                    #[cfg(feature = "broker")]
                    Broker::global()
                        .issue_send(WorkerMessage::new(
                            worker_id.clone(),
                            WorkerEvent::Error(format!("{e}")),
                        ))
                        .await;
                    // let base: i32 = 2; // an explicit type is required
                    // let millis = base.pow(job.attempts());
                    storage.reschedule(&job, Duration::from_millis(10000)).await
                }
            };
            if let Err(e) = finalize {
                #[cfg(feature = "broker")]
                Broker::global()
                    .issue_send(WorkerMessage::new(
                        worker_id.clone(),
                        WorkerEvent::Error(format!("{e}")),
                    ))
                    .await;
                T::on_worker_error(job.inner(), &job, &WorkerError::Storage(e));
            }
            if let Err(e) = storage.update_by_id(job_id.clone(), &job).await {
                #[cfg(feature = "broker")]
                Broker::global()
                    .issue_send(WorkerMessage::new(
                        worker_id.clone(),
                        WorkerEvent::Error(format!("{e}")),
                    ))
                    .await;
                T::on_worker_error(job.inner(), &job, &WorkerError::Storage(e));
            };
        }

        res
    }
}

impl Message for StorageWorkerPulse {
    type Result = ();
}

#[async_trait::async_trait]
impl<T: 'static, S: 'static, H: 'static, F> Handler<StorageWorkerPulse> for StorageWorker<T, S, H>
where
    S: Storage<Output = T> + Unpin + Send + Sync,
    T: Job + Send,
    H: Service<JobRequest<T>, Response = JobResult, Error = JobError, Future = F>
        + Unpin
        + Send
        + 'static,
    F: Future<Output = Result<JobResult, JobError>> + Send,
{
    type Result = ();
    async fn handle(&mut self, beat: StorageWorkerPulse) -> Self::Result {
        let queue = &mut self.storage;
        let _heartbeat = queue.heartbeat(beat).await;
    }
}

pub(crate) struct KeepAlive;

impl Message for KeepAlive {
    type Result = ();
}

#[async_trait::async_trait]
impl<T: 'static, S: 'static, H: 'static, F> Handler<KeepAlive> for StorageWorker<T, S, H>
where
    S: Storage<Output = T> + Unpin + Send + Sync,
    T: Job + Send,
    H: Service<JobRequest<T>, Response = JobResult, Error = JobError, Future = F>
        + Unpin
        + Send
        + 'static,
    F: Future<Output = Result<JobResult, JobError>> + Send,
{
    type Result = ();
    async fn handle(&mut self, _keep_alive: KeepAlive) -> Self::Result {
        let queue = &mut self.storage;
        let id = self.id.to_string();
        let _beat = queue.keep_alive::<H>(id).await;
    }
}

impl<T, S, M, Ser, Fut> WorkerFactory<Ser> for WorkerBuilder<T, S, M>
where
    S: Storage<Output = T> + Unpin + Send + 'static + Sync,
    T: Job + Send + 'static,
    M: Layer<Ser>,
    <M as Layer<Ser>>::Service: Service<JobRequest<T>, Response = JobResult, Error = JobError, Future = Fut>
        + Unpin
        + Send
        + 'static,
    Fut: Future<Output = Result<JobResult, JobError>> + Send,
{
    type Worker = StorageWorker<T, S, <M as Layer<Ser>>::Service>;

    fn build(self, service: Ser) -> Self::Worker {
        StorageWorker::new(self.source, self.layer.layer(service))
    }
}