use std::fmt;
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;
use super::queue::Queue;
use super::run::Run;
use super::worker::Worker;
use crate::Result;
use crate::jobs::{self, Job, Outcome};
use crate::pool::Pool;
#[must_use = "a schedule opens with work() or open()"]
pub struct ScheduleBuilder {
builder: jobs::ScheduleBuilder,
pool: Arc<Pool>,
}
impl fmt::Debug for ScheduleBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.builder, f)
}
}
impl ScheduleBuilder {
pub(super) fn new(builder: jobs::ScheduleBuilder, pool: Arc<Pool>) -> Self {
ScheduleBuilder { builder, pool }
}
fn with(self, option: impl FnOnce(jobs::ScheduleBuilder) -> jobs::ScheduleBuilder) -> Self {
ScheduleBuilder { builder: option(self.builder), pool: self.pool }
}
pub fn every(self, span: Duration) -> Self {
self.with(|builder| builder.every(span))
}
pub fn cron(self, expr: &str, zone: &str) -> Self {
self.with(|builder| builder.cron(expr, zone))
}
pub fn attempts(self, attempts: u32) -> Self {
self.with(|builder| builder.attempts(attempts))
}
pub fn backoff(self, initial: Duration, max: Duration) -> Self {
self.with(|builder| builder.backoff(initial, max))
}
pub fn timeout(self, timeout: Duration) -> Self {
self.with(|builder| builder.timeout(timeout))
}
pub fn open(self) -> Result<Schedule> {
Ok(Schedule::of(self.builder.open()?, self.pool))
}
pub fn work<F, W, R, E>(self, handler: F) -> Result<Worker>
where
F: Fn(Run) -> W + Send + Sync + 'static,
W: Future<Output = std::result::Result<R, E>> + Send + 'static,
R: Into<Outcome> + 'static,
E: fmt::Display + 'static,
{
let schedule = self.open()?;
jobs::refuse_on_a_copy(&schedule.queue.queue)?;
Ok(schedule.work(handler))
}
}
pub struct Schedule {
schedule: jobs::Schedule,
queue: Queue<()>,
}
impl From<jobs::Schedule> for Schedule {
fn from(schedule: jobs::Schedule) -> Schedule {
let pool = super::pool_of(schedule.queue());
Schedule::of(schedule, pool)
}
}
impl Schedule {
fn of(schedule: jobs::Schedule, pool: Arc<Pool>) -> Schedule {
let queue = Queue { queue: schedule.queue().clone(), pool };
Schedule { schedule, queue }
}
pub fn blocking(&self) -> &jobs::Schedule {
&self.schedule
}
fn name(&self) -> &str {
&self.queue.queue.state.name
}
pub fn get(&self) -> impl Future<Output = Result<Option<Job<()>>>> + Send + 'static {
self.queue.get(self.name())
}
pub fn work<F, W, R, E>(&self, handler: F) -> Worker
where
F: Fn(Run) -> W + Send + Sync + 'static,
W: Future<Output = std::result::Result<R, E>> + Send + 'static,
R: Into<Outcome> + 'static,
E: fmt::Display + 'static,
{
match jobs::refuse_on_a_copy(&self.queue.queue) {
Ok(()) => self.queue.work(move |(): (), run: Run| handler(run)),
Err(refused) => Worker::refused(&self.queue, refused),
}
}
pub fn run_due<F, W, R, E>(&self, handler: F) -> impl Future<Output = Result<usize>> + Send + 'static
where
F: Fn(Run) -> W + Send + Sync + 'static,
W: Future<Output = std::result::Result<R, E>> + Send + 'static,
R: Into<Outcome> + 'static,
E: fmt::Display + 'static,
{
self.queue.run_due(move |(): (), run: Run| handler(run))
}
pub fn cancel(&self) -> impl Future<Output = Result<bool>> + Send + 'static {
self.queue.cancel(self.name())
}
}
impl fmt::Debug for Schedule {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.schedule, f)
}
}