laraindexnow 0.1.2

IndexNow for Laravel Rust: tell Bing, Yandex and other search engines a URL changed, the moment it changes.
Documentation
use laravel::container::Inject;
use laravel::queue::{Attempt, Queueable, job};
use laravel::time::ToSpan;

use crate::manager::IndexNow;
use crate::result::RetryableSubmission;

/// Drains the buffer. Exactly one exists per window, however many events arrive.
#[derive(Queueable)]
pub struct FlushBuffer;

#[job(name = "laraindexnow.flush_buffer")]
impl FlushBuffer {
    async fn handle(self, manager: Inject<IndexNow>) -> laravel::Result<()> {
        manager.release_marker().await?;
        manager.flush().await?;

        Ok(())
    }
}

/// Sends one batch of one host.
///
/// A `429` is released with the endpoint's own `Retry-After`. A `5xx` or a connection failure
/// fails the attempt and backs off at 1, 5 and 15 minutes. A rejected key or a malformed URL is
/// never retried, because retrying will not fix it. The last attempt fails with the reason the
/// submission actually failed for.
#[derive(Queueable)]
pub struct SubmitUrls {
    /// The host every URL belongs to.
    pub host: String,
    /// The URLs to send.
    pub urls: Vec<String>,
}

#[job(
    name = "laraindexnow.submit_urls",
    tries = 4,
    backoff = [1.minutes(), 5.minutes(), 15.minutes()],
)]
impl SubmitUrls {
    async fn handle(self, manager: Inject<IndexNow>, attempt: &Attempt) -> laravel::Result<()> {
        match manager.send_batch(&self.host, &self.urls).await {
            Ok(_) => Ok(()),
            Err(error) => {
                let Some(retryable) = error.downcast_ref::<RetryableSubmission>() else {
                    return Err(error);
                };

                if let Some(seconds) = retryable.retry_after
                    && attempt.number() < 4
                {
                    attempt.release((seconds as i64).seconds());

                    return Ok(());
                }

                Err(error)
            }
        }
    }
}