Eventmachine::defer + ruby
I been Using EventMachine for quite a while a know and I really found it great as It Manage show much more me that i didn't have to worry for anything.But recently I just found this weird issue which i just fail to understand
Here what is just telling
I have Eventmachine loop Which look like this
EventMachine::run {
EventMachine::add_periodic_timer(10) do
EventMachine::defer(@operation_block,@callback_block)
end
}
here my operation block look like(Code below use of amqp using carrot gem)
@operation_block = Proc.new { begin puts "Initiating the queue" @carrot ||= Carrot.new(:host => localhost) @queue ||= @carrot.queue("my_queue") puts "The Queue is Poping the message" if @queue.pop [MY LOGIC HERE] $input_to_callback = "SUCCESS" ## IF LOGIC GET EVALUATED WITHOUT ERROR ELSE WILL SET TO FAIL else $input_to_callback = "NOTHING TO PROCESSES" end rescue puts e retry! end $input_to_callback }
Here my callback block look like
@callback_block = Proc.new {|operation_block_output|
if operation_block_output == "SUCCESS"
puts "YAHOOOOOOOOO SUCCESS"
elsif operation_block_output == "NOTHING TO PROCESSES"
puts "BOO Nothing to processes"
else
puts "FAIL ALARM"
end
}
Now Here come the Trouble The Code Though Work the way it should until something bad happen Here what i meant
Now suppose I running this Above Code
I have a queue set name "my_queue" or it will create one if it doesn't exists the queue is initially empty
her开发者_运维技巧e the output that I get on console
Initiating the queue
The Queue is Poping the Message
BOO Nothing to processes
As I message built the output Change in console accordingly based
Now the trouble
If I momentarily shutdown my AMQP server Here the output(to show what i meant)
Initiating the queue
Broken Pipe => Error That Caught in begin rescue block in operation block
Now Start AMQP Server
The Code just never seem move ahead from line where it currently got error that means I never Seem to print the line
The Queue is Poping the message
not only the current defer for which is retry happened but also the new defer operation block that get initiated after the Timer time just elapsed i.e means of the subsequent call to operation block never seem to move ahead and do processing and always print the following output only
Initiating the queue
Just never seem to move ahead(stuck) and do Processing that it meant to i.e fetch the message from the queue do the processing accordingly and all the other stuff(as the AMQP server is running now)
Thanks
Aman Gupta has replied for the question above https://github.com/eventmachine/eventmachine/issues/127#issuecomment-535210
精彩评论