7

In our application we expose a callback route for an external service to hit. When we receive the callback, we publish an update to client-side subscribers using Eventsource on the client/browser-side and cramp on the server side. Sometimes, however, we get bombarded with callback requests from this external service which results in us publishing a crap ton of updates to the client. Is there a way on the Rails-side, similar to a javascript debounce function, that would wait a set time between callbacks received to publish the message?

We're using sidekiq + threads already, so open to suggestions using those tools.

daino3
  • 4,386
  • 37
  • 48

1 Answers1

7

There is a Sidekiq-debounce gem available.

Another approach (without such gem) is to use the Rails.cache to trigger your execution only once per x time

delay = 1.minute
Rails.cache.fetch('unique-identifier-of-the-job', expires_in: delay) do
 YourActiveJobHere.set(wait: delay).perform_later('your-action')
end
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
  • interesting approach, @MaximKrizhanovsky! I'll give the cache approach a try. – daino3 Oct 10 '16 at 14:18
  • 3
    while the cache is an excellent idea, and I love the simplicity behind it, it will not work with any sort of load balancer or other situation where there are multiple rails instances if it has to be debounced across the application – Sampson Crowley Feb 14 '19 at 17:24