retry in Ruby can not be performed - ruby

for i in 1..5
retry if i > 2
puts "Value of local variable is #{i}"
end
When I ran the code above, I got an error message saying Invalid retry
The version of Ruby I was using is 1.9.3. Would anyone know what went wrong with the code?

From Ruby 1.9 onwards retry needs to be in a rescue clause and will only work when in a rescue clause

Related

Ruby C extension returns report_on_exception is true

I was trying to import server_info api in the ibm_db extension. It worked well in windows but in Linux, it returns tread sleep error.
#<Thread:0x000055c9febc5ca0 sleep> terminated with exception (report_on_exception is true):
Traceback (most recent call last):
stack level too deep (SystemStackError)
^C*** stack smashing detected ***: <unknown> terminated
code:-
require 'ibm_db'
conn = IBM_DB.connect('DATABASE=;HOSTNAME=localhost;PORT=50000;PROTOCOL=TCPIP;UID=;PWD=','','')
puts 'this is ibm_db'
#Thread.report_on_exception = false
puts 'this is begin'
puts IBM_DB
serverinfo = IBM_DB::server_info( conn )
puts serverinfo.DBMS_NAME[0,100]
puts IBM_DB.close(conn)
why the same code returns the error in Linux but woe=rks well in windows.
When I run the code in debug mode it does not produce any error only in normal mode it produces an error.
Thanks,
Akhil
Well, two approach
use sleep for 5-10 second after db connection
check db connection like: p "connected" if ActiveRecord::Base.connected?
In your case, need to check in ibm_db gem how to check if connection established or not
conn=ibm_db.connect("DATABASE=database;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=username;PWD=password",'','')
connState = ibm_db.active(conn)
print(connState)
if it says "TRUE" go ahead else pause/sleep whatever your requirement.

Thread deadlock on simple operations in pry

[Note: I "fixed" this problem by creating and using a new gemset. I'm still curious why the problem occurred but it is no longer blocking me.]
[I am aware that there is a similar issue at Deadlock in Ruby join(), but I have tried the timeout parameter suggested there and it does not help. I suspect there is a pry-specific problem not covered there.]
I am getting the error below when running the code below, but only when executed within a pry session. This code has not been changed and has been working fine for quite a while, and I have no idea why it's an issue just now. I am using pry version 0.11.3 on Ruby 2.5.1. Also, this code works fine when pasted into pry; it's not working in my wifi-wand application that launches pry in the context of one of its objects (gem install wifi-wand to install, https://github.com/keithrbennett/wifiwand is the project page).
domains = %w(google.com baidu.com)
puts "Calling dig on domains #{domains}..." if #verbose_mode
threads = domains.map do |domain|
Thread.new do
output = `dig +short #{domain}`
output.length > 0
end
end
threads.each(&:join)
[1] pry(#<WifiWand::CommandLineInterface>)> ci
Calling dig on domains ["google.com", "baidu.com"]...
fatal: No live threads left. Deadlock?
3 threads, 3 sleeps current:0x00007fbd13d0c5a0 main thread:0x00007fbd13d0c5a0
* #<Thread:0x00007fbd14069c20 sleep_forever>
rb_thread_t:0x00007fbd13d0c5a0 native:0x00007fff89c2b380 int:0
/Users/kbennett/work/wifi-wand/lib/wifi-wand/models/base_model.rb:89:in `join'
/Users/kbennett/work/wifi-wand/lib/wifi-wand/models/base_model.rb:89:in `each'
/Users/kbennett/work/wifi-wand/lib/wifi-wand/models/base_model.rb:89:in `block in connected_to_internet?'
/Users/kbennett/work/wifi-wand/lib/wifi-wand/models/base_model.rb:126:in `connected_to_internet?'
/Users/kbennett/work/wifi-wand/lib/wifi-wand/command_line_interface.rb:264:in `cmd_ci'
Strangely, the problem was fixed by uninstalling and reinstalling the awesome_print gem. I have no idea why.

Ruby Logger rotation on CentOs

I´m doing some tests with default ruby Logger
and I´m getting this error on file rotation
log rotation inter-process lock failed. Text file busy #
rb_file_s_rename - (file.log, file.log.0)
log writing failed. closed stream
log shifting failed. closed stream
My environment.
CentOS 7.2.1511
Ruby: 2.0.0, 2.1.0, 2.3.0, 2.3.1
Note:
This error only occurs on CentOS with ruby versions after 2.0.0. With ruby 2.0.0 works fine!
I made some tests using Ubuntu with all ruby versions after 1.9 and
this error not occurs.
Here my test code
require 'logger'
begin
puts "Starting test"
# rotate when file size equals 20kb keeping 3 old logs
log = Logger.new("file.log", 3, 20000)
while true
puts "Logging..."
log.info "info"
log.error "error"
log.warn "warn"
log.debug "debug"
sleep(1)
end
rescue Interrupt => _
puts "Exiting..."
end
Thanks for help

chef: How do I increase the CommandTimout for Mixlib::ShellOut in a ruby block?

I'm trying to perform a database data load in a chef recipe on an Ubuntu 14.04 system with a Chef 11.10.4 based client. I'm doing the data load in a ruby block and here are the relevant portions of the error in the log:
Mixlib::ShellOut::CommandTimeout
Command timed out after 600s:
Command exceeded allowed execution time, killed by TERM signal.
[2015-10-06T01:24:00+00:00] ERROR: ruby_block[Load Dataset] (chef-virtuoso::load line 178) had an error:
Mixlib::ShellOut::CommandTimeout: Command timed out after 600s:
Anyone know how to increase the default CommandTimeout to 3600 for a shell_out! in a ruby block?
Here is the ruby block from the recipe that is failing:
::Chef::Recipe.send(:include, Chef::Mixin::ShellOut)
ruby_block "Load Dataset" do
block do
shell_out!(DATA_LOAD_COMMAND)
end
end
According to mixlib-shellout code and chef mixin shell_out code you should be able to use the timeout option like this:
ruby_block "Load Dataset" do
block do
shell_out!(DATA_LOAD_COMMAND, :timeout => 3600)
end
end
You don't have to send the Class into Recipe, it's already part of the DSL.

IO::EAGAINWaitReadable: Resource temporarily unavailable - read would block

I get the following error when I try to use the method "read_nonblock" from the "socket" library
IO::EAGAINWaitReadable: Resource temporarily unavailable - read would block
But when I try it through the IRB on the terminal it works fine
How can I make it read the buffer?
I get the following error when I try to use the method "read_nonblock" from the "socket" library
It is expected behaviour when the data isn't ready in the buffer. Since the exception, IO::EAGAINWaitReadable is originated from ruby version 2.1.0, in older version you must trap IO::WaitReadable with additional port selection and retry. So do as it was adviced in the ruby documentation:
begin
result = io.read_nonblock(maxlen)
rescue IO::WaitReadable
IO.select([io])
retry
end
For newer version os ruby you should trap IO::EAGAINWaitReadable also, but just with retrial reading for a timeout or infinitely. I haven't found out the example in the docs, but remember that it was without port selection:
begin
result = io.read_nonblock(maxlen)
rescue IO::EAGAINWaitReadable
retry
end
However some my investigations lead to that it is also better to do port selection on IO::EAGAINWaitReadable, so you'll can get:
begin
result = io.read_nonblock(maxlen)
rescue IO::WaitReadable, IO::EAGAINWaitReadable
IO.select([io])
retry
end
To support the code with both versions of exception, just declare the definition of IO::EAGAINWaitReadable in lib/ core under if clause:
if ! ::IO.const_defined?(:EAGAINWaitReadable)
class ::IO::EAGAINWaitReadable; end
end

Resources