New Discussion

Notifications

You’re not receiving notifications from this thread.

How to debounce ActiveJobs?

1
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?

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.

Join the discussion
Create an account Log in

Learning Ruby on Rails? Join our newsletter.

We won't send you spam. Unsubscribe at any time.