Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to debounce ActiveJobs?

Matthias Orgler asked in Rails

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?

Reply

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.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 84,256+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.