0xSina
0xSina

Reputation: 21553

How do I do a method call every x seconds?

Is there a simple library that will allow me to do schedule a very simple task every x seconds specified by the web request coming in?

I am looking for something along the lines of:

Something.after(5) { call_method }

Very simple. Do I have to install a library like resque, or is there a library that can handle this? I am not looking for full blown background queue processing, just a simple method call after x amount of seconds. I know about resque and sidekiq...

I need to run this from Rails or Sinatra.

Upvotes: 3

Views: 2775

Answers (2)

Nath
Nath

Reputation: 6864

It would be best to ship this out to a background task, take a look at resque-scheduler & Resque. There is also a good Railscast which is worth checking out. Anything else like using crons would not be right.

I did find this gem (sucker_punch) which looks like it would do what you require

Upvotes: 0

Arie Xiao
Arie Xiao

Reputation: 14082

Maybe rewrite your code sample:

Something.after(5) { call_method }

to:

Something
Thread.new do
  sleep(5)
  call_method
end

Upvotes: 5

Related Questions