aj_core 0.8.0

Background Job Library for Rust
Documentation
#![allow(clippy::field_reassign_with_default)]

#[macro_use]
extern crate log;
#[macro_use]
extern crate serde_with;

pub mod aj;
pub mod backend;
pub mod error;
pub mod job;
pub mod plugin;
pub mod queue;
pub mod util;

pub use aj::*;
pub use backend::*;
pub use error::*;
pub use job::*;
pub use plugin::*;
pub use queue::*;
pub use util::*;

// External libs.
pub use async_trait;
pub use chrono;
pub use cron;
pub use serde;

use serde::{de::DeserializeOwned, Serialize};

impl<M> Job<M>
where
    M: BackgroundJob
        + Executable
        + Unpin
        + Send
        + Sync
        + Clone
        + Serialize
        + DeserializeOwned
        + 'static,
{
    /// It will wait to WorkQueue received and insert job into backend
    pub async fn run(self) -> Result<String, Error> {
        AJ::add_job(self, M::queue_name()).await
    }

    /// It will just send message to WorkQueue and no gurantee job is inserted to backend
    pub fn just_run(self) {
        if let Some(aj_ref) = get_aj_address() {
            let job = self;
            let queue_name = M::queue_name().to_string();
            // Fire and forget - spawn a task to send the message
            tokio::spawn(async move {
                let _ = aj_ref.tell(JustRunJob { job, queue_name }).await;
            });
        }
    }
}