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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//! Types for interacting with an Ora store.

use std::{
    fmt::Debug,
    future::Future,
    marker::PhantomData,
    pin::Pin,
    sync::Arc,
    task::{Context, Poll},
};

use async_trait::async_trait;
use futures::{future::Shared, FutureExt};
use ora_client::{ClientOperations, RawTaskResult, ScheduleOperations, TaskOperations};
pub use ora_client::{
    LabelMatch, LabelValueMatch, ScheduleListOrder, Schedules, TaskListOrder, Tasks,
};
use ora_common::{
    schedule::ScheduleDefinition,
    task::{TaskDataFormat, TaskDefinition, TaskStatus},
};
use serde::de::DeserializeOwned;
use time::OffsetDateTime;
use uuid::Uuid;

use crate::Task;

/// A client for interacting with the Ora framework.
///
/// This is usually implemented by a store or any interface
/// over a store.
#[async_trait]
pub trait Client: core::fmt::Debug + Send + Sync + 'static {
    /// Add a new task of the given type.
    async fn add_task<T>(&self, task: TaskDefinition<T>) -> eyre::Result<TaskHandle<T>>
    where
        T: Send + 'static;

    /// Add multiple tasks of a given type.
    ///
    /// This is more performant for some implementations
    /// than calling `add_tasks` repeatedly.
    async fn add_tasks<T, I>(&self, tasks: I) -> eyre::Result<Vec<TaskHandle<T>>>
    where
        T: Send + 'static,
        I: IntoIterator<Item = TaskDefinition<T>> + Send,
        I::IntoIter: ExactSizeIterator + Send;

    /// Retrieve a task by its ID.
    async fn task<T>(&self, task_id: Uuid) -> eyre::Result<TaskHandle<T>>
    where
        T: Send + 'static;

    /// Retrieve multiple tasks.
    ///
    /// The returned task handles are type-erased,
    /// individual tasks can be cast via [`TaskHandle::cast`]
    /// to specific types.
    async fn tasks(&self, options: &Tasks) -> eyre::Result<Vec<TaskHandle>>;

    /// Count current tasks.
    async fn task_count(&self, options: &Tasks) -> eyre::Result<u64>;

    /// Cancel all matching tasks and return handles to tasks
    /// that were cancelled.
    ///
    /// Cancellation is no-op on already finished tasks.
    ///
    /// The returned task handles are type-erased.
    async fn cancel_tasks(&self, options: &Tasks) -> eyre::Result<Vec<TaskHandle>>;

    /// Add a new schedule.
    async fn add_schedule(&self, schedule: ScheduleDefinition) -> eyre::Result<ScheduleHandle>;

    /// Add multiple schedules.
    ///
    /// This is more performant for some implementations
    /// than calling `add_schedule` repeatedly.
    async fn add_schedules<I>(&self, schedules: I) -> eyre::Result<Vec<ScheduleHandle>>
    where
        I: IntoIterator<Item = ScheduleDefinition> + Send,
        I::IntoIter: ExactSizeIterator + Send;

    /// Retrieve a schedule by its ID.
    async fn schedule(&self, schedule_id: Uuid) -> eyre::Result<ScheduleHandle>;

    /// Retrieve multiple schedules.
    async fn schedules(&self, options: &Schedules) -> eyre::Result<Vec<ScheduleHandle>>;

    /// Count current schedules.
    async fn schedule_count(&self, options: &Schedules) -> eyre::Result<u64>;

    /// Cancel all matching schedules and return handles to schedules
    /// that were cancelled.
    ///
    /// Cancellation is no-op on inactive schedules.
    async fn cancel_schedules(&self, options: &Schedules) -> eyre::Result<Vec<ScheduleHandle>>;
}

/// An object-safe version of [`Client`] with type-erased task information and
/// no generic arguments.
#[async_trait]
pub trait ClientObj {
    /// Add a new task of the given type.
    async fn add_task(&self, task: TaskDefinition) -> eyre::Result<TaskHandle>;

    /// Add multiple tasks of a given type.
    ///
    /// This is more performant for some implementations
    /// than calling `add_tasks` repeatedly.
    async fn add_tasks(
        &self,
        tasks: &mut (dyn ExactSizeIterator<Item = TaskDefinition> + Send),
    ) -> eyre::Result<Vec<TaskHandle>>;

    /// Retrieve a task by its ID.
    async fn task(&self, task_id: Uuid) -> eyre::Result<TaskHandle>;

    /// Retrieve multiple tasks.
    async fn tasks(&self, options: &Tasks) -> eyre::Result<Vec<TaskHandle>>;

    /// Cancel all matching tasks and return handles to tasks
    /// that were cancelled.
    ///
    /// Cancellation is no-op on already finished tasks.
    async fn cancel_tasks(&self, options: &Tasks) -> eyre::Result<Vec<TaskHandle>>;

    /// Count current tasks.
    async fn task_count(&self, options: &Tasks) -> eyre::Result<u64>;

    /// Add a new schedule.
    async fn add_schedule(&self, schedule: ScheduleDefinition) -> eyre::Result<ScheduleHandle>;

    /// Add multiple schedules.
    ///
    /// This is more performant for some implementations
    /// than calling `add_schedule` repeatedly.
    async fn add_schedules(
        &self,
        schedules: &mut (dyn ExactSizeIterator<Item = ScheduleDefinition> + Send),
    ) -> eyre::Result<Vec<ScheduleHandle>>;

    /// Retrieve a schedule by its ID.
    async fn schedule(&self, schedule_id: Uuid) -> eyre::Result<ScheduleHandle>;

    /// Retrieve multiple schedules.
    async fn schedules(&self, options: &Schedules) -> eyre::Result<Vec<ScheduleHandle>>;

    /// Count current schedules.
    async fn schedule_count(&self, options: &Schedules) -> eyre::Result<u64>;

    /// Cancel all matching schedules and return handles to schedules
    /// that were cancelled.
    ///
    /// Cancellation is no-op on inactive schedules.
    async fn cancel_schedules(&self, options: &Schedules) -> eyre::Result<Vec<ScheduleHandle>>;
}

#[async_trait]
impl<C> Client for C
where
    C: ClientOperations,
{
    async fn add_task<T>(&self, task: TaskDefinition<T>) -> eyre::Result<TaskHandle<T>>
    where
        T: Send + 'static,
    {
        let id = <Self as ClientOperations>::add_task(self, task.cast()).await?;
        <Self as Client>::task::<T>(self, id).await
    }

    async fn add_tasks<T, I>(&self, tasks: I) -> eyre::Result<Vec<TaskHandle<T>>>
    where
        I: IntoIterator<Item = TaskDefinition<T>> + Send,
        I::IntoIter: ExactSizeIterator + Send,
    {
        let ids = <Self as ClientOperations>::add_tasks(
            self,
            &mut tasks.into_iter().map(TaskDefinition::cast),
        )
        .await?;
        Ok(<Self as ClientOperations>::tasks_by_ids(self, ids)
            .await?
            .into_iter()
            .map(|ops| {
                let ops2 = ops.clone();
                TaskHandle {
                    id: ops.id(),
                    out: async move { Arc::new(ops2.wait_result().await) }
                        .boxed()
                        .shared(),
                    ops,
                    task_type: PhantomData,
                }
            })
            .collect())
    }

    async fn task<T>(&self, task_id: Uuid) -> eyre::Result<TaskHandle<T>> {
        Ok(TaskHandle::new_raw(
            <Self as ClientOperations>::task(self, task_id).await?,
        ))
    }

    async fn tasks(&self, options: &Tasks) -> eyre::Result<Vec<TaskHandle>> {
        Ok(<Self as ClientOperations>::tasks(self, options)
            .await?
            .into_iter()
            .map(|ops| {
                let ops2 = ops.clone();
                TaskHandle {
                    id: ops.id(),
                    out: async move { Arc::new(ops2.wait_result().await) }
                        .boxed()
                        .shared(),
                    ops,
                    task_type: PhantomData,
                }
            })
            .collect())
    }

    async fn task_count(&self, options: &Tasks) -> eyre::Result<u64> {
        <Self as ClientOperations>::task_count(self, options).await
    }

    async fn cancel_tasks(&self, options: &Tasks) -> eyre::Result<Vec<TaskHandle>> {
        <Self as Client>::cancel_tasks(self, options).await
    }

    async fn add_schedule(&self, schedule: ScheduleDefinition) -> eyre::Result<ScheduleHandle> {
        let id = <Self as ClientOperations>::add_schedule(self, schedule).await?;
        <Self as Client>::schedule(self, id).await
    }

    async fn add_schedules<I>(&self, schedules: I) -> eyre::Result<Vec<ScheduleHandle>>
    where
        I: IntoIterator<Item = ScheduleDefinition> + Send,
        I::IntoIter: ExactSizeIterator + Send,
    {
        let ids =
            <Self as ClientOperations>::add_schedules(self, &mut schedules.into_iter()).await?;
        Ok(<Self as ClientOperations>::schedules_by_ids(self, ids)
            .await?
            .into_iter()
            .map(|ops| ScheduleHandle { id: ops.id(), ops })
            .collect())
    }

    async fn schedule(&self, schedule_id: Uuid) -> eyre::Result<ScheduleHandle> {
        let ops = <Self as ClientOperations>::schedule(self, schedule_id).await?;
        Ok(ScheduleHandle {
            id: schedule_id,
            ops,
        })
    }

    async fn schedules(&self, options: &Schedules) -> eyre::Result<Vec<ScheduleHandle>> {
        Ok(<Self as ClientOperations>::schedules(self, options)
            .await?
            .into_iter()
            .map(|ops| ScheduleHandle { id: ops.id(), ops })
            .collect())
    }

    async fn schedule_count(&self, options: &Schedules) -> eyre::Result<u64> {
        <Self as ClientOperations>::schedule_count(self, options).await
    }

    async fn cancel_schedules(&self, options: &Schedules) -> eyre::Result<Vec<ScheduleHandle>> {
        <Self as Client>::cancel_schedules(self, options).await
    }
}

#[async_trait]
impl<C> ClientObj for C
where
    C: Client,
{
    async fn add_task(&self, task: TaskDefinition) -> eyre::Result<TaskHandle> {
        <Self as Client>::add_task(self, task).await
    }

    async fn add_tasks(
        &self,
        tasks: &mut (dyn ExactSizeIterator<Item = TaskDefinition> + Send),
    ) -> eyre::Result<Vec<TaskHandle>> {
        <Self as Client>::add_tasks(self, tasks).await
    }

    async fn task(&self, task_id: Uuid) -> eyre::Result<TaskHandle> {
        <Self as Client>::task(self, task_id).await
    }

    async fn tasks(&self, options: &Tasks) -> eyre::Result<Vec<TaskHandle>> {
        <Self as Client>::tasks(self, options).await
    }

    async fn cancel_tasks(&self, options: &Tasks) -> eyre::Result<Vec<TaskHandle>> {
        <Self as Client>::cancel_tasks(self, options).await
    }

    async fn task_count(&self, options: &Tasks) -> eyre::Result<u64> {
        <Self as Client>::task_count(self, options).await
    }

    async fn add_schedule(&self, schedule: ScheduleDefinition) -> eyre::Result<ScheduleHandle> {
        <Self as Client>::add_schedule(self, schedule).await
    }

    async fn add_schedules(
        &self,
        schedules: &mut (dyn ExactSizeIterator<Item = ScheduleDefinition> + Send),
    ) -> eyre::Result<Vec<ScheduleHandle>> {
        <Self as Client>::add_schedules(self, schedules).await
    }

    async fn schedule(&self, schedule_id: Uuid) -> eyre::Result<ScheduleHandle> {
        <Self as Client>::schedule(self, schedule_id).await
    }

    async fn schedules(&self, options: &Schedules) -> eyre::Result<Vec<ScheduleHandle>> {
        <Self as Client>::schedules(self, options).await
    }

    async fn schedule_count(&self, options: &Schedules) -> eyre::Result<u64> {
        <Self as Client>::schedule_count(self, options).await
    }

    async fn cancel_schedules(&self, options: &Schedules) -> eyre::Result<Vec<ScheduleHandle>> {
        <Self as Client>::cancel_schedules(self, options).await
    }
}

type SharedTaskOutputFut =
    Shared<Pin<Box<dyn Future<Output = Arc<eyre::Result<RawTaskResult>>> + Send>>>;

/// A handle to a task that can be used for further inspections and operations.
pub struct TaskHandle<T = ()> {
    id: Uuid,
    ops: Arc<dyn TaskOperations>,
    out: SharedTaskOutputFut,
    task_type: PhantomData<T>,
}

impl<T> Clone for TaskHandle<T> {
    fn clone(&self) -> Self {
        Self {
            id: self.id,
            ops: self.ops.clone(),
            out: self.out.clone(),
            task_type: self.task_type,
        }
    }
}

impl<T> TaskHandle<T> {
    /// Create a new task handle over a raw implementation.
    pub fn new_raw(task_operations: Arc<dyn TaskOperations>) -> Self {
        let ops2 = task_operations.clone();
        TaskHandle {
            id: task_operations.id(),
            out: async move { Arc::new(ops2.wait_result().await) }
                .boxed()
                .shared(),
            ops: task_operations,
            task_type: PhantomData,
        }
    }
}

impl<T> std::fmt::Debug for TaskHandle<T>
where
    T: Debug + Unpin,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TaskHandle")
            .field("id", &self.id)
            .field("ops", &self.ops)
            .finish_non_exhaustive()
    }
}

#[allow(clippy::missing_errors_doc)]
impl<T> TaskHandle<T> {
    /// Return the task's ID.
    #[must_use]
    pub const fn id(&self) -> Uuid {
        self.id
    }

    /// Return the task's current status.
    pub async fn status(&self) -> eyre::Result<TaskStatus> {
        self.ops.status().await
    }

    /// Return the task's target timestamp.
    pub async fn target(&self) -> eyre::Result<OffsetDateTime> {
        self.ops.target().await.map(Into::into)
    }

    /// Return the task's definition.
    pub async fn definition(&self) -> eyre::Result<TaskDefinition<T>> {
        self.ops.definition().await.map(TaskDefinition::cast)
    }

    /// Return the task's schedule, if any.
    pub async fn schedule(&self) -> eyre::Result<Option<ScheduleHandle>> {
        Ok(self
            .ops
            .schedule()
            .await?
            .map(|ops| ScheduleHandle { id: ops.id(), ops }))
    }

    /// Return when the task was added.
    pub async fn added_at(&self) -> eyre::Result<OffsetDateTime> {
        self.ops.added_at().await.map(Into::into)
    }

    /// Return when the task was ready.
    pub async fn ready_at(&self) -> eyre::Result<Option<OffsetDateTime>> {
        self.ops.ready_at().await.map(|v| v.map(Into::into))
    }
    /// Return when the task was started by a worker.
    pub async fn started_at(&self) -> eyre::Result<Option<OffsetDateTime>> {
        self.ops.started_at().await.map(|v| v.map(Into::into))
    }

    /// Return when the task was completed by a worker.
    pub async fn succeeded_at(&self) -> eyre::Result<Option<OffsetDateTime>> {
        self.ops.succeeded_at().await.map(|v| v.map(Into::into))
    }

    /// Return when the task failed.
    pub async fn failed_at(&self) -> eyre::Result<Option<OffsetDateTime>> {
        self.ops.failed_at().await.map(|v| v.map(Into::into))
    }

    /// Return when the task was cancelled.
    pub async fn cancelled_at(&self) -> eyre::Result<Option<OffsetDateTime>> {
        self.ops.cancelled_at().await.map(|v| v.map(Into::into))
    }

    /// Cancel a task, if the task has already finished
    /// running, this is a no-op.
    pub async fn cancel(&self) -> eyre::Result<()> {
        self.ops.cancel().await
    }

    /// Return the worker's ID that is associated with the task, if any.
    pub async fn worker_id(&self) -> eyre::Result<Option<Uuid>> {
        self.ops.worker_id().await
    }

    /// Cast the handle to a different task type or `()`
    /// to erase the type.
    #[must_use]
    pub fn cast<U>(self) -> TaskHandle<U> {
        TaskHandle {
            id: self.id,
            ops: self.ops,
            out: self.out,
            task_type: PhantomData,
        }
    }
}

impl<T> Future for TaskHandle<T>
where
    T: Task + Unpin,
{
    type Output = eyre::Result<T::Output>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        match self.out.poll_unpin(cx) {
            Poll::Ready(res) => match &*res {
                Ok(raw_out) => Poll::Ready(match &raw_out {
                    RawTaskResult::Success {
                        output_format,
                        output,
                    } => match output_format {
                        TaskDataFormat::Unknown => Err(eyre::eyre!("unknown format")),
                        TaskDataFormat::MessagePack => Ok(rmp_serde::from_slice(output)?),
                        TaskDataFormat::Json => Ok(serde_json::from_slice(output)?),
                    },
                    RawTaskResult::Failure { reason } => Err(eyre::eyre!("task failed: {reason}")),
                    RawTaskResult::Cancelled => Err(eyre::eyre!("task cancelled")),
                }),
                Err(error) => Poll::Ready(Err(eyre::eyre!("{error:?}"))),
            },
            Poll::Pending => Poll::Pending,
        }
    }
}

impl Future for TaskHandle<()> {
    type Output = eyre::Result<TaskOutput>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        match self.out.poll_unpin(cx) {
            Poll::Ready(res) => match &*res {
                Ok(raw_out) => Poll::Ready(match &raw_out {
                    RawTaskResult::Success {
                        output_format,
                        output,
                    } => Ok(TaskOutput {
                        output: output.clone(),
                        output_format: *output_format,
                    }),
                    RawTaskResult::Failure { reason } => Err(eyre::eyre!("task failed: {reason}")),
                    RawTaskResult::Cancelled => Err(eyre::eyre!("task cancelled")),
                }),
                Err(error) => Poll::Ready(Err(eyre::eyre!("{error:?}"))),
            },
            Poll::Pending => Poll::Pending,
        }
    }
}

/// Output of a task.
#[derive(Debug)]
pub struct TaskOutput {
    output_format: TaskDataFormat,
    output: Vec<u8>,
}

impl TaskOutput {
    /// Deserialize the task's output.
    ///
    /// # Errors
    ///
    /// Deserialization errors are returned.
    pub fn get<T: DeserializeOwned>(&self) -> eyre::Result<T> {
        match self.output_format {
            TaskDataFormat::Unknown => eyre::bail!("unknown format"),
            TaskDataFormat::MessagePack => Ok(rmp_serde::from_slice(&self.output)?),
            TaskDataFormat::Json => Ok(serde_json::from_slice(&self.output)?),
        }
    }

    /// Expose the raw output bytes.
    #[must_use]
    pub fn bytes(&self) -> &[u8] {
        &self.output
    }

    /// Convert into the raw output bytes.
    #[must_use]
    pub fn into_bytes(self) -> Vec<u8> {
        self.output
    }

    /// Return the task's output format.
    #[must_use]
    pub fn format(&self) -> TaskDataFormat {
        self.output_format
    }
}

/// A handle to a schedule that allows inspection and operations.
#[derive(Debug, Clone)]
pub struct ScheduleHandle {
    id: Uuid,
    ops: Arc<dyn ScheduleOperations>,
}

#[allow(clippy::missing_errors_doc)]
impl ScheduleHandle {
    /// Create a new schedule handle over a raw implementation.
    pub fn new_raw(ops: Arc<dyn ScheduleOperations>) -> Self {
        ScheduleHandle { id: ops.id(), ops }
    }

    /// Return the schedule's ID.
    #[must_use]
    pub fn id(&self) -> Uuid {
        self.id
    }

    /// Return the schedule's definition.
    pub async fn definition(&self) -> eyre::Result<ScheduleDefinition> {
        self.ops.definition().await
    }

    /// Return whether the schedule is currently active,
    /// meaning it has a currently running task or
    /// there can be more tasks spawned by it in the future.
    pub async fn is_active(&self) -> eyre::Result<bool> {
        self.ops.is_active().await
    }

    /// Return whether the schedule was cancelled.
    pub async fn cancelled_at(&self) -> eyre::Result<Option<OffsetDateTime>> {
        self.ops.cancelled_at().await.map(|v| v.map(Into::into))
    }

    /// Return the latest active task of the schedule.
    ///
    /// A task is considered active if it has not succeeded, has
    /// not failed and has not been cancelled.
    pub async fn active_task(&self) -> eyre::Result<Option<TaskHandle>> {
        let ops = self.ops.active_task().await?;
        Ok(ops.map(|ops| {
            let ops2 = ops.clone();
            TaskHandle {
                id: ops.id(),
                ops,
                out: async move { Arc::new(ops2.wait_result().await) }
                    .boxed()
                    .shared(),
                task_type: PhantomData,
            }
        }))
    }

    /// Cancel the schedule and all of its active tasks.
    pub async fn cancel(&self) -> eyre::Result<()> {
        self.ops.cancel().await
    }
}