ora_storage_memory/
lib.rs

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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
//! A simple in-memory storage backend with no persistence.

use std::{sync::Arc, time::SystemTime};

use async_trait::async_trait;
use eyre::{bail, Context};
use indexmap::IndexSet;
use parking_lot::RwLock;
use uuid::Uuid;

use ora_storage::{
    CancelledJob, CancelledSchedule, ExecutionDetails, IndexMap, JobExecutionStatus,
    JobQueryFilters, JobQueryOrder, JobQueryResult, JobRetryPolicy, JobTimeoutPolicy, JobType,
    NewExecution, NewJob, NewSchedule, PendingExecution, PendingJob, PendingSchedule,
    ReadyExecution, ScheduleJobCreationPolicy, ScheduleJobTimingPolicy, ScheduleQueryFilters,
    ScheduleQueryOrder, ScheduleQueryResult, ScheduleTimeRange, Storage,
};

mod job_query;
mod schedule_query;
mod snapshot;

/// A storage backend that holds all data in memory.
///
/// It serves as a reference implementation for storage backends
/// as well as a test platform for the server itself.
/// It is optimized for simplicity and readability, and is not
/// intended for production use.
///
/// It may be used for small-scale production applications
/// where persistence is not required and the amount of data
/// is small enough to fit in memory.
//
// Indexes and other helper data structures are omitted
// on purpose to keep the implementation simple.
//
// The implementation also contains more safety checks and
// assertions that test the server itself.
#[derive(Debug, Default, Clone)]
#[must_use]
pub struct MemoryStorage {
    job_types: Arc<RwLock<IndexMap<String, JobType>>>,

    // Jobs are partitioned by whether they are schedulable or unschedulable.
    schedulable_jobs: Arc<RwLock<IndexMap<Uuid, Job>>>,
    unschedulable_jobs: Arc<RwLock<IndexMap<Uuid, Job>>>,

    // Executions are partitioned by the phase they are in.
    pending_executions: Arc<RwLock<IndexMap<Uuid, Execution>>>,
    ready_executions: Arc<RwLock<IndexMap<Uuid, Execution>>>,
    assigned_executions: Arc<RwLock<IndexMap<Uuid, Execution>>>,
    started_executions: Arc<RwLock<IndexMap<Uuid, Execution>>>,
    succeeded_executions: Arc<RwLock<IndexMap<Uuid, Execution>>>,
    failed_executions: Arc<RwLock<IndexMap<Uuid, Execution>>>,

    // Schedules are partitioned by whether they are schedulable or unschedulable.
    schedulable_schedules: Arc<RwLock<IndexMap<Uuid, Schedule>>>,
    unschedulable_schedules: Arc<RwLock<IndexMap<Uuid, Schedule>>>,
}

impl MemoryStorage {
    /// Create a new in-memory storage backend.
    pub fn new() -> Self {
        Self::default()
    }
}

#[async_trait]
impl Storage for MemoryStorage {
    async fn job_types_added(&self, job_types: Vec<JobType>) -> eyre::Result<()> {
        self.job_types.write().extend(
            job_types
                .iter()
                .map(|job_type| (job_type.id.clone(), job_type.clone())),
        );

        Ok(())
    }

    async fn jobs_added(&self, new_jobs: Vec<NewJob>) -> eyre::Result<()> {
        for new_job in new_jobs {
            let mut jobs = self.schedulable_jobs.write();

            if jobs.contains_key(&new_job.id) {
                bail!("job with ID {} already exists", new_job.id);
            }

            jobs.insert(new_job.id, Job::from(new_job));
        }

        Ok(())
    }

    async fn jobs_cancelled(
        &self,
        job_ids: &[Uuid],
        timestamp: SystemTime,
    ) -> eyre::Result<Vec<CancelledJob>> {
        let mut cancelled_jobs = Vec::with_capacity(job_ids.len());

        for job_id in job_ids {
            let active_job = self.schedulable_jobs.write().swap_remove(job_id);

            if let Some(mut job) = active_job {
                debug_assert!(job.cancelled_at.is_none());
                job.cancelled_at = Some(timestamp);

                let active_execution = self
                    .pending_executions
                    .read()
                    .iter()
                    .find_map(|(_, execution)| {
                        if &execution.job_id == job_id {
                            Some(execution.id)
                        } else {
                            None
                        }
                    })
                    .or_else(|| {
                        self.ready_executions
                            .read()
                            .iter()
                            .find_map(|(_, execution)| {
                                if &execution.job_id == job_id {
                                    Some(execution.id)
                                } else {
                                    None
                                }
                            })
                    })
                    .or_else(|| {
                        self.assigned_executions
                            .read()
                            .iter()
                            .find_map(|(_, execution)| {
                                if &execution.job_id == job_id {
                                    Some(execution.id)
                                } else {
                                    None
                                }
                            })
                    })
                    .or_else(|| {
                        self.started_executions
                            .read()
                            .iter()
                            .find_map(|(_, execution)| {
                                if &execution.job_id == job_id {
                                    Some(execution.id)
                                } else {
                                    None
                                }
                            })
                    });

                if active_execution.is_some() {
                    job.marked_unschedulable_at = Some(timestamp);
                    self.unschedulable_jobs.write().insert(*job_id, job);
                } else {
                    self.schedulable_jobs.write().insert(*job_id, job);
                }

                cancelled_jobs.push(CancelledJob {
                    id: *job_id,
                    active_execution,
                });
            }
        }

        Ok(cancelled_jobs)
    }

    async fn executions_added(
        &self,
        executions: Vec<NewExecution>,
        timestamp: SystemTime,
    ) -> eyre::Result<()> {
        for execution in executions {
            let mut pending_executions = self.pending_executions.write();
            if pending_executions.contains_key(&execution.id) {
                bail!("execution with ID {} already exists", execution.id);
            }

            pending_executions.insert(
                execution.id,
                Execution {
                    id: execution.id,
                    job_id: execution.job_id,
                    target_execution_time: execution.target_execution_time,
                    created_at: timestamp,
                    executor_id: None,
                    ready_at: None,
                    assigned_at: None,
                    started_at: None,
                    succeeded_at: None,
                    failed_at: None,
                    output_payload_json: None,
                    failure_reason: None,
                },
            );
        }

        Ok(())
    }

    async fn executions_ready(
        &self,
        execution_ids: &[Uuid],
        timestamp: SystemTime,
    ) -> eyre::Result<()> {
        for execution_id in execution_ids {
            let execution = self.pending_executions.write().swap_remove(execution_id);

            if let Some(mut execution) = execution {
                debug_assert!(execution.ready_at.is_none());
                execution.ready_at = Some(timestamp);
                self.ready_executions
                    .write()
                    .insert(*execution_id, execution);
            }
        }

        Ok(())
    }

    async fn execution_assigned(
        &self,
        execution_id: Uuid,
        executor_id: Uuid,
        timestamp: SystemTime,
    ) -> eyre::Result<()> {
        let execution = self.ready_executions.write().swap_remove(&execution_id);

        if let Some(mut execution) = execution {
            debug_assert!(execution.assigned_at.is_none());
            execution.assigned_at = Some(timestamp);
            execution.executor_id = Some(executor_id);

            self.assigned_executions
                .write()
                .insert(execution_id, execution);
        }

        Ok(())
    }

    async fn execution_started(
        &self,
        execution_id: Uuid,
        timestamp: SystemTime,
    ) -> eyre::Result<()> {
        let execution = self.assigned_executions.write().swap_remove(&execution_id);
        if let Some(mut execution) = execution {
            debug_assert!(execution.started_at.is_none());
            execution.started_at = Some(timestamp);

            self.started_executions
                .write()
                .insert(execution_id, execution);
        }

        Ok(())
    }

    async fn execution_succeeded(
        &self,
        execution_id: Uuid,
        timestamp: SystemTime,
        output_payload_json: String,
    ) -> eyre::Result<()> {
        // executions may succeed at any phase, so we need to check all phases
        let mut execution = if let Some(execution) =
            self.pending_executions.write().swap_remove(&execution_id)
        {
            execution
        } else if let Some(execution) = self.ready_executions.write().swap_remove(&execution_id) {
            execution
        } else if let Some(execution) = self.assigned_executions.write().swap_remove(&execution_id)
        {
            execution
        } else if let Some(execution) = self.started_executions.write().swap_remove(&execution_id) {
            execution
        } else {
            return Ok(());
        };

        debug_assert!(execution.succeeded_at.is_none());
        debug_assert!(execution.failed_at.is_none());

        execution.succeeded_at = Some(timestamp);
        execution.output_payload_json = Some(output_payload_json);

        let job = self.schedulable_jobs.write().swap_remove(&execution.job_id);
        if let Some(mut job) = job {
            debug_assert!(job.marked_unschedulable_at.is_none());
            job.marked_unschedulable_at = Some(timestamp);

            self.unschedulable_jobs
                .write()
                .insert(execution.job_id, job);
        }

        self.succeeded_executions
            .write()
            .insert(execution_id, execution);

        Ok(())
    }

    async fn executions_failed(
        &self,
        execution_ids: &[Uuid],
        timestamp: SystemTime,
        reason: String,
        mark_job_inactive: bool,
    ) -> eyre::Result<()> {
        // executions may fail at any phase, so we need to check all phases
        for execution_id in execution_ids {
            let mut execution = if let Some(execution) =
                self.pending_executions.write().swap_remove(execution_id)
            {
                execution
            } else if let Some(execution) = self.ready_executions.write().swap_remove(execution_id)
            {
                execution
            } else if let Some(execution) =
                self.assigned_executions.write().swap_remove(execution_id)
            {
                execution
            } else if let Some(execution) =
                self.started_executions.write().swap_remove(execution_id)
            {
                execution
            } else {
                return Ok(());
            };

            debug_assert!(execution.succeeded_at.is_none());
            debug_assert!(execution.failed_at.is_none());

            execution.failed_at = Some(timestamp);
            execution.failure_reason = Some(reason.clone());

            if mark_job_inactive {
                let job = self.schedulable_jobs.write().swap_remove(&execution.job_id);
                if let Some(mut job) = job {
                    debug_assert!(job.marked_unschedulable_at.is_none());
                    job.marked_unschedulable_at = Some(timestamp);

                    self.unschedulable_jobs
                        .write()
                        .insert(execution.job_id, job);
                }
            }

            self.failed_executions
                .write()
                .insert(*execution_id, execution);
        }

        Ok(())
    }

    async fn orphan_execution_ids(&self, executor_ids: &[Uuid]) -> eyre::Result<Vec<Uuid>> {
        Ok(self
            .assigned_executions
            .read()
            .iter()
            .filter_map(|(id, execution)| {
                if executor_ids.contains(&execution.executor_id.unwrap()) {
                    None
                } else {
                    Some(*id)
                }
            })
            .chain(
                self.started_executions
                    .read()
                    .iter()
                    .filter_map(|(id, execution)| {
                        if executor_ids.contains(&execution.executor_id.unwrap()) {
                            None
                        } else {
                            Some(*id)
                        }
                    }),
            )
            .collect())
    }

    async fn jobs_unschedulable(
        &self,
        job_ids: &[Uuid],
        timestamp: SystemTime,
    ) -> eyre::Result<()> {
        for job_id in job_ids {
            let job = self.schedulable_jobs.write().swap_remove(job_id);
            if let Some(mut job) = job {
                debug_assert!(job.marked_unschedulable_at.is_none());
                job.marked_unschedulable_at = Some(timestamp);

                self.unschedulable_jobs.write().insert(*job_id, job);
            } else {
                debug_assert!(false, "active job with ID {job_id} not found");
            }
        }

        Ok(())
    }

    async fn pending_executions(&self, after: Option<Uuid>) -> eyre::Result<Vec<PendingExecution>> {
        Ok(self
            .pending_executions
            .read()
            .iter()
            .filter_map(|(id, execution)| {
                if let Some(after) = after {
                    if *id <= after {
                        return None;
                    }
                }

                Some(PendingExecution {
                    id: *id,
                    target_execution_time: execution.target_execution_time,
                })
            })
            .collect())
    }

    async fn ready_executions(&self, after: Option<Uuid>) -> eyre::Result<Vec<ReadyExecution>> {
        Ok({
            let mut executions = self
                .ready_executions
                .read()
                .iter()
                .filter_map(|(id, execution)| {
                    let jobs = self.schedulable_jobs.read();
                    let Some(job) = jobs.get(&execution.job_id) else {
                        debug_assert!(false, "active job with ID {} not found", execution.job_id);
                        return None;
                    };

                    Some(ReadyExecution {
                        id: *id,
                        target_execution_time: execution.target_execution_time,
                        job_id: execution.job_id,
                        input_payload_json: job.input_payload_json.clone(),
                        attempt_number: 0,
                        job_type_id: job.job_type_id.clone(),
                        timeout_policy: job.timeout_policy,
                    })
                })
                .collect::<Vec<_>>();

            executions.sort_by_key(|execution| execution.id);

            if let Some(after) = after {
                executions.retain(|execution| execution.id > after);
            }

            for execution in &mut executions {
                execution.attempt_number = u64::try_from(
                    self.executions_by_job_id(execution.job_id)
                        .position(|id| id == execution.id)
                        .unwrap(),
                )
                .unwrap()
                    + 1;
            }

            executions
        })
    }

    async fn pending_jobs(&self, after: Option<Uuid>) -> eyre::Result<Vec<PendingJob>> {
        let mut jobs: Vec<PendingJob> = {
            let pending_executions = self.pending_executions.read();
            let ready_executions = self.ready_executions.read();
            let assigned_executions = self.assigned_executions.read();
            let started_executions = self.started_executions.read();

            self.schedulable_jobs
                .read()
                .iter()
                .filter_map(|(job_id, job)| {
                    if pending_executions
                        .values()
                        .any(|execution| execution.job_id == *job_id)
                    {
                        return None;
                    }

                    if ready_executions
                        .values()
                        .any(|execution| execution.job_id == *job_id)
                    {
                        return None;
                    }

                    if assigned_executions
                        .values()
                        .any(|execution| execution.job_id == *job_id)
                    {
                        return None;
                    }

                    if started_executions
                        .values()
                        .any(|execution| execution.job_id == *job_id)
                    {
                        return None;
                    }

                    if let Some(after) = after {
                        if job.id <= after {
                            return None;
                        }
                    }

                    Some(PendingJob {
                        id: job.id,
                        target_execution_time: job.target_execution_time,
                        execution_count: 0,
                        retry_policy: job.retry_policy,
                        timeout_policy: job.timeout_policy,
                    })
                })
                .collect::<Vec<_>>()
        };

        for job in &mut jobs {
            job.execution_count = u64::try_from(self.executions_by_job_id(job.id).len()).unwrap();
        }

        Ok(jobs)
    }

    async fn query_jobs(
        &self,
        cursor: Option<String>,
        limit: usize,
        order: JobQueryOrder,
        filters: JobQueryFilters,
    ) -> eyre::Result<JobQueryResult> {
        let cursor: Option<job_query::Cursor> = match cursor {
            Some(cursor) => serde_json::from_str(&cursor).wrap_err("invalid cursor")?,
            None => None,
        };

        Ok(self.query_jobs_impl(cursor, limit, order, filters))
    }

    async fn query_job_ids(&self, filters: JobQueryFilters) -> eyre::Result<Vec<Uuid>> {
        Ok(self.query_job_ids_impl(filters))
    }

    async fn count_jobs(&self, filters: JobQueryFilters) -> eyre::Result<u64> {
        Ok(self.count_jobs_impl(filters))
    }

    async fn query_job_types(&self) -> eyre::Result<Vec<JobType>> {
        Ok(self.job_types.read().values().cloned().collect())
    }

    async fn delete_jobs(&self, filters: JobQueryFilters) -> eyre::Result<Vec<Uuid>> {
        let job_ids = self.query_job_ids_impl(filters);

        let mut executions_to_remove = Vec::new();

        for job_id in &job_ids {
            self.unschedulable_jobs
                .write()
                .swap_remove(job_id)
                .or_else(|| self.schedulable_jobs.write().swap_remove(job_id));

            executions_to_remove.extend(self.executions_by_job_id(*job_id));
        }

        self.pending_executions
            .write()
            .retain(|id, _| !executions_to_remove.contains(id));
        self.ready_executions
            .write()
            .retain(|id, _| !executions_to_remove.contains(id));
        self.assigned_executions
            .write()
            .retain(|id, _| !executions_to_remove.contains(id));
        self.started_executions
            .write()
            .retain(|id, _| !executions_to_remove.contains(id));
        self.succeeded_executions
            .write()
            .retain(|id, _| !executions_to_remove.contains(id));
        self.failed_executions
            .write()
            .retain(|id, _| !executions_to_remove.contains(id));

        Ok(job_ids)
    }

    async fn schedules_added(&self, schedules: Vec<NewSchedule>) -> eyre::Result<()> {
        for schedule in schedules {
            let mut active_schedules = self.schedulable_schedules.write();

            if self
                .schedulable_schedules
                .write()
                .contains_key(&schedule.id)
            {
                bail!("schedule with ID {} already exists", schedule.id);
            }

            active_schedules.insert(schedule.id, Schedule::from(schedule));
        }

        Ok(())
    }

    async fn schedules_cancelled(
        &self,
        schedule_ids: &[Uuid],
        timestamp: SystemTime,
    ) -> eyre::Result<Vec<CancelledSchedule>> {
        let mut cancelled_schedules = Vec::with_capacity(schedule_ids.len());

        for schedule_id in schedule_ids {
            let schedule = self.schedulable_schedules.write().swap_remove(schedule_id);

            if let Some(mut schedule) = schedule {
                debug_assert!(schedule.cancelled_at.is_none());
                schedule.cancelled_at = Some(timestamp);

                debug_assert!(schedule.marked_unschedulable_at.is_none());
                schedule.marked_unschedulable_at = Some(timestamp);

                let schedule_id = schedule.id;

                self.unschedulable_schedules
                    .write()
                    .insert(schedule_id, schedule);

                cancelled_schedules.push(CancelledSchedule { id: schedule_id });
            }
        }

        Ok(cancelled_schedules)
    }

    async fn pending_schedules(&self, after: Option<Uuid>) -> eyre::Result<Vec<PendingSchedule>> {
        Ok(self
            .schedulable_schedules
            .read()
            .iter()
            .filter_map(|(id, schedule)| {
                if self
                    .schedulable_jobs
                    .read()
                    .values()
                    .any(|job| job.schedule_id == Some(schedule.id))
                {
                    return None;
                }

                if let Some(after) = after {
                    if *id <= after {
                        return None;
                    }
                }

                Some(PendingSchedule {
                    id: *id,
                    job_timing_policy: schedule.job_timing_policy.clone(),
                    job_creation_policy: schedule.job_creation_policy.clone(),
                    last_target_execution_time: self.last_target_execution_time(schedule.id),
                    time_range: schedule.time_range,
                })
            })
            .collect::<Vec<_>>())
    }

    async fn query_schedules(
        &self,
        cursor: Option<String>,
        limit: usize,
        filters: ScheduleQueryFilters,
        order: ScheduleQueryOrder,
    ) -> eyre::Result<ScheduleQueryResult> {
        let cursor: Option<schedule_query::Cursor> = match cursor {
            Some(cursor) => serde_json::from_str(&cursor).wrap_err("invalid cursor")?,
            None => None,
        };

        Ok(self.query_schedules_impl(cursor, limit, order, filters))
    }

    async fn query_schedule_ids(&self, filters: ScheduleQueryFilters) -> eyre::Result<Vec<Uuid>> {
        Ok(self.query_schedule_ids_impl(filters))
    }

    async fn count_schedules(&self, filters: ScheduleQueryFilters) -> eyre::Result<u64> {
        Ok(self.count_schedules_impl(filters))
    }

    async fn schedules_unschedulable(
        &self,
        schedule_ids: &[Uuid],
        timestamp: SystemTime,
    ) -> eyre::Result<()> {
        for schedule_id in schedule_ids {
            let schedule = self.schedulable_schedules.write().swap_remove(schedule_id);

            if let Some(mut schedule) = schedule {
                debug_assert!(schedule.marked_unschedulable_at.is_none());
                schedule.marked_unschedulable_at = Some(timestamp);

                let schedule_id = schedule.id;

                self.unschedulable_schedules
                    .write()
                    .insert(schedule_id, schedule);
            }
        }

        Ok(())
    }

    async fn delete_schedules(&self, filters: ScheduleQueryFilters) -> eyre::Result<Vec<Uuid>> {
        let schedule_ids = self
            .query_schedule_ids_impl(filters)
            .into_iter()
            .collect::<IndexSet<_>>();

        // We don't care about orphaned jobs here.
        self.schedulable_schedules
            .write()
            .retain(|id, _| !schedule_ids.contains(id));
        self.unschedulable_schedules
            .write()
            .retain(|id, _| !schedule_ids.contains(id));

        Ok(schedule_ids.into_iter().collect())
    }
}

impl MemoryStorage {
    /// Returns all execution IDs for a job in creation order.
    fn executions_by_job_id(&self, job_id: Uuid) -> impl ExactSizeIterator<Item = Uuid> {
        let mut execution_ids = self
            .pending_executions
            .read()
            .values()
            .chain(self.ready_executions.read().values())
            .chain(self.assigned_executions.read().values())
            .chain(self.started_executions.read().values())
            .chain(self.succeeded_executions.read().values())
            .chain(self.failed_executions.read().values())
            .filter(move |execution| execution.job_id == job_id)
            .map(|execution| execution.id)
            .collect::<Vec<_>>();

        execution_ids.sort_unstable();

        execution_ids.into_iter()
    }

    /// Returns the last target execution time of a schedule.
    fn last_target_execution_time(&self, schedule_id: Uuid) -> Option<SystemTime> {
        self.schedulable_jobs
            .read()
            .values()
            .filter(|job| job.schedule_id == Some(schedule_id))
            .map(|job| job.target_execution_time)
            .max()
            .max(
                self.unschedulable_jobs
                    .read()
                    .values()
                    .filter(|job| job.schedule_id == Some(schedule_id))
                    .map(|job| job.target_execution_time)
                    .max(),
            )
    }
}

#[derive(Debug, Clone)]
struct Job {
    id: Uuid,
    schedule_id: Option<Uuid>,
    created_at: SystemTime,
    job_type_id: String,
    target_execution_time: SystemTime,
    retry_policy: JobRetryPolicy,
    timeout_policy: JobTimeoutPolicy,
    labels: IndexMap<String, String>,
    marked_unschedulable_at: Option<SystemTime>,
    cancelled_at: Option<SystemTime>,
    input_payload_json: String,
    metadata_json: Option<String>,
}

impl From<NewJob> for Job {
    fn from(job: NewJob) -> Self {
        Self {
            id: job.id,
            schedule_id: job.schedule_id,
            created_at: job.created_at,
            job_type_id: job.job_type_id,
            target_execution_time: job.target_execution_time,
            retry_policy: job.retry_policy,
            timeout_policy: job.timeout_policy,
            labels: job.labels,
            input_payload_json: job.input_payload_json,
            marked_unschedulable_at: None,
            cancelled_at: None,
            metadata_json: job.metadata_json,
        }
    }
}

#[derive(Debug, Clone)]
struct Execution {
    id: Uuid,
    job_id: Uuid,
    target_execution_time: SystemTime,
    executor_id: Option<Uuid>,
    created_at: SystemTime,
    ready_at: Option<SystemTime>,
    assigned_at: Option<SystemTime>,
    started_at: Option<SystemTime>,
    succeeded_at: Option<SystemTime>,
    failed_at: Option<SystemTime>,
    output_payload_json: Option<String>,
    failure_reason: Option<String>,
}

impl From<&Execution> for ExecutionDetails {
    fn from(value: &Execution) -> Self {
        Self {
            id: value.id,
            job_id: value.job_id,
            executor_id: value.executor_id,
            status: if value.succeeded_at.is_some() {
                JobExecutionStatus::Succeeded
            } else if value.failed_at.is_some() {
                JobExecutionStatus::Failed
            } else if value.started_at.is_some() {
                JobExecutionStatus::Running
            } else if value.assigned_at.is_some() {
                JobExecutionStatus::Assigned
            } else if value.ready_at.is_some() {
                JobExecutionStatus::Ready
            } else {
                JobExecutionStatus::Pending
            },
            created_at: value.created_at,
            ready_at: value.ready_at,
            assigned_at: value.assigned_at,
            started_at: value.started_at,
            succeeded_at: value.succeeded_at,
            failed_at: value.failed_at,
            output_payload_json: value.output_payload_json.clone(),
            failure_reason: value.failure_reason.clone(),
        }
    }
}

#[derive(Debug, Clone)]
struct Schedule {
    id: Uuid,
    created_at: SystemTime,
    job_type_id: Option<String>,
    labels: IndexMap<String, String>,
    marked_unschedulable_at: Option<SystemTime>,
    cancelled_at: Option<SystemTime>,
    job_timing_policy: ScheduleJobTimingPolicy,
    job_creation_policy: ScheduleJobCreationPolicy,
    time_range: Option<ScheduleTimeRange>,
    metadata_json: Option<String>,
}

impl From<NewSchedule> for Schedule {
    fn from(schedule: NewSchedule) -> Self {
        Self {
            id: schedule.id,
            created_at: schedule.created_at,
            job_type_id: match &schedule.job_creation_policy {
                ScheduleJobCreationPolicy::JobDefinition(schedule_new_job_definition) => {
                    Some(schedule_new_job_definition.job_type_id.clone())
                }
            },
            labels: schedule.labels,
            marked_unschedulable_at: None,
            cancelled_at: None,
            job_timing_policy: schedule.job_timing_policy,
            job_creation_policy: schedule.job_creation_policy,
            time_range: schedule.time_range,
            metadata_json: schedule.metadata_json,
        }
    }
}