apalis-core 1.0.0

Core for apalis: simple, extensible multithreaded background processing for Rust
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
//! A helper macros to delegate `Backend` trait methods from a wrapper type to its inner backend field.

/// This macro is used to implement the `Sink` trait for a wrapper type that contains a backend field. It delegates the `Sink` methods to the inner backend field, allowing the wrapper type to behave like a `Sink` without having to manually implement each method.
#[macro_export]
macro_rules! delegate_sink {
    ($wrapper:ident<$($lt:lifetime,)? $backend:ident $(, $rest:ident)* $(,)?>, $field:ident) => {
        impl<$($lt,)? $backend, $($rest,)* T, Err> Sink<T> for $wrapper<$($lt,)? $backend, $($rest,)*>
        where
            $backend: Sink<T, Error = Err> + Unpin,
            $($rest: Unpin,)*
        {
            type Error = Err;

            fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
                self.get_mut().$field.start_send_unpin(item)
            }

            fn poll_ready(
                self: Pin<&mut Self>,
                cx: &mut Context<'_>,
            ) -> Poll<Result<(), Self::Error>> {
                self.get_mut().$field.poll_ready_unpin(cx)
            }

            fn poll_flush(
                self: Pin<&mut Self>,
                cx: &mut Context<'_>,
            ) -> Poll<Result<(), Self::Error>> {
                self.get_mut().$field.poll_flush_unpin(cx)
            }

            fn poll_close(
                self: Pin<&mut Self>,
                cx: &mut Context<'_>,
            ) -> Poll<Result<(), Self::Error>> {
                self.get_mut().$field.poll_close_unpin(cx)
            }
        }
    };
}

/// This macro is used to implement the `Deref` and `DerefMut` traits for a wrapper type
/// that contains a backend field. It delegates to the inner backend field, allowing the
/// wrapper type to behave like the backend without having to manually implement each method.
#[macro_export]
macro_rules! delegate_deref {
    ($wrapper:ident<$($lt:lifetime,)? $backend:ident $(, $rest:ident)* $(,)?>, $field:ident) => {
        impl<$($lt,)? $backend, $($rest,)*> std::ops::Deref for $wrapper<$($lt,)? $backend, $($rest,)*> {
            type Target = $backend;

            fn deref(&self) -> &Self::Target {
                &self.$field
            }
        }

        impl<$($lt,)? $backend, $($rest,)*> std::ops::DerefMut for $wrapper<$($lt,)? $backend, $($rest,)*> {
            fn deref_mut(&mut self) -> &mut Self::Target {
                &mut self.$field
            }
        }
    };
}

/// This macro is used to implement the `BackendConfig` trait for a wrapper type that
/// contains a backend field. It delegates the associated types and methods to the inner
/// backend field, allowing the wrapper type to behave like a `BackendConfig` without having
/// to manually implement each item.
#[macro_export]
macro_rules! delegate_config {
    ($wrapper:ident<$($lt:lifetime,)? $backend:ident $(, $rest:ident)* $(,)?>, $field:ident) => {
        impl<$($lt,)? $backend, $($rest,)*> BackendConfig for $wrapper<$($lt,)? $backend, $($rest,)*>
        where
            $backend: BackendConfig,
        {
            type Id = $backend::Id;
            type Args = $backend::Args;
            type Kind = $backend::Kind;
            type Config = $backend::Config;
            type Layer = $backend::Layer;

            fn config(&self) -> &Self::Config {
                self.$field.config()
            }

            fn middleware(&mut self, worker: &mut WorkerContext) -> Self::Layer {
                self.$field.middleware(worker)
            }
        }
    };
}

/// This macro is used to implement the `WireFormatBackend` trait for a wrapper type that
/// contains a backend field. It delegates the associated types and methods to the inner
/// backend field, allowing the wrapper type to behave like a `WireFormatBackend` without having
/// to manually implement each item.
#[macro_export]
macro_rules! delegate_codec {
    ($wrapper:ident<$($lt:lifetime,)? $backend:ident $(, $rest:ident)* $(,)?>, $field:ident) => {
        impl<$($lt,)? $backend, $($rest,)*> WireFormatBackend for $wrapper<$($lt,)? $backend, $($rest,)*>
        where
            $backend: WireFormatBackend,
        {
            type Compact = $backend::Compact;
            type Codec = $backend::Codec;

            fn codec(&self) -> &Self::Codec {
                self.$field.codec()
            }
        }
    };
}

/// Delegates FetchById, Update, Reschedule, Vacuum, ResumeById,
/// ResumeAbandoned, RegisterWorker, and WaitForCompletion to a named field
/// in one call. The caller supplies shared extra bounds, and optionally a
/// `wrap` expression applied to every delegated method's `Result`.
///
/// `wrap` receives the `Result<T, B::Error>` returned by the inner call and
/// must produce the wrapper's own `Result<T, Self::Error>` — use it for
/// `.map_err(...)`, logging, retries, or any other postprocessing.
///
/// The `impl<...>` generic list may include a leading lifetime (e.g. `impl<'a, B>`)
/// when the wrapper type itself is generic over a lifetime.
///
/// ```ignore
/// // plain forwarding
/// delegate_expose!(
///     impl<B> for RawDataBackend<B>
///     where {
///         B: Send + 'static,
///         B::Compact: Send + Clone + 'static,
///     }
///     => inner
/// );
///
/// // with a lifetime parameter
/// delegate_expose!(
///     impl<'a, B> for BorrowedBackend<'a, B>
///     where {
///         B: Send + 'static,
///     }
///     => inner
/// );
///
/// // with postprocessing
/// delegate_expose!(
///     impl<B, F, E2> for MapErr<B, F>
///     where {
///         B: Backend,
///         F: Fn(B::Error) -> E2,
///         E2: std::error::Error + Send + Sync + 'static,
///     }
///     => backend,
///     wrap = |this, result| result.map_err(|err| (this.f)(err))
/// );
/// ```
#[macro_export]
macro_rules! delegate_expose {
    (
        impl<$($lt:lifetime),* $(,)? $($generic:ident),+> for $wrapper:ty
        where { $($bound:tt)+ }
        => $field:ident
    ) => {
        $crate::delegate_expose!(
            impl<$($lt),* , $($generic),+> for $wrapper
            where { $($bound)+ }
            => $field,
            wrap = |this, result| result
        );
    };
    (
        impl<$($lt:lifetime),* $(,)? $($generic:ident),+> for $wrapper:ty
        where { $($bound:tt)+ }
        => $field:ident,
        wrap = |$this:ident, $result:ident| $wrap_body:expr
    ) => {
        impl<$($lt,)* $($generic),+> FetchById for $wrapper
        where
            B: FetchById,
            B::Task: Send,
            $($bound)+
        {
            async fn fetch_by_id(
                &mut self,
                task_id: &$crate::task::task_id::TaskId,
            ) -> Result<Option<B::Task>, Self::Error> {
                let $result = self.$field.fetch_by_id(task_id).await;
                #[allow(unused_variables)]
                let $this = &*self;
                $wrap_body
            }
        }

        impl<$($lt,)* $($generic),+> Update for $wrapper
        where
            B: Update + Send,
            B::Task: Send,
            $($bound)+
        {
            async fn update(
                &mut self,
                task: Self::Task,
            ) -> Result<(), Self::Error> {
                let $result = self.$field.update(task).await;
                #[allow(unused_variables)]
                let $this = &*self;
                $wrap_body
            }
        }

        impl<$($lt,)* $($generic),+> Reschedule for $wrapper
        where
            B: Reschedule,
            B::Task: Send,
            $($bound)+
        {
            async fn reschedule(
                &mut self,
                task: Self::Task,
                wait: std::time::Duration,
            ) -> Result<(), Self::Error> {
                let $result = self.$field.reschedule(task, wait).await;
                #[allow(unused_variables)]
                let $this = &*self;
                $wrap_body
            }
        }

        impl<$($lt,)* $($generic),+> Vacuum for $wrapper
        where
            B: Vacuum,
            $($bound)+
        {
            async fn vacuum(&mut self) -> Result<usize, Self::Error> {
                let $result = self.$field.vacuum().await;
                #[allow(unused_variables)]
                let $this = &*self;
                $wrap_body
            }

            async fn vacuum_before(&mut self, duration: Duration) -> Result<usize, Self::Error> {
                let $result = self.$field.vacuum_before(duration).await;
                #[allow(unused_variables)]
                let $this = &*self;
                $wrap_body
            }
        }

        impl<$($lt,)* $($generic),+> ResumeById for $wrapper
        where
            B: ResumeById,
            $($bound)+
        {
            async fn resume_by_id(
                &mut self,
                id: TaskId,
            ) -> Result<bool, Self::Error> {
                let $result = self.$field.resume_by_id(id).await;
                #[allow(unused_variables)]
                let $this = &*self;
                $wrap_body
            }
        }

        impl<$($lt,)* $($generic),+> ResumeAbandoned for $wrapper
        where
            B: ResumeAbandoned,
            $($bound)+
        {
            async fn resume_abandoned(&mut self) -> Result<usize, Self::Error> {
                let $result = self.$field.resume_abandoned().await;
                #[allow(unused_variables)]
                let $this = &*self;
                $wrap_body
            }
        }

        impl<$($lt,)* $($generic),+> RegisterWorker for $wrapper
        where
            B: RegisterWorker,
            $($bound)+
        {
            async fn register_worker(&mut self, worker_id: String) -> Result<(), Self::Error> {
                let $result = self.$field.register_worker(worker_id).await;
                #[allow(unused_variables)]
                let $this = &*self;
                $wrap_body
            }
        }

        impl<$($lt,)* Output, $($generic),+> WaitForCompletion<Output> for $wrapper
        where
            B: WaitForCompletion<Output> + Sync,
            Output: 'static,
            $($bound)+
        {
            type ResultStream = futures_core::stream::BoxStream<'static, Result<TaskResult<Output>, Self::Error>>;

            fn wait_for(
                &mut self,
                task_ids: impl IntoIterator<Item = TaskId>,
            ) -> Self::ResultStream {
                use futures_util::StreamExt;
                let $result = self.$field.wait_for(task_ids);
                #[allow(unused_variables)]
                let $this = &*self;
                $wrap_body.boxed()
            }

            async fn check_status(
                &mut self,
                task_ids: impl IntoIterator<Item = TaskId> + Send,
            ) -> Result<Vec<TaskResult<Output>>, Self::Error> {
                let $result = self.$field.check_status(task_ids).await;
                #[allow(unused_variables)]
                let $this = &*self;
                $wrap_body
            }
        }

        impl<$($lt,)* $($generic),+> ListQueues for $wrapper
        where
            B: ListQueues + Send + Sync,
            $($bound)+
        {
            async fn list_queues(
                &self,
            ) -> Result<Vec<QueueInfo>, Self::Error> {
                let $result = self.$field.list_queues().await;
                 #[allow(unused_variables)]
                let $this = &*self;
                $wrap_body
            }
        }

        impl<$($lt,)* $($generic),+> ListWorkers for $wrapper
        where
            B: ListWorkers + Send + Sync,
            $($bound)+
        {
            async fn list_workers(
                &self,
            ) -> Result<Vec<RunningWorker>, Self::Error> {
                let $result = self.$field.list_workers().await;
                #[allow(unused_variables)]
                let $this = &*self;
                $wrap_body
            }

            async fn list_all_workers(
                &self,
            ) -> Result<Vec<RunningWorker>, Self::Error> {
                let $result = self.$field.list_all_workers().await;
                #[allow(unused_variables)]
                let $this = &*self;
                $wrap_body
            }
        }

        impl<$($lt,)* $($generic),+> ListTasks for $wrapper
        where
            B: ListTasks + Send + Sync,
            $($bound)+
        {
            async fn list_tasks(
                &self,
                filter: &Filter,
            ) -> Result<Vec<Task<Self::Compact>>, Self::Error> {
                let $result = self.$field.list_tasks(filter).await;
                #[allow(unused_variables)]
                let $this = &*self;
                $wrap_body
            }
        }

        impl<$($lt,)* $($generic),+> ListAllTasks for $wrapper
        where
            B: ListAllTasks + Send + Sync,
            $($bound)+
        {
            async fn list_all_tasks(
                &self,
                filter: &Filter,
            ) -> Result<Vec<Task<Self::Compact>>, Self::Error> {
                let $result = self.$field.list_all_tasks(filter).await;
                #[allow(unused_variables)]
                let $this = &*self;
                $wrap_body
            }
        }

        impl<$($lt,)* $($generic),+> Metrics for $wrapper
        where
            B: Metrics + Send + Sync,
            $($bound)+
        {
            async fn global(
                &self,
            ) -> Result<Vec<Statistic>, Self::Error> {
                let $result = self.$field.global().await;
                #[allow(unused_variables)]
                let $this = &*self;
                $wrap_body
            }

            async fn fetch_by_queue(
                &self,
            ) -> Result<Vec<Statistic>, Self::Error> {
                let $result = self.$field.fetch_by_queue().await;
                #[allow(unused_variables)]
                let $this = &*self;
                $wrap_body
            }
        }
    };
}