<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>

<channel>
	<title>Programming code snippets</title>
	<atom:link href="http://nerdcave.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://nerdcave.com</link>
	<description>Just another WordPress weblog</description>
	<pubDate>Mon, 04 May 2009 18:51:20 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Patching Mongrel Cluster Restart</title>
		<link>http://nerdcave.com/?p=15</link>
		<comments>http://nerdcave.com/?p=15#comments</comments>
		<pubDate>Sat, 02 May 2009 04:40:14 +0000</pubDate>
		<dc:creator>Jay</dc:creator>
		
		<category><![CDATA[Deployment]]></category>

		<category><![CDATA[Ruby / Rails]]></category>

		<guid isPermaLink="false">http://nerdcave.com/?p=15</guid>
		<description><![CDATA[The following is commonly used to restart a Rails application that uses mongrel and the mongrel_cluster gem:

mongrel_rails cluster::restart

This usually works fine, but if you have a very active application that&#8217;s getting hammered with requests, it&#8217;s not guaranteed that your mongrel instance will stop and start as you expect.  Let&#8217;s take a look at the restart method [...]]]></description>
			<content:encoded><![CDATA[<p>The following is commonly used to restart a Rails application that uses mongrel and the mongrel_cluster gem:</p>

<div class="wp_syntax"><div class="code"><pre class="unix" style="font-family:monospace;">mongrel_rails cluster::restart</pre></div></div>

<p>This usually works fine, but if you have a very active application that&#8217;s getting hammered with requests, it&#8217;s not guaranteed that your mongrel instance will stop and start as you expect.  Let&#8217;s take a look at the restart method to see why that is:</p>

<div class="wp_syntax"><div class="code"><pre class="unix" style="font-family:monospace;">sudo pico /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/lib/mongrel_cluster/init.rb</pre></div></div>

<p>Search for <strong>restart</strong> and look for the <strong>run </strong>method a few lines under.  Here&#8217;s what it looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">    <span style="color:#9966CC; font-weight:bold;">def</span> run
      stop
      start
    <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This is a problem because the <strong>stop</strong> method doesn&#8217;t actually make sure the mongrel instance is down before returning.  So the <strong>start</strong> method will execute immediately after <strong>stop </strong>returns, which means that if your mongrel instance is hung up for whatever reason, <strong>start</strong> may be invoked <em>before your mongrel is actually shut down</em>.  Furthermore, this means that by the time your mongrel actually stops, <strong>start</strong> had already been invoked and failed (you&#8217;ll get the &#8220;mongrel already started&#8221; error).  So then your mongrel eventually stops and will be down until you explicitly run another start.  Not cool.</p>
<p>So, what&#8217;s the solution?  I&#8217;m not sure what most people do, but I like to patch the mongrel <strong>run</strong> method with the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">    <span style="color:#9966CC; font-weight:bold;">def</span> run
<span style="color:#008000; font-style:italic;">#      stop</span>
<span style="color:#008000; font-style:italic;">#      start</span>
      read_options
      <span style="color:#0066ff; font-weight:bold;">@force</span>, <span style="color:#0066ff; font-weight:bold;">@clean</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">false</span>, <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#0066ff; font-weight:bold;">@ports</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>port<span style="color:#006600; font-weight:bold;">|</span>
        <span style="color:#0066ff; font-weight:bold;">@only</span> = port
        stop
        check_wait
        start
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    private
      <span style="color:#9966CC; font-weight:bold;">def</span> check_wait<span style="color:#006600; font-weight:bold;">&#40;</span>wait_time = <span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        wait_time.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#9966CC; font-weight:bold;">unless</span> check_process<span style="color:#006600; font-weight:bold;">&#40;</span>@only<span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#CC0066; font-weight:bold;">sleep</span> <span style="color:#006666;">1</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
        log <span style="color:#996600;">&quot;*Slept #{wait_time} seconds but still not dead, force kill&quot;</span>
        <span style="color:#0066ff; font-weight:bold;">@force</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
        stop
        <span style="color:#0066ff; font-weight:bold;">@force</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
        stop
        <span style="color:#0066ff; font-weight:bold;">@force</span> = <span style="color:#0000FF; font-weight:bold;">false</span>
      <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Just save the file and you&#8217;re done.  I&#8217;m not sure how a purist would rate this on an elegance scale, but it works like a charm.  Now when you execute  <code lang="unix">mongrel_rails cluster::restart</code><br />
and your mongrel happens to be hanging, it will wait <strong>wait_time</strong> seconds before forcibly shutting it down.  It will also print out a message telling you what&#8217;s going on.</p>
<p>If the mongrel(s) of your application are forcefully shut down frequently, you may want to consider running more mongrel instances or optimizing/scaling your application; it&#8217;s not ideal to just forcibly kill your mongrel as common practice since it might be in the middle of processing an &#8220;important&#8221; request (say, some payment request in your application).  I&#8217;ve never run into a problem though.</p>
<p>Anyway, I use this patch on every server that I have Rails installed.  Even if you don&#8217;t think you&#8217;ll need it, doesn&#8217;t hurt.  Saves a lot of headache if and when the time comes.</p>
]]></content:encoded>
			<wfw:commentRss>http://nerdcave.com/?feed=rss2&amp;p=15</wfw:commentRss>
		</item>
	</channel>
</rss>
