On Fri, Aug 24, 2012 at 03:57:26AM -0700, edward wrote:
>
> the following is my script:
> -----<8----
> def time_format(t) ;t.strftime("%Y-%m-%d %a %H:%M") ;end
>
> begin
> scheduler = Rufus::Scheduler.start_new
> i=0
> scheduler.every '30s' do |job|
> shut_time=time_format(Time.now)
> p shut_time
> if shut_time =="2012-08-24 Fri 18:53"
> puts "crop is ready #{i}."
> job.unschedule
> Process.exit(0)
> else
> i+=1
> puts "crop not yet ready..#{i}."
> end
> end
> scheduler.join
> end
> -------->8--------
Hello,
too bad you didn't mention what exception got caught.
Two variants, a:
---8<----
require 'rufus-scheduler'
def time_format(t); t.strftime("%Y-%m-%d %a %H:%M"); end
scheduler = Rufus::Scheduler.start_new
def scheduler.handle_exception(job, exception)
exit(0) if exception.is_a?(SystemExit)
p [ job, exception ]
end
i = 0
scheduler.every '30s' do |job|
shut_time = time_format(Time.now)
p shut_time
if shut_time == "2012-08-24 Fri 18:53"
puts "crop is ready #{i}."
#job.unschedule # no need to unschedule if we exit
Process.exit(0)
end
i += 1
puts "crop not yet ready..#{i}."
end
scheduler.join
--->8---
b:
---8<----
require 'rufus-scheduler'
def time_format(t); t.strftime("%Y-%m-%d %a %H:%M"); end
scheduler = Rufus::Scheduler.start_new
def scheduler.handle_exception(job, exception)
exit(0) if exception.message == 'over.'
p [ job, exception ]
end
i = 0
scheduler.every '30s' do |job|
shut_time = time_format(Time.now)
p shut_time
if shut_time == "2012-08-24 Fri 18:53"
puts "crop is ready #{i}."
#job.unschedule # no need to unschedule if we exit
raise "over."
end
i += 1
puts "crop not yet ready..#{i}."
end
scheduler.join
--->8---
And a third variant, because I think it's overkill to use rufus-scheduler for
that:
---8<---
require 'time'
# to enable Time.parse(s)
shutdown_time = Time.parse('2012-08-24 Fri 18:53')
i = 0
loop do
sleep 30
if Time.now >= shutdown_time
puts "#{Time.now} - crop is ready #{i}."
break
end
i = i + 1
puts "#{Time.now} - crop not yet ready..#{i}."
end
--->8---
Best regards,