Skip to content

Instantly share code, notes, and snippets.

@DimaSamodurov
Created September 15, 2019 20:05
Show Gist options
  • Save DimaSamodurov/273a9663f05e64a80ce3d0617706709d to your computer and use it in GitHub Desktop.
Save DimaSamodurov/273a9663f05e64a80ce3d0617706709d to your computer and use it in GitHub Desktop.
module TimeUtils
def self.wait_for(title: '', max_wait_time: 5, wait_interval: 0.1,
raise_timeout_error: false,
debug: false,
&block)
puts "Waiting for #{title}" if debug
waiting_time = 0
loop do
if yield
puts 'Success' if debug
return
end
puts("Sleepping #{wait_interval}..") if debug
sleep(wait_interval)
waiting_time += wait_interval
if waiting_time > max_wait_time
puts "Timeout"
raise Timeout::Error if raise_timeout_error
break
end
end
end
def self.wait_for_file(filename, **wait_options)
wait_for(**(wait_options.merge(title: "file #{filename}"))) do
File.exist?(filename)
end
end
end
puts 'Waiting routine example.'
TimeUtils.wait_for_file 'sample.pdf', debug: true
# You can enter `echo test > sample.pdf` in separate console to test sucessful result.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment