<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[a blog thing.]]></title>
  <link href="http://rares.github.com/atom.xml" rel="self"/>
  <link href="http://rares.github.com/"/>
  <updated>2012-05-18T16:17:41-04:00</updated>
  <id>http://rares.github.com/</id>
  <author>
    <name><![CDATA[Rob Ares]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Safe Shutdown Of Eventmachine Reactors]]></title>
    <link href="http://rares.github.com/2010/09/26/safe-shutdown-of-eventmachine-reactors/"/>
    <updated>2010-09-26T00:00:00-04:00</updated>
    <id>http://rares.github.com/2010/09/26/safe-shutdown-of-eventmachine-reactors</id>
    <content type="html"><![CDATA[<p>
  Recently I found myself trying to devise a way to prevent a
  process from shutting down until an EventMachine
  <a href="http://en.wikipedia.org/wiki/Reactor_pattern" target="_blank">Reactor Loop</a>
  was finished performing some network IO.
  I found a particular pattern quite effective and wanted to it share here.
</p>

<p>
  In-case you haven&#8217;t done much work with EventMachine, it provides a mixin (called a <a href="http://eventmachine.rubyforge.org/EventMachine/Deferrable.html">Deferrable</a>) that allows
  the developer to model their activity in the reactor loop as callbacks (success and failure). Using this pattern,
  we can create a class to register
  pieces of work and unregister them when the work is complete. In this way, we can make assertions on whether
  or not it is safe to shut down a process or not depending or not if we have work left to complete.
</p>

<p>
  The exact situation I am trying to solve for is the following: I have a client that is pulling messages off
  of a queue and sending messages to another system (via another protocol). At any given time, we will have
  N number of IO jobs active in the event loop. But what happens when we send -TERM to this process? EventMachine
  starts closing these open connections and network calls that we want to succeed will fail. What I wanted was
  a way to register individual pieces of work (pulling the message off the queue and pushing to the remote service)
  and check to see if all the work is done (thus safe to allow the process to exit).
</p>

<p>
 So consider the following class:
</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
</pre></td><td class='code'><pre><code class='rb'><span class='line'><span class="k">class</span> <span class="nc">DeferrableProgressMonitor</span>
</span><span class='line'>  <span class="kp">attr_reader</span> <span class="ss">:total</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">initialize</span>
</span><span class='line'>    <span class="vi">@marked</span><span class="p">,</span> <span class="vi">@total</span> <span class="o">=</span> <span class="o">[]</span><span class="p">,</span> <span class="mi">0</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">register</span><span class="p">(</span><span class="n">deferrable</span><span class="p">)</span>
</span><span class='line'>    <span class="vi">@total</span> <span class="o">+=</span> <span class="mi">1</span>
</span><span class='line'>    <span class="vi">@marked</span> <span class="o">&lt;&lt;</span> <span class="n">deferrable</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">deferrable</span><span class="o">.</span><span class="n">callback</span> <span class="p">{</span> <span class="o">|</span><span class="n">object</span><span class="o">|</span> <span class="vi">@marked</span><span class="o">.</span><span class="n">pop</span> <span class="p">}</span>
</span><span class='line'>    <span class="n">deferrable</span><span class="o">.</span><span class="n">errback</span> <span class="p">{</span> <span class="o">|</span><span class="n">e</span><span class="p">,</span> <span class="n">object</span><span class="o">|</span> <span class="vi">@marked</span><span class="o">.</span><span class="n">pop</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="kp">nil</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>  <span class="k">alias</span> <span class="ss">:&lt;&lt;</span> <span class="ss">:register</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">current</span>
</span><span class='line'>    <span class="vi">@marked</span><span class="o">.</span><span class="n">size</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">wait?</span>
</span><span class='line'>    <span class="vi">@total</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="vi">@marked</span><span class="o">.</span><span class="n">empty?</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>

<p>Here is a small piece of code that uses it:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='rb'><span class='line'><span class="no">EM</span><span class="o">.</span><span class="n">run</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">monitor</span> <span class="o">=</span> <span class="no">DeferrableProgressMonitor</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'>  <span class="nb">trap</span><span class="p">(</span><span class="s2">&quot;TERM&quot;</span><span class="p">)</span> <span class="k">do</span>
</span><span class='line'>    <span class="k">unless</span> <span class="n">monitor</span><span class="o">.</span><span class="n">wait?</span>
</span><span class='line'>      <span class="no">EM</span><span class="o">.</span><span class="n">stop</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">monitor</span> <span class="o">&lt;&lt;</span> <span class="n">thing_that_returns_a_deferrable</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>

<p>
  So the signal trap will make a call to check to see if there is any outstanding work
  left. if there is, we keep on going (my actual code sets a timer that continually keeps checking #wait?).
  Otherwise, we shut the loop down.
</p>

<p>
  The are a few gotchas here:
  <ul>
    <li>
      The signature for Deferrable#callback and Deferrable#errback will fail if it
      doesn&#8217;t match the call to Deferrable#set_deferred_status. So this pattern breaks down a bit as a general solution
      but works well if you have a well-defined activity whose completion is critical.
    </li>
    <li>
      Be mindful handling errors, anything that raises can cause you counters to be off, thus preventing
      the process without sending -KILL to it. You would want to try to catch what you can and utilize
      Deferrable#set_deferred_status to control success/failure.
    </li>
  </ul>
</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Rails Rumble 2009 Entry A.ppend.to]]></title>
    <link href="http://rares.github.com/2009/08/24/rails-rumble-2009-entry-a.ppend.to/"/>
    <updated>2009-08-24T00:00:00-04:00</updated>
    <id>http://rares.github.com/2009/08/24/rails-rumble-2009-entry-a.ppend.to</id>
    <content type="html"><![CDATA[<p>
 Over the weekend, I had the privilege of competing with <a href="http://r09.railsrumble.com/teams/scatapult" target="_blank">Scatapult</a> in this year&#8217;s Rails Rumble. I had an absolute blast working with <a href="http://twitter.com/reagent">Pat</a>, <a href="http://twitter.com/tpitale">Tony</a> and <a href="http://twitter.com/averyvery">Doug</a> this time around and
it goes without saying that everyone was on their game.
</p>

<p>
a.ppend.to&#8217;s description:
<blockquote>
  Microblogging? How about Macroblogging? Use the power of Twitter to create a media-rich personal blog from just 140 characters of text.

  Simply send an @reply via Twitter to us (@ppend) and we&#8217;ll do the rest. We recognize different types of content including photos, videos, mp3 audio, quotes, and more - so start posting!
</blockquote>
</p>
<p>
Please check <a href="http://a.ppend.to" target="_blank">it</a> out when it goes to public voting next week and vote for us on our  <a href="http://r09.railsrumble.com/teams/scatapult" target="_blank">team page</a>.
</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Streaming Twitter Api]]></title>
    <link href="http://rares.github.com/2009/07/14/streaming-twitter-api/"/>
    <updated>2009-07-14T00:00:00-04:00</updated>
    <id>http://rares.github.com/2009/07/14/streaming-twitter-api</id>
    <content type="html"><![CDATA[<p>
  I have been researching the <a href="http://apiwiki.twitter.com/Streaming-API-Documentation" target="_blank" rel="nofollow">Twitter Streaming API</a> recently for a side project (secret) and was playing around with it in Ruby to get a feel for how it works. I started out using an <a href="http://github.com/igrigorik/em-http-request/tree/master" target="_blank" rel="nofollow">asynchronous http client</a> to take advantage of the API but wound up having some issues with how it was chunking json as a response type (.xml was fine).
</p>
<p>
  I wound up stumbling upon Brian Lopez&#8217;s <a href="http://github.com/brianmario/yajl-ruby/tree/master" target="_blank" rel="nofollow">yajl-ruby</a> which is a ruby binding for a native streaming json library. It has excellent support for dealing with streams and also supports persistent http connections (perfect for the streaming API).
</p>

<p>
  Below is some code utilizing yajl that opens a connection and pushes the screen name and text of a tweet to the console as it is received. (disclaimer: This is not even close to being production-ready as Twitter has strong recommendations for reconnect logic and dealing with the API&#8217;s alpha QOS). This is using the publicly available sprinkler stream; which is only a subsection of the public timeline but should be good enough for general application use (there are gardenhose and firehose streams available for statistical purposes but access to these is at Twitter&#8217;s discretion).
</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='rb'><span class='line'><span class="nb">require</span> <span class="s2">&quot;uri&quot;</span>
</span><span class='line'><span class="nb">require</span> <span class="s2">&quot;yajl/http_stream&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="n">u</span> <span class="o">=</span> <span class="s2">&quot;twitter username here&quot;</span>
</span><span class='line'><span class="nb">p</span> <span class="o">=</span> <span class="s2">&quot;twitter password here&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="n">url</span> <span class="o">=</span> <span class="no">URI</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s2">&quot;http://</span><span class="si">#{</span><span class="n">u</span><span class="si">}</span><span class="s2">:</span><span class="si">#{</span><span class="nb">p</span><span class="si">}</span><span class="s2">@stream.twitter.com/spritzer.json&quot;</span><span class="p">)</span>
</span><span class='line'><span class="no">Yajl</span><span class="o">::</span><span class="no">HttpStream</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="ss">:symbolize_keys</span> <span class="o">=&gt;</span> <span class="kp">true</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">status</span><span class="o">|</span>
</span><span class='line'>  <span class="k">unless</span> <span class="n">status</span><span class="o">.</span><span class="n">has_key?</span><span class="p">(</span><span class="ss">:delete</span><span class="p">)</span>
</span><span class='line'>    <span class="nb">puts</span> <span class="s2">&quot;screen_name: </span><span class="si">#{</span><span class="n">status</span><span class="o">[</span><span class="ss">:user</span><span class="o">][</span><span class="ss">:screen_name</span><span class="o">]</span><span class="si">}</span><span class="s2">&quot;</span>
</span><span class='line'>    <span class="nb">puts</span> <span class="s2">&quot;message: </span><span class="si">#{</span><span class="n">status</span><span class="o">[</span><span class="ss">:text</span><span class="o">]</span><span class="si">}</span><span class="s2">&quot;</span>
</span><span class='line'>    <span class="nb">puts</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>

<p>
  And that is it. I can point out that the streams will additionally push delete operations out that are to be honored by client applications. Above I am filtering out the delete messages by key name and only printing out actual statuses.
</p>
]]></content>
  </entry>
  
</feed>

