Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retry jobs to the end of the queue #497

Closed
IgnisDa opened this issue Jan 11, 2025 · 7 comments
Closed

Add retry jobs to the end of the queue #497

IgnisDa opened this issue Jan 11, 2025 · 7 comments

Comments

@IgnisDa
Copy link

IgnisDa commented Jan 11, 2025

Retries are done instantly when using the RetryLayer. I do not see any options in RetryPolicy (https://docs.rs/apalis/latest/apalis/layers/retry/struct.RetryPolicy.html) so that the retry job goes to the end of the queue.

@geofmureithi
Copy link
Owner

Have you checked out #399 (comment)

@geofmureithi
Copy link
Owner

You can also implement your own policy:

impl<T, Res, Ctx> Policy<Req<T, Ctx>, Res, Err> for RetryPolicy
where
    T: Clone,
    Ctx: Clone,
{
    type Future = future::Ready<()>;

    fn retry(
        &mut self,
        req: &mut Req<T, Ctx>,
        result: &mut Result<Res, Err>,
    ) -> Option<Self::Future> {
        let attempt = &req.parts.attempt;
        match result {
            Ok(_) => {
                // Treat all `Response`s as success,
                // so don't retry...
                None
            }
            Err(_) if self.retries == 0 => None,
            Err(_) if (self.retries - attempt.current() > 0) => Some(future::ready(())),
            Err(_) => None,
        }
    }

    fn clone_request(&mut self, req: &Req<T, Ctx>) -> Option<Req<T, Ctx>> {
        let req = req.clone();
        req.parts.attempt.increment();
        Some(req)
    }
}

You can replace with a future that represents your logic:

Err(_) if (self.retries - attempt.current() > 0) => Some(tokio::sleep(duration)),

@IgnisDa
Copy link
Author

IgnisDa commented Jan 11, 2025 via email

@geofmureithi
Copy link
Owner

geofmureithi commented Jan 11, 2025 via email

@IgnisDa
Copy link
Author

IgnisDa commented Jan 11, 2025

Yes. Thats how the bullmq library works (https://docs.bullmq.io/guide/retrying-failing-jobs).

@geofmureithi
Copy link
Owner

geofmureithi commented Jan 11, 2025 via email

@IgnisDa
Copy link
Author

IgnisDa commented Jan 11, 2025

Closing in favour of #399. Thanks!

@IgnisDa IgnisDa closed this as completed Jan 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants