1

I want to schedule an execution for every 30 mins in Ruby on Rails. I know sleep can be used. Is there any other efficient method?

Note that Cron jobs, "whenever" gems won't be helpful since I dont want it as a job just a part of execution. And once sleep is called, is there any other way to wake it up other than aborting it?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Daniel Sagayaraj
  • 177
  • 2
  • 20

1 Answers1

0

I believe you might "wake it" by catching process signals

trap('INT') do
  # do something
end

To wake up a thread, you call the 'run' method, even if it is in the 'sleep' status, it will resume execution

t = Thread.new do
  puts 'going to sleep'
  sleep 100
  loop do
    puts '.'
  end
end

loop do
  sleep 3
  puts t.status
  t.run
end

You could do the same with main thread, just be sure to fetch it with Thread::main

Thread.new do
  sleep 3
  puts Thread::main.status
  Thread::main.run
end

puts 'going to sleep'
sleep 100
loop do
  puts '.'
end