0

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
liborza
  • 969
  • 1
  • 7
  • 28

1 Answers1

1

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

Sakshi Jain
  • 410
  • 5
  • 21
  • That is based on external jobs? Or it's local? – liborza May 05 '16 at 17:54
  • Because i have more than 100 commits and each of them running that function countdown so its pretty huge – liborza May 05 '16 at 17:54
  • Sidekiq can handle many jobs at the same time in the same process. You can always add workers for different jobs and enqueue them in sidekiq. There is also a Railscast available for using this gem http://railscasts.com/episodes/366-sidekiq – Sakshi Jain May 05 '16 at 17:57
  • 1
    I found that before, just wasn't really sure how it's gonna work so i'll try that. Thanks a lot. – liborza May 05 '16 at 17:59