Skip to main content

How to debounce ActiveJobs?

Rails • Asked by Matthias Orgler

How can I debounce jobs in ActiveJob?

Why I need this:
Events in my Rails app can trigger the same job independently from multiple places. But I want this job to run only once – only running the last job scheduled. I want to cancel all earlier jobs to save resources. Here is an example:

Job 1: Re-render file x
Job 2: Re-render file x
Job 3: Re-render file x
=> cancel job 1+2, only execute job 3

What I found:
Found https://medium.com/amco/sidekiq-tricks-dont-do-the-same-work-twice-378d4ee4c2d8, which implements this for Sidekiq. But I can't figure out how to do it in ActiveJob directly.

Is there a way to debounce jobs in ActiveJob?


Possibly this with a supported backend queue? https://github.com/y-yagi/activejob-cancel

I think when you enqueue a job it gives you back an ID. You could probably save that ID to the model that's being processed and look for a match when the job starts to only allow that Job ID to do the processing.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 20,000+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.