Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby sleep to specific time

Tags:

sleep

ruby

How could i write, that my code sleep not for example 5 seconds, but until some time on machine is reached.

not so:

sleep(5)

but something like:

now = DateTime.Now
while now != "14:40:03"
  sleep(0.5)
end

how is it correct to do?

like image 467
brabertaser19 Avatar asked Oct 18 '13 11:10

brabertaser19


2 Answers

require "time"
sleep(Time.parse("14:40:03") - Time.now)
like image 194
sawa Avatar answered Sep 21 '22 22:09

sawa


Or use the Rufus Scheduler

https://github.com/jmettraux/rufus-scheduler

From the OpenWFEru/ruote project.

require 'rubygems'
require 'rufus-scheduler'

scheduler = Rufus::Scheduler.new

scheduler.in '20m' do
  #do something
end

this provide a real passive waiting, no load.

like image 29
Romain Avatar answered Sep 21 '22 22:09

Romain