use laravel::queue::{Attempt, Queueable, job};
use serde_json::Value;
use crate::action::Reference;
use crate::arguments::Arguments;
use crate::registry;
pub const MAX_TRIES: u32 = 100;
#[derive(Queueable)]
pub struct ActionJob {
pub action: String,
pub state: Value,
pub arguments: Arguments,
pub actionable: Option<Reference>,
pub actor: Option<Reference>,
pub tries: u32,
pub logging: bool,
pub tracing: bool,
}
#[job(name = "laractions.action", tries = 100)]
impl ActionJob {
async fn handle(self, attempt: &Attempt) -> laravel::Result<()> {
let registration = registry::find(&self.action)?;
let tries = self.tries.clamp(1, MAX_TRIES);
match registration.run(&self).await {
Ok(()) => Ok(()),
Err(error) if attempt.number() >= tries => {
attempt.fail(error);
Ok(())
}
Err(error) => Err(error),
}
}
pub async fn failed(self, error: &laravel::Error) {
tracing::error!(
target: "laractions",
exception = ?error,
"[ActionJob: {}] Job failed: {error}",
self.action
);
}
}