izta 0.1.2

Izta is a drop-in job queue for Rust
Documentation
use crate::cron::Cron;
use crate::job::Job;
use chrono::{DateTime, Duration, Utc};
use serde::Serialize;

/// Use `TaskReq` to build a new task before scheduling it.
///
/// # Example
/// ```rust
/// # #[macro_use] extern crate serde;
///
/// # #[derive(Serialize, Deserialize)]
/// # struct SomeJob {}
/// #
/// # impl Job for SomeJob {
/// #     type R = ();
/// #     type E = ();
/// #     const UUID: &'static str = "some uuid";
/// #     const MAX_ATTEMPTS: usize = 1;
/// #
/// #     fn run(&self) -> Result<Self::R, Self::E> {
/// #         unimplemented!()
/// #     }
/// # }
/// #
/// # impl SomeJob {
/// #     pub fn new() -> SomeJob {
/// #         SomeJob {}
/// #     }
/// # }
/// # use izta::job::Job;
/// use izta::task::task_req::TaskReq;
/// use chrono::{Duration, Utc};
/// use izta::cron::Cron;
///
/// // Describe a task that is run as soon as possible
/// let task1 = TaskReq::new(SomeJob::new());
///
/// // For a task that runs 5 minutes from now
/// let five_minutes_from_now = Utc::now() + Duration::minutes(5);
/// let task2 = TaskReq::new(SomeJob::new()).run_at(five_minutes_from_now);
///
/// // For a task that runs on the 15th of every month at 5:30 PM
/// let cron = Cron::new().day_of_month(15).hours(5).minutes(30);
/// let task3 = TaskReq::new(SomeJob::new()).schedule(&cron);
///
/// // For a task that runs every 30 seconds in a special queue
/// let seconds_30 = Duration::seconds(30);
/// let task4 = TaskReq::new(SomeJob::new()).run_every(seconds_30).put_in("busy_queue");
/// ```
#[derive(Clone)]
pub struct TaskReq {
    pub job_uuid: String,
    pub run_at: Option<DateTime<Utc>>,
    pub queue: Option<String>,
    pub max_attempts: usize,
    pub cron: Option<Cron>,
    pub interval: Option<Duration>,
    pub job: String,
}

impl TaskReq {
    /// Create a new `TaskReq` associated with provided `Job` (this does not schedule the task; see
    /// [`Runner::add_task`] for that)
    ///
    /// [`Runner::add_task`]: runner/struct.Runner.html#method_add_task
    pub fn new<J>(job: J) -> TaskReq
    where
        J: Job + Serialize,
    {
        TaskReq {
            job_uuid: J::UUID.to_string(),
            run_at: None,
            queue: None,
            max_attempts: J::MAX_ATTEMPTS,
            cron: None,
            interval: None,
            job: serde_json::to_string(&job).unwrap(),
        }
    }

    /// Returns a new `TaskReq` for a task to run at `unix_time` (milliseconds since epoch).
    pub fn run_at(self, t: DateTime<Utc>) -> TaskReq {
        let mut tr = self.clone();
        tr.run_at = Some(t);
        tr
    }

    /// Returns a new `TaskReq` for a task to run every `interval` milliseconds
    pub fn run_every(self, interval: Duration) -> TaskReq {
        let mut tr = self.clone();
        tr.interval = Some(interval);
        tr
    }

    /// Returns a new `TaskReq` for a task belonging to the `queue` queue.
    pub fn put_in(self, queue: &str) -> TaskReq {
        let mut tr = self.clone();
        tr.queue = Some(queue.to_string());
        tr
    }

    /// Returns a new `TaskReq` for a task with the provided `Cron` schedule.
    pub fn schedule(self, cron: &Cron) -> TaskReq {
        let mut tr = self.clone();
        tr.cron = Some(cron.clone());
        tr
    }
}