Search code examples
ruby-on-railsrubycountdown

Rails - Background job (countdown)


i did countdown for time remaining in ruby. But can't figured out how to run it in background, because when i run it in controller or helper it's starting before the view is loaded, so the countdown have to be finished then the view is loaded.

So i need to run it on background with a low priority because i need to save that updated time in DB.(function is working properly)

I found this gem RESQUE, it's good for it or better way is do it manualy?

I'm quite new in ruby, so i appreciate any advice with that.

THIS IS MY JOB

@commits.each do |t|
  timer_date = t.updated_at + 6000
    while Time.now < timer_date
      timeo = Time.at(timer_date.to_i - Time.now.to_i)
      t.remain = timeo
      t.save
      sleep 1
    end
end

Solution

  • You can use sidekiq gem https://github.com/mperham/sidekiq for background processing.