# EventMachine::Defer + Weird issue

**URL:** <https://rubytalk.org/t/eventmachine-defer-weird-issue/60564>\
**Category:** ruby-talk\
**Created:** [22 October 2010 07:48 UTC](https://rubytalk.org/t/eventmachine-defer-weird-issue/60564 "2010-10-22T07:48:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ken\_Ghosh](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@Ken\_Ghosh](https://rubytalk.org/u/Ken_Ghosh)\
**Post date:** [22 October 2010 07:48 UTC](https://rubytalk.org/t/eventmachine-defer-weird-issue/60564/1 "2010-10-22T07:48:24Z")

</div>

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 {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EventMachine::add\_periodic\_timer(10)  
> &nbsp;&nbsp;do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EventMachine::defer(@operation\_block,@callback\_block)  
> end }

here my operation block look like(Code below use of amqp using carrot  
gem)

> &nbsp;&nbsp;&nbsp;&nbsp;@operation\_block = Proc.new {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Initiating the queue"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@carrot ||= Carrot.new(:host =\> localhost)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@queue ||= @carrot.queue("my\_queue")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "The Queue is Poping the message"

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @queue.pop

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[MY LOGIC HERE]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$input\_to\_callback = "SUCCESS" ## IF LOGIC GET  
EVALUATED WITHOUT ERROR ELSE WILL SET TO FAIL  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$input\_to\_callback = "NOTHING TO PROCESSES"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts e  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retry!  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$input\_to\_callback

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

Here my callback block look like

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@callback\_block = Proc.new {|operation\_block\_output|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if operation\_block\_output == "SUCCESS"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "YAHOOOOOOOOO SUCCESS"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif operation\_block\_output == "NOTHING TO PROCESSES"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "BOO Nothing to processes"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "FAIL ALARM"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

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

here the output that I get on console

> &nbsp;&nbsp;Initiating the queue

> &nbsp;&nbsp;The Queue is Poping the Message

> &nbsp;&nbsp;BOO Nothing to processes

As I message built the output Change in console accordingly based

Now the trouble

&nbsp;&nbsp;If I momentarily shutdown my AMQP server Here the output(to show what  
i meant)

> Initiating the queue
> 
> &nbsp;&nbsp;Broken Pipe =\> Error That Caught in  
> begin rescue block in operation block

&nbsp;&nbsp;Now Start AMQP Server

&nbsp;&nbsp;The Code just never seem move ahead from line where it currently got  
error  
&nbsp;&nbsp;that means I never Seem to print the line

&nbsp;&nbsp;The Queue is Poping the message

&nbsp;&nbsp;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  
&nbsp;&nbsp;&nbsp;&nbsp;i.e means of the subsequent call to operation block  
&nbsp;&nbsp;&nbsp;&nbsp;never seem to move ahead and do processing and always print the  
following  
&nbsp;&nbsp;&nbsp;&nbsp;output only

&nbsp;&nbsp;&nbsp;&nbsp;Initiating the queue

&nbsp;&nbsp;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)

&nbsp;&nbsp;Thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
