Ruby and Rails Async
Posted
by
Edijs Petersons
on Stack Overflow
See other posts from Stack Overflow
or by Edijs Petersons
Published on 2012-08-31T13:08:50Z
Indexed on
2012/08/31
15:39 UTC
Read the original article
Hit count: 222
I need to perform long-running operation in ruby/rails asynchronously. Googling around one of the options I find is Sidekiq.
class WeeklyReportWorker
include Sidekiq::Worker
def perform(user, product, year = Time.now.year, week = Date.today.cweek)
report = WeeklyReport.build(user, product, year, week)
report.save
end
end
# call WeeklyReportWorker.perform_async('user', 'product')
Everything works great! But there is a problem.
If I keep calling this async method every few seconds, but the actual time heavy operation performs is one minute things won't work.
Let me put it in example.
5.times { WeeklyReportWorker.perform_async('user', 'product') }
Now my heavy operation will be performed 5 times. Optimally it should have performed only once or twice depending on whether execution of first operaton started before 5th async call was made.
Do you have tips how to solve it?
© Stack Overflow or respective owner