use std::sync::Arc;
use async_trait::async_trait;
use rok_queue::{Job, JobContext, JobResult, Queue, Runnable};
use serde::{Deserialize, Serialize};
use crate::Mailable;
pub(crate) type QueueRef = Arc<Queue>;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct SendMailJob {
pub to: String,
pub subject: String,
pub body: String,
pub html_body: Option<String>,
}
impl Job for SendMailJob {
fn job_type() -> &'static str
where
Self: Sized,
{
"rok_mail::SendMailJob"
}
fn queue() -> &'static str
where
Self: Sized,
{
"mail"
}
fn max_attempts() -> u32
where
Self: Sized,
{
3
}
}
#[async_trait]
impl Runnable for SendMailJob {
async fn run(&self, _ctx: &JobContext) -> JobResult {
use crate::mail::global_config;
let config = global_config().cloned().unwrap_or_default();
struct QueuedMailable {
subject: String,
body: String,
html_body: Option<String>,
}
impl Mailable for QueuedMailable {
fn subject(&self) -> &str {
&self.subject
}
fn body(&self) -> String {
self.body.clone()
}
fn html_body(&self) -> Option<String> {
self.html_body.clone()
}
}
let mailable = QueuedMailable {
subject: self.subject.clone(),
body: self.body.clone(),
html_body: self.html_body.clone(),
};
crate::Mail::send_with(mailable, &self.to, &config)
.await
.map_err(|e| rok_queue::JobError(e.to_string()))
}
}
pub fn register_mail_jobs(registry: &mut rok_queue::JobRegistry) {
registry.register::<SendMailJob>();
}