rexecutor 0.1.0

A robust job processing library
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
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
//! Definition of the main trait [`Executor`] for defining enqueuable executions units (jobs).
//!
//! Jobs are defined by creating a struct/enum and implementing `Executor` for it.
//!
//! # Example
//!
//! You can define and enqueue a job as follows:
//!
//! ```
//! # use rexecutor::prelude::*;
//! # use chrono::{Utc, TimeDelta};
//! # use rexecutor::backend::memory::InMemoryBackend;
//! # use rexecutor::assert_enqueued;
//! # let backend = InMemoryBackend::new().paused();
//! # Rexecutor::new(backend).set_global_backend().unwrap();
//! struct EmailJob;
//!
//! #[async_trait::async_trait]
//! impl Executor for EmailJob {
//!     type Data = String;
//!     type Metadata = String;
//!     const NAME: &'static str = "email_job";
//!     const MAX_ATTEMPTS: u16 = 2;
//!     async fn execute(job: Job<Self::Data, Self::Metadata>) -> ExecutionResult {
//!         println!("{} running, with args: {}", Self::NAME, job.data);
//!         /// Do something important with an email
//!         ExecutionResult::Done
//!     }
//! }
//!
//! # tokio::runtime::Builder::new_current_thread().build().unwrap().block_on(async {
//! let _ = EmailJob::builder()
//!     .with_data("bob.shuruncle@example.com".to_owned())
//!     .schedule_in(TimeDelta::hours(3))
//!     .enqueue()
//!     .await;
//!
//! assert_enqueued!(
//!     with_data: "bob.shuruncle@example.com".to_owned(),
//!     scheduled_after: Utc::now() + TimeDelta::minutes(170),
//!     scheduled_before: Utc::now() + TimeDelta::minutes(190),
//!     for_executor: EmailJob
//! );
//! # });
//! ```
//!
//! # Unique jobs
//!
//! It is possible to ensure uniqueness of jobs based on certain criteria. This can be defined as
//! part of the implementation of `Executor` via [`Executor::UNIQUENESS_CRITERIA`] or when
//! inserting the job via [`JobBuilder::unique`].
//!
//! For example to ensure that only one unique job is ran every five minutes it is possible to use
//! the following uniqueness criteria.
//!
//! ```
//! # use rexecutor::prelude::*;
//! # use chrono::{Utc, TimeDelta};
//! # use rexecutor::backend::memory::InMemoryBackend;
//! # use rexecutor::assert_enqueued;
//! # let backend = InMemoryBackend::new().paused();
//! # Rexecutor::new(backend).set_global_backend().unwrap();
//! struct UniqueJob;
//!
//! #[async_trait::async_trait]
//! impl Executor for UniqueJob {
//!     type Data = ();
//!     type Metadata = ();
//!     const NAME: &'static str = "unique_job";
//!     const MAX_ATTEMPTS: u16 = 1;
//!     const UNIQUENESS_CRITERIA: Option<UniquenessCriteria<'static>> = Some(
//!         UniquenessCriteria::by_executor()
//!             .and_within(TimeDelta::seconds(300))
//!             .on_conflict(Replace::priority().for_statuses(&JobStatus::ALL)),
//!     );
//!     async fn execute(job: Job<Self::Data, Self::Metadata>) -> ExecutionResult {
//!         println!("{} running, with args: {:?}", Self::NAME, job.data);
//!         // Do something important
//!         ExecutionResult::Done
//!     }
//! }
//!
//! # tokio::runtime::Builder::new_current_thread().build().unwrap().block_on(async {
//! let _ = UniqueJob::builder().enqueue().await;
//! let _ = UniqueJob::builder().enqueue().await;
//!
//! // Only one of jobs was enqueued
//! assert_enqueued!(
//!     1 job,
//!     scheduled_before: Utc::now(),
//!     for_executor: UniqueJob
//! );
//! # });
//! ```
//!
//! Additionally it is possible to specify what action should be taken when there is a conflicting
//! job. In the example above the priority is override. For more details of how to use uniqueness
//! see [`UniquenessCriteria`].
//!
//!
//! # Overriding [`Executor`] default values
//!
//! When defining an [`Executor`] you specify the maximum number of attempts via
//! [`Executor::MAX_ATTEMPTS`]. However, when inserting a job it is possible to override this value
//! by calling [`JobBuilder::with_max_attempts`] (if not called the max attempts will be equal to
//! [`Executor::MAX_ATTEMPTS`].
//!
//! Similarly, the executor can define a job uniqueness criteria via
//! [`Executor::UNIQUENESS_CRITERIA`]. However, using [`JobBuilder::unique`] it is possible to
//! override this value for a specific job.
use async_trait::async_trait;
use chrono::TimeDelta;
use serde::{de::DeserializeOwned, Serialize};
use std::{error::Error, fmt::Display};

use crate::{
    backend::{Backend, Query},
    backoff::{BackoffStrategy, Exponential, Jitter, Strategy},
    global_backend::GlobalBackend,
    job::{builder::JobBuilder, query::Where, uniqueness_criteria::UniquenessCriteria, Job, JobId},
    RexecutorError,
};
type Result<T> = std::result::Result<T, RexecutorError>;

/// The default backoff strategy for exucutor jobs:
///  - exponential backoff with initial backoff of 4 seconds, and
///  - a max backoff of seven days,
///  - with a 10% jitter margin.
const DEFAULT_BACKOFF_STRATEGY: BackoffStrategy<Exponential> =
    BackoffStrategy::exponential(TimeDelta::seconds(4))
        .with_max(TimeDelta::days(7))
        .with_jitter(Jitter::Relative(0.1));

/// An enqueuable execution unit.
///
/// Jobs are defined by creating a struct/enum and implementing `Executor` for it.
///
/// # Example
///
/// You can define and enqueue a job as follows:
///
/// ```
/// # use rexecutor::prelude::*;
/// # use chrono::{Utc, TimeDelta};
/// # use rexecutor::backend::memory::InMemoryBackend;
/// # use rexecutor::assert_enqueued;
/// # let backend = InMemoryBackend::new().paused();
/// # Rexecutor::new(backend).set_global_backend().unwrap();
/// struct EmailJob;
///
/// #[async_trait::async_trait]
/// impl Executor for EmailJob {
///     type Data = String;
///     type Metadata = String;
///     const NAME: &'static str = "email_job";
///     const MAX_ATTEMPTS: u16 = 2;
///     async fn execute(job: Job<Self::Data, Self::Metadata>) -> ExecutionResult {
///         println!("{} running, with args: {}", Self::NAME, job.data);
///         /// Do something important with an email
///         ExecutionResult::Done
///     }
/// }
///
/// # tokio::runtime::Builder::new_current_thread().build().unwrap().block_on(async {
/// let _ = EmailJob::builder()
///     .with_data("bob.shuruncle@example.com".to_owned())
///     .schedule_in(TimeDelta::hours(3))
///     .enqueue()
///     .await;
///
/// assert_enqueued!(
///     with_data: "bob.shuruncle@example.com".to_owned(),
///     scheduled_after: Utc::now() + TimeDelta::minutes(170),
///     scheduled_before: Utc::now() + TimeDelta::minutes(190),
///     for_executor: EmailJob
/// );
/// # });
/// ```
///
/// # Unique jobs
///
/// It is possible to ensure uniqueness of jobs based on certain criteria. This can be defined as
/// part of the implementation of `Executor` via [`Executor::UNIQUENESS_CRITERIA`] or when
/// inserting the job via [`JobBuilder::unique`].
///
/// For example to ensure that only one unique job is ran every five minutes it is possible to use
/// the following uniqueness criteria.
///
/// ```
/// # use rexecutor::prelude::*;
/// # use chrono::{Utc, TimeDelta};
/// # use rexecutor::backend::memory::InMemoryBackend;
/// # use rexecutor::assert_enqueued;
/// # let backend = InMemoryBackend::new().paused();
/// # Rexecutor::new(backend).set_global_backend().unwrap();
/// struct UniqueJob;
///
/// #[async_trait::async_trait]
/// impl Executor for UniqueJob {
///     type Data = ();
///     type Metadata = ();
///     const NAME: &'static str = "unique_job";
///     const MAX_ATTEMPTS: u16 = 1;
///     const UNIQUENESS_CRITERIA: Option<UniquenessCriteria<'static>> = Some(
///         UniquenessCriteria::by_executor()
///             .and_within(TimeDelta::seconds(300))
///             .on_conflict(Replace::priority().for_statuses(&JobStatus::ALL)),
///     );
///     async fn execute(job: Job<Self::Data, Self::Metadata>) -> ExecutionResult {
///         println!("{} running, with args: {:?}", Self::NAME, job.data);
///         // Do something important
///         ExecutionResult::Done
///     }
/// }
///
/// # tokio::runtime::Builder::new_current_thread().build().unwrap().block_on(async {
/// let _ = UniqueJob::builder().enqueue().await;
/// let _ = UniqueJob::builder().enqueue().await;
///
/// // Only one of jobs was enqueued
/// assert_enqueued!(
///     1 job,
///     scheduled_before: Utc::now(),
///     for_executor: UniqueJob
/// );
/// # });
/// ```
///
/// Additionally it is possible to specify what action should be taken when there is a conflicting
/// job. In the example above the priority is override. For more details of how to use uniqueness
/// see [`UniquenessCriteria`].
///
///
/// # Overriding [`Executor`] default values
///
/// When defining an [`Executor`] you specify the maximum number of attempts via
/// [`Executor::MAX_ATTEMPTS`]. However, when inserting a job it is possible to override this value
/// by calling [`JobBuilder::with_max_attempts`] (if not called the max attempts will be equal to
/// [`Executor::MAX_ATTEMPTS`].
///
/// Similarly, the executor can define a job uniqueness criteria via
/// [`Executor::UNIQUENESS_CRITERIA`]. However, using [`JobBuilder::unique`] it is possible to
/// override this value for a specific job.
#[async_trait]
pub trait Executor {
    /// The type representing the executors arguments/data.
    ///
    /// If this is not needed it can be set to unit `()`.
    type Data: Serialize + DeserializeOwned;

    /// The type for storing the executors metadata this is always optional.
    ///
    /// If this is not needed it can be set to unit `()`.
    type Metadata: Serialize + DeserializeOwned;

    /// The name of the executor.
    ///
    /// This is used to associate the jobs stored in the backend with this particular executor.
    ///
    /// This should be set to a unique value for the backend to ensure no clashes with other
    /// executors running on the same backend.
    ///
    /// The motivation for using a static string here is to enable developers to rename their rust
    /// types for their executor without breaking the integration with the backend.
    const NAME: &'static str;

    /// The maximum number of attempts to try this job before it is discarded.
    ///
    /// When enqueuing any given job this can be overridden via [`JobBuilder::with_max_attempts`].
    const MAX_ATTEMPTS: u16 = 5;

    /// The maximum number of concurrent jobs to be running. If set to [`None`] there will be no
    /// concurrency limit and an arbitrary number can be ran simultaneously.
    const MAX_CONCURRENCY: Option<usize> = None;

    /// This flag should be set to true if the job is computationally expensive.
    ///
    /// This is to prevent a computationally expensive job locking up the asynchronous runtime.
    ///
    /// Under the covers this results in the job executor being ran via
    /// [`tokio::task::spawn_blocking`]. See the docs for more details about blocking futures.
    const BLOCKING: bool = false;

    /// This can be used to ensure that only unique jobs matching the [`UniquenessCriteria`] are
    /// inserted.
    ///
    /// If there is already a job inserted matching the given constraints there is the option to
    /// either update the current job or do nothing. See the docs of [`UniquenessCriteria`] for
    /// more details.
    ///
    /// It is possible to override when inserting a specific job via [`JobBuilder::unique`].
    const UNIQUENESS_CRITERIA: Option<UniquenessCriteria<'static>> = None;

    /// The work this a job should do when executing.
    async fn execute(job: Job<Self::Data, Self::Metadata>) -> ExecutionResult;

    /// Defines the backoff after a failed job.
    ///
    /// The default backoff strategy is
    ///  - exponential backoff with initial backoff of 4 seconds, and
    ///  - a max backoff of seven days,
    ///  - with a 10% jitter margin.
    ///
    /// For some standard backoff strategies see the [`crate::backoff`].
    ///
    /// **Note**: this method is called preemptively before attempting execution.
    fn backoff(job: &Job<Self::Data, Self::Metadata>) -> TimeDelta {
        DEFAULT_BACKOFF_STRATEGY.backoff(job.attempt)
    }

    /// The timeout when executing a specific job.
    ///
    /// If this returns [`None`] (default behaviour) then the job will be ran without a timeout.
    fn timeout(_job: &Job<Self::Data, Self::Metadata>) -> Option<std::time::Duration> {
        None
    }

    /// The builder for inserting jobs.
    ///
    /// For details of the API see [`JobBuilder`].
    fn builder<'a>() -> JobBuilder<'a, Self>
    where
        Self: Sized,
        Self::Data: Serialize + DeserializeOwned,
        Self::Metadata: Serialize + DeserializeOwned,
    {
        Default::default()
    }

    // Should the job manipulation API always return the job?

    /// Cancel a job with the given reason.
    ///
    /// To make use this API and the global backend, [`crate::Rexecutor::set_global_backend`]
    /// should be called. If this hasn't been called, then a [`RexecutorError::GlobalBackend`]
    /// will be returned.
    ///
    /// # Example
    ///
    /// ```
    /// # use rexecutor::prelude::*;
    /// # let job_id = 0.into();
    /// # use chrono::TimeDelta;
    /// # pub(crate) struct SimpleExecutor;
    /// #
    /// # #[async_trait::async_trait]
    /// # impl Executor for SimpleExecutor {
    /// #     type Data = String;
    /// #     type Metadata = String;
    /// #     const NAME: &'static str = "simple_executor";
    /// #     const MAX_ATTEMPTS: u16 = 2;
    /// #     async fn execute(_job: Job<Self::Data, Self::Metadata>) -> ExecutionResult {
    /// #         ExecutionResult::Done
    /// #     }
    /// # }
    /// # tokio::runtime::Builder::new_current_thread().build().unwrap().block_on(async {
    /// let reason = Box::new("No longer needed");
    /// let result = SimpleExecutor::cancel_job(job_id, reason).await;
    /// # });
    /// ```
    async fn cancel_job(
        job_id: JobId,
        cancellation_reason: Box<dyn CancellationReason>,
    ) -> Result<()> {
        Self::cancel_job_on_backend(job_id, cancellation_reason, GlobalBackend::as_ref()?).await
    }

    /// Cancel a job with the given reason on the provided backend.
    async fn cancel_job_on_backend<B>(
        job_id: JobId,
        cancellation_reason: Box<dyn CancellationReason>,
        backend: &B,
    ) -> Result<()>
    where
        B: Backend + ?Sized + Sync,
    {
        backend
            .mark_job_cancelled(job_id, cancellation_reason.into())
            .await?;
        Ok(())
    }

    /// Rerun a completed or discarded job.
    ///
    /// To make use this API and the global backend, [`crate::Rexecutor::set_global_backend`]
    /// should be called. If this hasn't been called, then a [`RexecutorError::GlobalBackend`]
    /// will be returned.
    ///
    /// # Example
    ///
    /// ```
    /// # use rexecutor::prelude::*;
    /// # let job_id = 0.into();
    /// # use chrono::TimeDelta;
    /// # pub(crate) struct SimpleExecutor;
    /// #
    /// # #[async_trait::async_trait]
    /// # impl Executor for SimpleExecutor {
    /// #     type Data = String;
    /// #     type Metadata = String;
    /// #     const NAME: &'static str = "simple_executor";
    /// #     const MAX_ATTEMPTS: u16 = 2;
    /// #     async fn execute(_job: Job<Self::Data, Self::Metadata>) -> ExecutionResult {
    /// #         ExecutionResult::Done
    /// #     }
    /// # }
    /// # tokio::runtime::Builder::new_current_thread().build().unwrap().block_on(async {
    /// let result = SimpleExecutor::rerun_job(job_id).await;
    /// # });
    /// ```
    async fn rerun_job(job_id: JobId) -> Result<()> {
        Self::rerun_job_on_backend(job_id, GlobalBackend::as_ref()?).await
    }

    /// Rerun a completed or discarded job on the provided backend.
    async fn rerun_job_on_backend<B>(job_id: JobId, backend: &B) -> Result<()>
    where
        B: Backend + ?Sized + Sync,
    {
        backend.rerun_job(job_id).await?;
        Ok(())
    }

    /// Query the jobs for this executor using the provided query.
    ///
    /// For details of the query API see [`Where`].
    ///
    /// To make use this API and the global backend, [`crate::Rexecutor::set_global_backend`]
    /// should be called. If this hasn't been called, then a [`RexecutorError::GlobalBackend`]
    /// will be returned.
    ///
    /// # Example
    ///
    /// To query all completed jobs for the `SimpleExecutor`:
    ///
    /// ```
    /// # use rexecutor::prelude::*;
    /// # use chrono::TimeDelta;
    /// # pub(crate) struct SimpleExecutor;
    /// #
    /// # #[async_trait::async_trait]
    /// # impl Executor for SimpleExecutor {
    /// #     type Data = String;
    /// #     type Metadata = String;
    /// #     const NAME: &'static str = "simple_executor";
    /// #     const MAX_ATTEMPTS: u16 = 2;
    /// #     async fn execute(_job: Job<Self::Data, Self::Metadata>) -> ExecutionResult {
    /// #         ExecutionResult::Done
    /// #     }
    /// # }
    /// # tokio::runtime::Builder::new_current_thread().build().unwrap().block_on(async {
    /// let result = SimpleExecutor::query_jobs(Where::status_equals(JobStatus::Complete)).await;
    /// # });
    /// ```
    async fn query_jobs<'a>(
        query: Where<'a, Self::Data, Self::Metadata>,
    ) -> Result<Vec<Job<Self::Data, Self::Metadata>>>
    where
        Self::Data: 'static + Send + Serialize + DeserializeOwned + Sync,
        Self::Metadata: 'static + Send + Serialize + DeserializeOwned + Sync,
    {
        Self::query_jobs_on_backend(query, GlobalBackend::as_ref()?).await
    }

    /// Query the jobs for this executor using the provided query on the provided backend.
    ///
    /// For details of the query API see [`Where`].
    async fn query_jobs_on_backend<'a, B>(
        query: Where<'a, Self::Data, Self::Metadata>,
        backend: &B,
    ) -> Result<Vec<Job<Self::Data, Self::Metadata>>>
    where
        Self::Data: 'static + Send + Serialize + DeserializeOwned + Sync,
        Self::Metadata: 'static + Send + Serialize + DeserializeOwned + Sync,
        B: Backend + ?Sized + Sync,
    {
        Ok(backend
            .query(Query::try_from(query.0)?.for_executor(Self::NAME))
            .await?
            .into_iter()
            .map(TryFrom::try_from)
            .collect::<std::result::Result<Vec<_>, _>>()?)
    }

    /// Update the given job
    ///
    /// To make use this API and the global backend, [`crate::Rexecutor::set_global_backend`]
    /// should be called. If this hasn't been called, then a [`RexecutorError::GlobalBackend`]
    /// will be returned.
    ///
    /// # Example
    ///
    /// To query all completed jobs for the `SimpleExecutor`:
    ///
    /// ```
    /// # use rexecutor::prelude::*;
    /// # use chrono::TimeDelta;
    /// # pub(crate) struct SimpleExecutor;
    /// #
    /// # #[async_trait::async_trait]
    /// # impl Executor for SimpleExecutor {
    /// #     type Data = String;
    /// #     type Metadata = String;
    /// #     const NAME: &'static str = "simple_executor";
    /// #     const MAX_ATTEMPTS: u16 = 2;
    /// #     async fn execute(mut job: Job<Self::Data, Self::Metadata>) -> ExecutionResult {
    /// job.max_attempts = 5;
    /// let result = SimpleExecutor::update_job(job).await;
    /// #         ExecutionResult::Done
    /// #     }
    /// # }
    /// ```
    async fn update_job(job: Job<Self::Data, Self::Metadata>) -> Result<()>
    where
        Self::Data: 'static + Send + Serialize + Sync,
        Self::Metadata: 'static + Send + Serialize + Sync,
    {
        Self::update_job_on_backend(job, GlobalBackend::as_ref()?).await
    }

    /// Update the given job
    async fn update_job_on_backend<B>(
        job: Job<Self::Data, Self::Metadata>,
        backend: &B,
    ) -> Result<()>
    where
        B: Backend + ?Sized + Sync,
        Self::Data: 'static + Serialize + Send + Sync,
        Self::Metadata: 'static + Serialize + Send + Sync,
    {
        let job = job.try_into()?;
        backend.update_job(job).await?;
        Ok(())
    }
}

/// An identifier for an executor.
#[derive(Debug, Eq, PartialEq, Clone, Hash)]
pub struct ExecutorIdentifier(&'static str);

impl From<&'static str> for ExecutorIdentifier {
    fn from(value: &'static str) -> Self {
        Self(value)
    }
}

impl ExecutorIdentifier {
    /// Returns a view of this [`ExecutorIdentifier`] as a string slice.
    pub fn as_str(&self) -> &'static str {
        self.0
    }
}

impl std::ops::Deref for ExecutorIdentifier {
    type Target = str;

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

/// The return value of a job execution.
///
/// The value returns determines what will happen to the completed job.
///
/// If the jobs completes successfully [`ExecutionResult::Done`] should be returned. This will
/// result in the job status being set to [`crate::job::JobStatus::Complete`].
///
/// If the job resulted in a error, then [`ExecutionResult::Error`] can be returned. This will set
/// the job status equal to [`crate::job::JobStatus::Retryable`] if the job's `attempt` value is
/// less than its `max_attempts` value. The retry will be scheduled after the `TimeDelta` returned
/// from the [`Executor::backoff`].
///
/// If the job should be attempted later then it is possible to snooze the job by returning
/// [`ExecutionResult::Snooze`] providing the delay to wait until trying the job again. Unlike
/// returning an error this does not increment the job's attempt field.
///
/// If the job should not attempt to be retried and did not complete successfully then
/// [`ExecutionResult::Cancel`] can be returned passing in the cancellation reason.
pub enum ExecutionResult {
    /// The job completed successfully and should be marked as completed.
    Done,
    /// The job did not complete successfully and should not be retried.
    Cancel {
        /// The reason the job should be cancelled. This will be stored in the `error` field on the
        /// job.
        reason: Box<dyn CancellationReason>,
    },
    /// The job should be retried after the `delay`.
    ///
    /// Note unlike returning an error returning `Snooze` will not increment the job's attempt
    /// number.
    Snooze {
        /// The delay to wait until attempting this job again.
        delay: TimeDelta,
    },
    /// The job's execution resulted in an error and the job should either be retired if
    /// `job.attempt < job.max_attempts` or be discarded if `job.attempt == job.max_attempts`.
    Error {
        /// The error that occurred when attempting to execute this job.
        error: Box<dyn ExecutionError>,
    },
}

impl<T> From<T> for ExecutionResult
where
    T: ExecutionError + 'static,
{
    fn from(value: T) -> Self {
        Self::Error {
            error: Box::new(value),
        }
    }
}

/// Errors returned from jobs as part of [`ExecutionResult::Error`] should implement this trait.
/// The error type can be later used for identifying particular failures in the job's errors array.
pub trait ExecutionError: Error + Send {
    /// An identifier for the type of error that occurred while execution the job.
    fn error_type(&self) -> &'static str;
}

/// Anything that can be used as a cancellation recons. Generally this should not need to be
/// implemented and instead users of the library should be able to rely on the blanket
/// implementations.
pub trait CancellationReason: Display + Send {}

impl<T> CancellationReason for T where T: Display + Send {}

#[cfg(test)]
pub(crate) mod test {
    use async_trait::async_trait;
    use serde::{Deserialize, Serialize};

    use crate::{
        backend::{BackendError, MockBackend},
        job::JobStatus,
    };

    use super::*;

    pub(crate) struct SimpleExecutor;

    #[async_trait]
    impl Executor for SimpleExecutor {
        type Data = String;
        type Metadata = String;
        const NAME: &'static str = "simple_executor";
        const MAX_ATTEMPTS: u16 = 2;
        async fn execute(_job: Job<Self::Data, Self::Metadata>) -> ExecutionResult {
            ExecutionResult::Done
        }
    }

    pub(crate) struct MockReturnExecutor;

    #[derive(Debug, Clone, Serialize, Deserialize, Hash)]
    pub(crate) enum MockExecutionResult {
        Done,
        Panic,
        Timeout,
        Cancelled { reason: String },
        Snooze { delay: std::time::Duration },
        Error { error: MockError },
    }

    #[derive(Debug, Clone, Serialize, Deserialize, Hash)]
    pub(crate) struct MockError(pub String);
    impl std::fmt::Display for MockError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "{}", self.0)
        }
    }
    impl Error for MockError {}
    impl ExecutionError for MockError {
        fn error_type(&self) -> &'static str {
            "custom"
        }
    }

    #[async_trait]
    impl Executor for MockReturnExecutor {
        type Data = MockExecutionResult;
        type Metadata = ();
        const NAME: &'static str = "basic_executor";
        const MAX_ATTEMPTS: u16 = 2;
        async fn execute(job: Job<Self::Data, Self::Metadata>) -> ExecutionResult {
            match job.data {
                MockExecutionResult::Panic => panic!("job paniced"),
                MockExecutionResult::Timeout => {
                    tokio::time::sleep(std::time::Duration::from_millis(10)).await;
                    ExecutionResult::Done
                }
                MockExecutionResult::Done => ExecutionResult::Done,
                MockExecutionResult::Cancelled { reason } => ExecutionResult::Cancel {
                    reason: Box::new(reason),
                },
                MockExecutionResult::Snooze { delay } => ExecutionResult::Snooze {
                    delay: TimeDelta::from_std(delay).unwrap(),
                },
                MockExecutionResult::Error { error } => ExecutionResult::Error {
                    error: Box::new(error),
                },
            }
        }

        fn timeout(job: &Job<Self::Data, Self::Metadata>) -> Option<std::time::Duration> {
            if matches!(job.data, MockExecutionResult::Timeout) {
                Some(std::time::Duration::from_millis(1))
            } else {
                None
            }
        }
    }

    #[tokio::test]
    async fn cancel_job_error() {
        let job_id = 0.into();
        let mut backend = MockBackend::default();
        backend
            .expect_mark_job_cancelled()
            .returning(move |_, _| Err(BackendError::BadState));

        let reason = Box::new("No longer needed");
        let result = SimpleExecutor::cancel_job_on_backend(job_id, reason, &backend).await;

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn cancel_job_success() {
        let job_id = 0.into();
        let mut backend = MockBackend::default();
        backend
            .expect_mark_job_cancelled()
            .returning(move |_, _| Ok(()));

        let reason = Box::new("No longer needed");
        let result = SimpleExecutor::cancel_job_on_backend(job_id, reason, &backend).await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn rerun_job_error() {
        let job_id = 0.into();
        let mut backend = MockBackend::default();
        backend
            .expect_rerun_job()
            .returning(move |_| Err(BackendError::BadState));

        let result = SimpleExecutor::rerun_job_on_backend(job_id, &backend).await;

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn rerun_job_success() {
        let job_id = 0.into();
        let mut backend = MockBackend::default();
        backend.expect_rerun_job().returning(move |_| Ok(()));

        let result = SimpleExecutor::rerun_job_on_backend(job_id, &backend).await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn query_jobs_error() {
        let mut backend = MockBackend::default();
        backend
            .expect_query()
            .returning(move |_| Err(BackendError::BadState));
        let query = Where::status_equals(JobStatus::Cancelled);

        let result = SimpleExecutor::query_jobs_on_backend(query, &backend).await;

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn query_jobs_success_empty() {
        let mut backend = MockBackend::default();
        backend.expect_query().returning(move |_| Ok(vec![]));
        let query = Where::status_equals(JobStatus::Cancelled);

        let result = SimpleExecutor::query_jobs_on_backend(query, &backend).await;

        assert!(result.is_ok());
        assert!(result.unwrap().is_empty());
    }

    #[tokio::test]
    async fn query_jobs_success() {
        let mut backend = MockBackend::default();
        backend.expect_query().returning(move |_| {
            Ok(vec![
                crate::backend::Job::mock_job::<SimpleExecutor>().with_data("Data")
            ])
        });
        let query = Where::status_equals(JobStatus::Cancelled);

        let result = SimpleExecutor::query_jobs_on_backend(query, &backend).await;

        assert!(result.is_ok());
        assert_eq!(result.unwrap().len(), 1);
    }

    #[tokio::test]
    async fn update_job_error() {
        let job = crate::backend::Job::mock_job::<SimpleExecutor>()
            .with_data("Data")
            .try_into()
            .unwrap();
        let mut backend = MockBackend::default();
        backend
            .expect_update_job()
            .returning(move |_| Err(BackendError::BadState));

        let result = SimpleExecutor::update_job_on_backend(job, &backend).await;

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn update_job_success() {
        let job = crate::backend::Job::mock_job::<SimpleExecutor>()
            .with_data("Data")
            .try_into()
            .unwrap();
        let mut backend = MockBackend::default();
        backend.expect_update_job().returning(move |_| Ok(()));

        let result = SimpleExecutor::update_job_on_backend(job, &backend).await;

        assert!(result.is_ok());
    }

    #[test]
    fn default_timeout_is_none() {
        let job = crate::backend::Job::mock_job::<SimpleExecutor>()
            .with_data("Data")
            .try_into()
            .unwrap();
        assert!(SimpleExecutor::timeout(&job).is_none());
    }
}