roadster 0.8.1

A "Batteries Included" web framework for rust designed to get you moving fast.
Documentation
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use crate::app::context::AppContext;
use crate::util::types;
#[cfg(feature = "worker-pg")]
pub use crate::worker::backend::pg::enqueue::PgEnqueuer;
#[cfg(feature = "worker-pg")]
pub use crate::worker::backend::pg::processor::PgProcessor;
#[cfg(feature = "worker-sidekiq")]
pub use crate::worker::backend::sidekiq::enqueue::SidekiqEnqueuer;
#[cfg(feature = "worker-sidekiq")]
pub use crate::worker::backend::sidekiq::processor::SidekiqProcessor;
use crate::worker::config::{EnqueueConfig, WorkerConfig};
use crate::worker::enqueue::Enqueuer;
#[cfg(any(feature = "worker-sidekiq", feature = "worker-pg"))]
use crate::worker::job::JobMetadata;
use async_trait::async_trait;
use axum_core::extract::FromRef;
use cron::Schedule;
use serde::{Deserialize, Serialize};
use std::borrow::Borrow;
use std::time::Duration;
use tracing::instrument;

pub mod backend;
pub mod config;
pub mod enqueue;
pub(crate) mod job;

#[async_trait]
pub trait Worker<S, Args>: Send + Sync
where
    S: Clone + Send + Sync + 'static,
    AppContext: FromRef<S>,
    Args: Send + Sync + Serialize + for<'de> Deserialize<'de>,
{
    type Error: std::error::Error + Send + Sync;
    type Enqueuer: Enqueuer + Send + Sync;

    /// The name of the worker. This will be encoded in the job data when it's enqueued the backing
    /// database (Redis/Postgres), and used to identify which type should handle a job when it's
    /// fetched from the queue. Therefore, it should be unique across the app, and care should be
    /// taken when refactoring.
    ///
    /// By default, [`Self::name`] returns the name of the type that implements the [`Worker`]
    /// trait. See [`types::simple_type_name`].
    ///
    /// This is not included in the [`EnqueueConfig`] because [`EnqueueConfig`] is included in
    /// the [`crate::config::AppConfig`] to allow defining defaults for the config values, but
    /// the name needs to be specified separately for each [`Worker`].
    fn name() -> String
    where
        Self: Sized,
    {
        types::simple_type_name::<Self>()
    }

    /// Get worker-specific configuration options to use when enqueuing a job. Any value not
    /// provided in the returned [`WorkerConfig`] will fall back to the value from the
    /// [`crate::config::AppConfig`].
    ///
    /// The [`Worker::enqueue_config`] method will be called when enqueuing a job for the worker.
    fn enqueue_config(_state: &S) -> EnqueueConfig {
        EnqueueConfig::default()
    }

    /// Get worker-specific configuration options to use when handling a job. Any value not provided
    /// in the returned [`WorkerConfig`] will fall back to the value from the
    /// [`crate::config::AppConfig`].
    ///
    /// The [`Worker::worker_config`] method will be called once for each worker when it is
    /// registered, and the config will be stored to be used when the worker handles a job.
    fn worker_config(&self, _state: &S) -> WorkerConfig {
        WorkerConfig::default()
    }

    #[instrument(skip_all)]
    async fn enqueue<ArgsRef>(
        state: &S,
        args: ArgsRef,
    ) -> Result<(), <Self::Enqueuer as Enqueuer>::Error>
    where
        Self: 'static + Sized,
        ArgsRef: Send + Sync + Borrow<Args> + Serialize,
    {
        Self::Enqueuer::enqueue::<Self, _, _, _, Self::Error>(state, args).await?;
        Ok(())
    }

    #[instrument(skip_all)]
    async fn enqueue_delayed<ArgsRef>(
        state: &S,
        args: ArgsRef,
        delay: Duration,
    ) -> Result<(), <Self::Enqueuer as Enqueuer>::Error>
    where
        Self: 'static + Sized,
        ArgsRef: Send + Sync + Borrow<Args> + Serialize,
    {
        Self::Enqueuer::enqueue_delayed::<Self, _, _, _, Self::Error>(state, args, delay).await?;
        Ok(())
    }

    #[instrument(skip_all)]
    async fn enqueue_batch<ArgsRef>(
        state: &S,
        args: &[ArgsRef],
    ) -> Result<(), <Self::Enqueuer as Enqueuer>::Error>
    where
        Self: 'static + Sized,
        ArgsRef: Send + Sync + Borrow<Args> + Serialize,
    {
        Self::Enqueuer::enqueue_batch::<Self, _, _, _, Self::Error>(state, args).await?;
        Ok(())
    }

    #[instrument(skip_all)]
    async fn enqueue_batch_delayed<ArgsRef>(
        state: &S,
        args: &[ArgsRef],
        delay: Duration,
    ) -> Result<(), <Self::Enqueuer as Enqueuer>::Error>
    where
        Self: 'static + Sized,
        ArgsRef: Send + Sync + Borrow<Args> + Serialize,
    {
        Self::Enqueuer::enqueue_batch_delayed::<Self, _, _, _, Self::Error>(state, args, delay)
            .await?;
        Ok(())
    }

    async fn handle(&self, state: &S, args: Args) -> Result<(), Self::Error>;

    /// This is a "private" API that's only intended for usage in Roadster's internal benchmarking suite.
    /// This method does not follow any semver guarantees.
    #[cfg(feature = "bench")]
    #[doc(hidden)]
    async fn on_complete(&self) {}
}

#[cfg(any(feature = "worker-pg", feature = "worker-sidekiq"))]
type WorkerFn<S> = Box<
    dyn Send
        + Sync
        + for<'a> Fn(
            &'a S,
            serde_json::Value,
        ) -> std::pin::Pin<
            Box<dyn 'a + Send + Future<Output = crate::error::RoadsterResult<()>>>,
        >,
>;

#[cfg(all(
    feature = "bench",
    any(feature = "worker-pg", feature = "worker-sidekiq")
))]
type OnCompleteFn =
    Box<dyn Send + Sync + Fn() -> std::pin::Pin<Box<dyn Send + Future<Output = ()>>>>;

#[derive(Clone)]
#[cfg(any(feature = "worker-pg", feature = "worker-sidekiq"))]
pub(crate) struct WorkerWrapper<S>
where
    S: Clone + Send + Sync + 'static,
    AppContext: FromRef<S>,
{
    inner: std::sync::Arc<WorkerWrapperInner<S>>,
}

#[cfg(any(feature = "worker-pg", feature = "worker-sidekiq"))]
pub(crate) struct WorkerWrapperInner<S>
where
    S: Clone + Send + Sync + 'static,
    AppContext: FromRef<S>,
{
    name: String,
    type_id: std::any::TypeId,
    #[allow(dead_code)]
    enqueue_config: EnqueueConfig,
    worker_config: WorkerConfig,
    worker_fn: WorkerFn<S>,
    #[cfg(feature = "bench")]
    #[allow(dead_code)]
    on_complete_fn: OnCompleteFn,
}

#[cfg(any(feature = "worker-pg", feature = "worker-sidekiq"))]
impl<S> WorkerWrapper<S>
where
    S: Clone + Send + Sync + 'static,
    AppContext: FromRef<S>,
{
    fn new<W, Args, E>(
        state: &S,
        worker: W,
        enqueue_config: EnqueueConfig,
    ) -> crate::error::RoadsterResult<Self>
    where
        W: 'static + Worker<S, Args, Error = E>,
        Args: Send + Sync + Serialize + for<'de> Deserialize<'de>,
        E: 'static + std::error::Error + Send + Sync,
    {
        use std::any::Any;

        let type_id = worker.type_id();
        let worker = std::sync::Arc::new(worker);

        #[cfg(feature = "bench")]
        let worker2 = worker.clone();

        Ok(Self {
            inner: std::sync::Arc::new(WorkerWrapperInner {
                name: W::name(),
                type_id,
                enqueue_config,
                worker_config: worker.worker_config(state),
                worker_fn: Box::new(move |state: &S, args: serde_json::Value| {
                    let worker = worker.clone();
                    Box::pin(async move {
                        let args: Args = serde_json::from_value(args)
                            .map_err(crate::error::worker::DequeueError::Serde)?;

                        match worker.clone().handle(state, args).await {
                            Ok(_) => Ok(()),
                            Err(err) => Err(crate::error::Error::from(
                                crate::error::worker::WorkerError::Handle(W::name(), Box::new(err)),
                            )),
                        }
                    })
                }),
                #[cfg(feature = "bench")]
                on_complete_fn: Box::new(move || {
                    let worker = worker2.clone();
                    Box::pin(async move {
                        worker.clone().on_complete().await;
                    })
                }),
            }),
        })
    }

    async fn handle(
        &self,
        state: &S,
        job_metadata: &JobMetadata,
        args: serde_json::Value,
    ) -> crate::error::RoadsterResult<()> {
        use futures::FutureExt;
        use tracing::Instrument;

        let span_name = format!("WORKER {}::handle", self.inner.name);
        let context = AppContext::from_ref(state);
        let queue_name = self.inner.enqueue_config.queue.as_ref().or(context
            .config()
            .service
            .worker
            .enqueue_config
            .queue
            .as_ref());
        let span = tracing::error_span!(
            "WORKER",
            otel.name = span_name,
            otel.kind = "CONSUMER",
            job.id = %job_metadata.id,
            worker.name = self.inner.name,
            worker.queue.name = queue_name
        );

        async {
            let inner =
                std::panic::AssertUnwindSafe((self.inner.worker_fn)(state, args)).catch_unwind();

            let context = AppContext::from_ref(state);
            let timeout = self
                .inner
                .worker_config
                .timeout
                .or(context.config().service.worker.worker_config.timeout)
                .unwrap_or_default();

            let max_duration = if timeout {
                self.inner.worker_config.max_duration.or(context
                    .config()
                    .service
                    .worker
                    .worker_config
                    .max_duration)
            } else {
                None
            };

            let result = if let Some(max_duration) = max_duration {
                tokio::time::timeout(max_duration, inner)
                    .await
                    .map_err(|_| {
                        tracing::error!(
                            worker.name = self.inner.name,
                            worker.max_duration = max_duration.as_secs(),
                            "Worker timed out"
                        );
                        crate::error::worker::WorkerError::Timeout(
                            self.inner.name.clone(),
                            max_duration,
                        )
                    })?
            } else {
                inner.await
            };

            match result {
                Ok(result) => result,
                Err(unwind_error) => {
                    tracing::error!(
                        worker.name = self.inner.name,
                        "Worker panicked while handling a job: {unwind_error:?}"
                    );
                    Err(crate::error::worker::WorkerError::Panic(self.inner.name.clone()).into())
                }
            }
        }
        .instrument(span.or_current())
        .await
    }
}

#[derive(bon::Builder)]
#[non_exhaustive]
pub struct PeriodicArgs<Args>
where
    Args: Send + Sync + Serialize + for<'de> Deserialize<'de>,
{
    pub args: Args,
    pub schedule: Schedule,
}

#[derive(Clone, bon::Builder, Eq, PartialEq)]
#[non_exhaustive]
#[cfg(any(feature = "worker-pg", feature = "worker-sidekiq"))]
pub(crate) struct PeriodicArgsJson {
    pub(crate) args: serde_json::Value,
    pub(crate) worker_name: String,
    pub(crate) schedule: Schedule,
}

#[cfg(any(feature = "worker-pg", feature = "worker-sidekiq"))]
impl std::hash::Hash for PeriodicArgsJson {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        crate::worker::job::periodic_hash(state, &self.worker_name, &self.schedule, &self.args);
    }
}

#[cfg(test)]
pub(crate) mod test {
    use crate::app::context::AppContext;
    use crate::worker::Worker;
    use crate::worker::config::EnqueueConfig;
    use crate::worker::enqueue::test::TestEnqueuer;
    use async_trait::async_trait;

    pub(crate) struct TestWorker;
    #[async_trait]
    impl Worker<AppContext, ()> for TestWorker {
        type Error = crate::error::Error;
        type Enqueuer = TestEnqueuer;

        fn enqueue_config(_state: &AppContext) -> EnqueueConfig {
            EnqueueConfig::builder().queue("default").build()
        }

        async fn handle(&self, _state: &AppContext, _args: ()) -> Result<(), Self::Error> {
            unimplemented!()
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::app::context::AppContext;
    use crate::config::AppConfig;
    use crate::worker::{Enqueuer, Worker};
    use async_trait::async_trait;
    use axum_core::extract::FromRef;
    use insta::assert_debug_snapshot;
    use rstest::{fixture, rstest};
    use serde::{Deserialize, Serialize};
    use std::borrow::Borrow;
    use std::time::Duration;

    struct FooBackend;

    #[async_trait]
    impl Enqueuer for FooBackend {
        type Error = crate::error::Error;

        async fn enqueue<W, S, Args, ArgsRef, E>(
            _state: &S,
            _args: ArgsRef,
        ) -> Result<(), Self::Error>
        where
            W: 'static + Worker<S, Args, Error = E>,
            S: Clone + Send + Sync + 'static,
            AppContext: FromRef<S>,
            Args: Send + Sync + Serialize + for<'de> Deserialize<'de>,
            ArgsRef: Send + Sync + Borrow<Args> + Serialize,
        {
            unimplemented!()
        }

        async fn enqueue_delayed<W, S, Args, ArgsRef, E>(
            _state: &S,
            _args: ArgsRef,
            _delay: Duration,
        ) -> Result<(), Self::Error>
        where
            W: 'static + Worker<S, Args, Error = E>,
            S: Clone + Send + Sync + 'static,
            AppContext: FromRef<S>,
            Args: Send + Sync + Serialize + for<'de> Deserialize<'de>,
            ArgsRef: Send + Sync + Borrow<Args> + Serialize,
        {
            unimplemented!()
        }

        async fn enqueue_batch<W, S, Args, ArgsRef, E>(
            _state: &S,
            _args: &[ArgsRef],
        ) -> Result<(), Self::Error>
        where
            W: 'static + Worker<S, Args, Error = E>,
            S: Clone + Send + Sync + 'static,
            AppContext: FromRef<S>,
            Args: Send + Sync + Serialize + for<'de> Deserialize<'de>,
            ArgsRef: Send + Sync + Borrow<Args> + Serialize,
        {
            unimplemented!()
        }

        async fn enqueue_batch_delayed<W, S, Args, ArgsRef, E>(
            _state: &S,
            _args: &[ArgsRef],
            _delay: Duration,
        ) -> Result<(), Self::Error>
        where
            W: 'static + Worker<S, Args, Error = E>,
            S: Clone + Send + Sync + 'static,
            AppContext: FromRef<S>,
            Args: Send + Sync + Serialize + for<'de> Deserialize<'de>,
            ArgsRef: Send + Sync + Borrow<Args> + Serialize,
        {
            unimplemented!()
        }
    }

    #[derive(Serialize, Deserialize)]
    struct FooWorkerArgs {
        foo: String,
    }

    struct FooWorker;

    #[async_trait::async_trait]
    impl super::Worker<AppContext, FooWorkerArgs> for FooWorker {
        type Error = crate::error::Error;
        type Enqueuer = FooBackend;

        #[cfg_attr(coverage_nightly, coverage(off))]
        async fn handle(
            &self,
            _state: &AppContext,
            _args: FooWorkerArgs,
        ) -> Result<(), Self::Error> {
            unimplemented!()
        }
    }

    #[fixture]
    #[once]
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn context() -> AppContext {
        let config = AppConfig::test(None).unwrap();
        AppContext::test(Some(config), None, None).unwrap()
    }

    #[rstest]
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn enqueue_config(context: &AppContext) {
        let enqueue_config = FooWorker::enqueue_config(context);
        assert_debug_snapshot!(enqueue_config);
    }
}