<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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" version="2.0">

<channel>
	<title>A Place for my head</title>
	
	<link>http://gnufied.org</link>
	<description>On Ruby, Rails, Concurrency and fiction</description>
	<pubDate>Thu, 22 Apr 2010 18:34:49 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/gnufied/ivAE" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="gnufied/ivae" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Faster zsh and git integration</title>
		<link>http://gnufied.org/2010/04/22/faster-zsh-and-git-integration/</link>
		<comments>http://gnufied.org/2010/04/22/faster-zsh-and-git-integration/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 18:32:06 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
		
		<category><![CDATA[git]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[zsh]]></category>

		<guid isPermaLink="false">http://gnufied.org/?p=149</guid>
		<description><![CDATA[There are quite a few zsh init scripts available out there which implement git integration. Unfortunately, they also slow down the shell workflow. I have found myself continuously frustrated by the fact, how much they make switching between directories slower. In the end, I have tried implementing something on my own and result is reasonably [...]]]></description>
			<content:encoded><![CDATA[<p>There are quite a few zsh init scripts available out there which implement git integration. Unfortunately, they also slow down the shell workflow. I have found myself continuously frustrated by the fact, how much they make switching between directories slower. In the end, I have tried implementing something on my own and result is reasonably faster than `oh-my-zsh` or other solutions.</p>
<p>You can get the code from:</p>

<div class="wp_syntax"><div class="code"><pre>git clone git://github.com/gnufied/dot_files.git</pre></div></div>

<p>Be warned that, it may or may not work with old version of zsh or git.</p>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2010/04/22/faster-zsh-and-git-integration/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The guilty man speaks (of Mac OS X)</title>
		<link>http://gnufied.org/2009/11/26/the-guilty-man-speaks-of-mac-os-x/</link>
		<comments>http://gnufied.org/2009/11/26/the-guilty-man-speaks-of-mac-os-x/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 17:48:28 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
		
		<category><![CDATA[macosx]]></category>

		<guid isPermaLink="false">http://gnufied.org/?p=140</guid>
		<description><![CDATA[I have been a GNU/Linux user for almost 7 years (no not dual booting, but exclusive user). Recently, I switched jobs ( Castle Rock Research  great place to work btw) and ended up with MacBook pro in the bargain.
You can imagine as a Linux user I have used everything thats out there literally (Gnome,KDE,Xmonad, [...]]]></description>
			<content:encoded><![CDATA[<p>I have been a GNU/Linux user for almost 7 years (no not dual booting, but exclusive user). Recently, I switched jobs (<a href="http://www.castlerockresearch.com/"> Castle Rock Research </a> great place to work btw) and ended up with MacBook pro in the bargain.</p>
<p>You can imagine as a Linux user I have used everything thats out there literally (Gnome,KDE,Xmonad, Awesome, WindowMaker, Stumpwm). My previous notebook was running KDE 4.3.2 and was pretty happy with it, overall.</p>
<p>Since I started using Macbook, my initial days were all pain, toil and fumbling in dark. Man O man. The biggest confusion is, many well known applications(such as Netbeans) use Command key in place of Control key in OSX. But this behavior is not uniform (there is no behavior, thats just my flimsy theory of adapting to OSX environment). So for copy you will have to press Command-C, but for some other shortcut you may still have to use Control key.</p>
<p>Then there is Emacs. The first question you will ask yourself as Emacs user on Mac OSX is, what should be the Meta key? Alt or Command? Well for me, both. Needless to say, Emacs will block some of global OSX shortcuts that use Command key, when you start using Command as meta key in Emacs.</p>
<p>What I miss most from Linux?</p>
<ul>
<li> apt-get.</li>
<li> Having a true POSIX environment which is supported by open source folks. Now there. Don&#8217;t jump the gun and say  OSX is <strong>POSIX</strong>. I know that already, but as I discovered painfully, Snow Leopard in particular has broken many Open Source libraries and tools. (Emacs23, rb-gsl, RMagick and many more). It was painful to see Apple fans blaming open source developers for this breakage and not coming up with the fix.</li>
<li> KDE4.3. I really loved KDE 4.3. I loved multi-screen handling  of KDE 4.3.2. I could move window between screens. I could move focus between screens. I can set window to start in particular screen. All without moving mouse. Its way ahead of whats there in Mac OSX (which is click and drag essentially).I miss vertical maximize. I miss, window specific shortcuts which I used to have ( and yeah QuickSilver can fix some of that, but its nice to have all these features right out of the box).</li>
<li> I miss Qt and Gtk. I was decent with Gtk+ I think. When fancy took me, I could whip up utility applications in Gtk+. I have new ropes to learn now.</li>
<li> More importantly I understood the system inside out (at least little more than basics).</li>
</ul>
<p>What I gained?</p>
<ul>
<li> Applications. CRRC uses some specific tools for communication, which simply won&#8217;t work on Linux. </li>
<li> No hibernate and hence fast resume. </li>
<li> Better batter life. </li>
</ul>
<p>I certainly wouldn&#8217;t add &#8220;ease of use&#8221; or crap like that here. If applications I need, would run on Linux, I would format this stuff and go back to Linux.</p>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2009/11/26/the-guilty-man-speaks-of-mac-os-x/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Constructor memory leaks</title>
		<link>http://gnufied.org/2009/03/25/constructor-memory-leaks/</link>
		<comments>http://gnufied.org/2009/03/25/constructor-memory-leaks/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 05:16:01 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
		
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://gnufied.org/?p=132</guid>
		<description><![CDATA[Yesterday I fixed a nasty memory leak in our PushServer which is written in Scala. As so happens with all the memory leaks, application was  running smoothily in test mode and just when I deployed in production it bombed within hours of launching the application.  The problem was with following bit of code:

class Quote&#40;dataHash: Map&#91;Int,Any&#93;,oldQuote: [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I fixed a nasty memory leak in our PushServer which is written in Scala. As so happens with all the memory leaks, application was  running smoothily in test mode and just when I deployed in production it bombed within hours of launching the application.  The problem was with following bit of code:</p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">class</span> Quote<span style="color: #009900;">&#40;</span>dataHash<span style="color: #339933;">:</span> <span style="color: #003399;">Map</span><span style="color: #009900;">&#91;</span>Int,<span style="color: #003399;">Any</span><span style="color: #009900;">&#93;</span>,oldQuote<span style="color: #339933;">:</span> Quote<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">extends</span> AmtdStockData <span style="color: #009900;">&#123;</span>
  val symbol<span style="color: #339933;">:</span> <span style="color: #003399;">String</span> <span style="color: #339933;">=</span> dataHash.<span style="color: #006633;">getOrElse</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>,oldQuote.<span style="color: #006633;">symbol</span><span style="color: #009900;">&#41;</span>
  val bid<span style="color: #339933;">:</span> <span style="color: #003399;">Float</span> <span style="color: #339933;">=</span> dataHash.<span style="color: #006633;">getOrElse</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>,oldQuote.<span style="color: #006633;">bid</span><span style="color: #009900;">&#41;</span>
  val ask<span style="color: #339933;">:</span> <span style="color: #003399;">Float</span> <span style="color: #339933;">=</span> ....
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Above code defines Quote class, the constructor of class takes a data Map as an argument and old quote for the same symbol. Class populates its field from data Map, but if a certain field is not found in the Map, it uses the field&#8217;s value from oldQuote. The Quote class basically works around the problem from data providers which send only updated fields for Level 1 quote.</p>
<p>Probably you have guessed by now, the problem with above code is that memory that is held by old quotes are never freed, because newer quotes always keep a reference to old quote values.</p>
<p>I can probably say, similar logic wouldn&#8217;t have created problems with Java or C++, but since constructor arguments are automatically taken as class fields in scala, we have a problem. The ability to pass arguments while defining a class and using it as a constructor is a life saver.It helps in avoiding placeholder field declaration which will be later populated through constructor or another method call as done in Java/C++.</p>
<p>But apparently, in this particular case I needed old ugly way back and hence new code is:</p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">class</span> Quote <span style="color: #000000; font-weight: bold;">extends</span> AmtdStockData <span style="color: #009900;">&#123;</span>
  var symbol<span style="color: #339933;">:</span> <span style="color: #003399;">String</span> <span style="color: #339933;">=</span> _
  var bid<span style="color: #339933;">:</span> <span style="color: #003399;">Float</span> <span style="color: #339933;">=</span> _
  var ask<span style="color: #339933;">:</span> <span style="color: #003399;">Float</span> <span style="color: #339933;">=</span> _
  def populateValues<span style="color: #009900;">&#40;</span>dataHash<span style="color: #339933;">:</span> <span style="color: #003399;">Map</span><span style="color: #009900;">&#91;</span>Int,<span style="color: #003399;">Any</span><span style="color: #009900;">&#93;</span>,oldQuote<span style="color: #339933;">:</span> Quote<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// populate above fields</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Above class design solves the problem at the expense of using &#8220;var&#8221;.Its not ideal, but I am scouting for ideas if the class can be designed in any other way!</p>
<p><strong>Update: </strong>Turns out compiler usually doesn&#8217;t emit field for constructor parameter unless parameter is accessed by an instance method/inner class. In above case, getOrElse creates an anonymous inner class and hence compiler emits a field for oldQuote and its never garbage collected.</p>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2009/03/25/constructor-memory-leaks/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Autotest and add_exception method</title>
		<link>http://gnufied.org/2009/01/25/autotest-and-add_exception-method/</link>
		<comments>http://gnufied.org/2009/01/25/autotest-and-add_exception-method/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 18:21:01 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
		
		<category><![CDATA[rails]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gnufied.org/?p=127</guid>
		<description><![CDATA[Stuff floating on intrawebs for adding more files to be ignored using &#8220;add_exception&#8221; may not work because by the time &#8220;run&#8221; hook gets a chance to run, files to be ignored regexp is already compiled. The alternative is to use &#8220;initialize&#8221; hook like this:

1
2
3
Autotest.add_hook :initialize do &#124;at&#124;
  at.add_exception&#40;/^&#40;coverage&#124;\.git&#41;/&#41;
end

This is with 3.11.0 of ZenTest and [...]]]></description>
			<content:encoded><![CDATA[<p>Stuff floating on intrawebs for adding more files to be ignored using &#8220;add_exception&#8221; may not work because by the time &#8220;run&#8221; hook gets a chance to run, files to be ignored regexp is already compiled. The alternative is to use &#8220;initialize&#8221; hook like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby">Autotest.<span style="color:#9900CC;">add_hook</span> <span style="color:#ff3333; font-weight:bold;">:initialize</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>at<span style="color:#006600; font-weight:bold;">|</span>
  at.<span style="color:#9900CC;">add_exception</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^<span style="color:#006600; font-weight:bold;">&#40;</span>coverage<span style="color:#006600; font-weight:bold;">|</span>\.<span style="color:#9900CC;">git</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>This is with 3.11.0 of ZenTest and YMMV.</p>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2009/01/25/autotest-and-add_exception-method/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Amarok2 and redefinition of awesomeness</title>
		<link>http://gnufied.org/2009/01/23/amarok2-and-redefinition-of-awesomeness/</link>
		<comments>http://gnufied.org/2009/01/23/amarok2-and-redefinition-of-awesomeness/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 08:57:21 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
		
		<category><![CDATA[amarok]]></category>

		<guid isPermaLink="false">http://gnufied.org/?p=119</guid>
		<description><![CDATA[ Disclaimer:  Fully aware of the fact that this post will bring no change to Amarok or KDE, I am setting out to write this nonetheless. I know I should have been more constructive, but this is all I got to spare right now.
With the disclaimer out of the way, I used to be [...]]]></description>
			<content:encoded><![CDATA[<p><strong> Disclaimer: </strong> Fully aware of the fact that this post will bring no change to Amarok or KDE, I am setting out to write this nonetheless. I know I should have been more constructive, but this is all I got to spare right now.</p>
<p style="text-align: left;">With the disclaimer out of the way, I used to be a KDE user and devoted Amarok user. Even when fancy took me to run Gnome, I used to run Amarok faithfully. Nothing unique, many Gnome users do the same.</p>
<p>Amarok2 has been criticised and critics attacked fittingly. Starting with huge <a href="http://amarok.kde.org/blog/archives/862-KDE-Trolls,-eat-this.html"> &#8220;fuck you&#8221; </a>in the clouds (addition is mine and text theirs) and much politer version ( <a title="1.4 Feature and code bloat" href="http://amarok.kde.org/en/node/604 ">http://amarok.kde.org/en/node/604</a> ).</p>
<p style="text-align: left;"><a href="http://gnufied.org/wp-content/uploads/2009/01/kde_be_free_small.jpg"><img class="size-medium wp-image-113 alignleft" style="margin: 5px 10px;" title="kde_be_free_small" src="http://gnufied.org/wp-content/uploads/2009/01/kde_be_free_small-300x208.jpg" alt="" width="300" height="208" /></a></p>
<p style="text-align: left;">However I am not entirely convinced that Amarok1.4 was a sinking  boat of feature creep and junkyard of code. At least it wan&#8217;t trying to be a dysfunctional video player like Banshee, right? <img src='http://gnufied.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p style="text-align: left;">Anyways I care about very few features and only complain from me in feature department is, they have hidden some quick actions inside deep menus in Amarok2, such as &#8220;repeat single track&#8221;. Apart from this snag, me and Amarok2 go quite well together when it comes to features.</p>
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">Now coming to main point.</p>
<ul>
<li><strong>Stability :  2 </strong>hasn&#8217;t been stable. Even with latest version, I have experienced playback issues and there is this UI jumble that none can deny. More of this can be found at: <a href="http://www.flickr.com/photos/gnufied/tags/amarok/">http://www.flickr.com/photos/gnufied/tags/amarok/</a></li>
</ul>
<p style="text-align: center;"><img class="aligncenter" src="http://farm4.static.flickr.com/3516/3219929392_a0c89eb7e8_o.png" alt="" width="600" height="480" /></p>
<p style="text-align: center;">
<ul>
<li><strong>UI : </strong>We programmers sometimes get so blinded by our creation that we see nothing past it. The plasma widget in center is exact case of that.  In a media player, meta info about music is not more important than the music itself. Meta information is a nice to have feature and shouldn&#8217;t protrude like it does in Amarok2. I do like lyrics, album info and other semantic information but main thing that I do with my media player is drag songs from collection and drop it in the playlist. Most of the time its just minized to system tray. I do not run music player to impress chicks. Besides that widget poses significant challenges to entire layout. In beta and RC releases, damn thing rarely used to scale with rest of the window. In final version its better but yet above screenshot is living proof of its fragility.</li>
</ul>
<ul>
<li><strong>Scattered playlist control : </strong>Okay even Amarok1.4 wasn&#8217;t a god in this, but it was better. Most of the playlist controls were somewhat closer. Contrast this with Amarok2 where playlist search is in one corner, &lt;play/pause/stop&gt; in another, &lt;clear playlist/add to playlist&gt; somewhere else. More so some of the controls are not at all on the screen or in context menu but are only accessible from main application menu.</li>
</ul>
<p style="text-align: center;"><a href="http://gnufied.org/wp-content/uploads/2009/01/screenshot-amarok.png"><img class="aligncenter" style="border: 0pt none; margin-top: 5px; margin-bottom: 5px;" title="screenshot-amarok" src="http://gnufied.org/wp-content/uploads/2009/01/screenshot-amarok-300x254.png" alt="" width="300" height="254" /></a></p>
<ul>
<li><strong>Amarok Bug tracker : </strong>You come as a well meaning user and want to submit a bug report for Amarok, I would be quite surprised if you can find a link to bug tracker on ( <a href="http://amarok.kde.org/">http://amarok.kde.org/</a> ). But then bug tracking has been traditionally a no-joy operative word in Open Source because of monstrosity called Bugzilla. In this post Aaron Seigo ( <a href="http://aseigo.blogspot.com/2009/01/building-community-around-your-foss.html">http://aseigo.blogspot.com/2009/01/building-community-around-your-foss.html</a> ) talks about lowering the barrier to entry. I have worked with bugzilla in past and user/submitter side of pain is nothing compared to pain the developer goes through. Contrast this with bliss Rails developers are having (<a href="http://rails.lighthouseapp.com/dashboard">http://rails.lighthouseapp.com/dashboard</a> ). Why can&#8217;t this be improved?</li>
</ul>
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2009/01/23/amarok2-and-redefinition-of-awesomeness/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Detect if daemon is really running</title>
		<link>http://gnufied.org/2008/11/02/detect-if-daemon-is-really-running/</link>
		<comments>http://gnufied.org/2008/11/02/detect-if-daemon-is-really-running/#comments</comments>
		<pubDate>Sun, 02 Nov 2008 07:55:13 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
		
		<category><![CDATA[backgroundrb]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gnufied.org/?p=106</guid>
		<description><![CDATA[You know the story, how your daemon die unexpectedly (mongrel,thin,BackgrounDRb) and on restart they complain about existing pid file (and assume its running). For BackgrounDRb we solved this irksome problem in following way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def really_running? pid
  begin
    Process.kill&#40;0,pid&#41;
    true
  rescue Errno::ESRCH
    puts &#34;pid file exists [...]]]></description>
			<content:encoded><![CDATA[<p>You know the story, how your daemon die unexpectedly (mongrel,thin,BackgrounDRb) and on restart they complain about existing pid file (and assume its running). For BackgrounDRb we solved this irksome problem in following way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> really_running? pid
  <span style="color:#9966CC; font-weight:bold;">begin</span>
    <span style="color:#CC00FF; font-weight:bold;">Process</span>.<span style="color:#9900CC;">kill</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span>,pid<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">true</span>
  <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#6666ff; font-weight:bold;">Errno::ESRCH</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;pid file exists but process doesn't seem to be running restarting now&quot;</span>
    <span style="color:#0000FF; font-weight:bold;">false</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> try_restart
  pid = <span style="color:#0000FF; font-weight:bold;">nil</span>
  pid = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>PID_FILE, <span style="color:#996600;">&quot;r&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>pid_handle<span style="color:#006600; font-weight:bold;">|</span> pid_handle.<span style="color:#CC0066; font-weight:bold;">gets</span>.<span style="color:#9900CC;">strip</span>.<span style="color:#CC0066; font-weight:bold;">chomp</span>.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> really_running? pid
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;pid file already exists, exiting...&quot;</span>
    <span style="color:#CC0066; font-weight:bold;">exit</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">-1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Simple, try sending signal &#8220;0&#8243; and if process responds to it, its alive otherwise pid file is stale and daemon can be safely restarted. There could be many more ways to solve this problem, for example to grep output of &#8220;ps aux&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2008/11/02/detect-if-daemon-is-really-running/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Rails debugging warmhole</title>
		<link>http://gnufied.org/2008/08/20/rails-debugging-warmhole/</link>
		<comments>http://gnufied.org/2008/08/20/rails-debugging-warmhole/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 07:35:42 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
		
		<category><![CDATA[rails]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gnufied.org/?p=95</guid>
		<description><![CDATA[Since, we started to scale to multi machine clustering, our rails application was showing one weird problem. We were generating Stock Market Technical charts and caching them in memcache (with 1 hour ttl). During testing we found that, if requests gets routed to mongrel cluster running on other machine, charts aren&#8217;t appearing, which was weird [...]]]></description>
			<content:encoded><![CDATA[<p>Since, we started to scale to multi machine clustering, our rails application was showing one weird problem. We were generating Stock Market Technical charts and caching them in memcache (with 1 hour ttl). During testing we found that, if requests gets routed to mongrel cluster running on other machine, charts aren&#8217;t appearing, which was weird because we had memcache cluster configured properly in &#8220;production.rb&#8221; file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="ruby">memcache_options = <span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#ff3333; font-weight:bold;">:c_threshold</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 10_000,
  <span style="color:#ff3333; font-weight:bold;">:compression</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>,
  <span style="color:#ff3333; font-weight:bold;">:debug</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>,
  <span style="color:#ff3333; font-weight:bold;">:namespace</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'foobar'</span>
  <span style="color:#ff3333; font-weight:bold;">:readonly</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>,
  <span style="color:#ff3333; font-weight:bold;">:urlencode</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
CACHE = MemCache.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>memcache_options<span style="color:#006600; font-weight:bold;">&#41;</span>
CACHE.<span style="color:#9900CC;">servers</span> = SIMPLE_CONFIG_FILE<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;memcache_servers&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></td></tr></table></div>

<p>Where SIMPLE_CONFIG_FILE['memcache_servers'] contains list of memcache clusters participating in clustering. After, debugging for few hours(*gasp*) and turning on verbose logging on all participating memcache clusters, I found that, trusty <a href="http://errtheblog.com/posts/57-kickin-ass-w-cachefu"> CacheFu </a>, was replacing the CACHE constant with following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby">silence_warnings <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">const_set</span> <span style="color:#ff3333; font-weight:bold;">:CACHE</span>, memcache_klass.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>config<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">const_set</span> <span style="color:#ff3333; font-weight:bold;">:SESSION_CACHE</span>, memcache_klass.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>config<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> config<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:session_servers</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
CACHE.<span style="color:#9900CC;">servers</span> = <span style="color:#CC0066; font-weight:bold;">Array</span><span style="color:#006600; font-weight:bold;">&#40;</span>config.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:servers</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
SESSION_CACHE.<span style="color:#9900CC;">servers</span> = <span style="color:#CC0066; font-weight:bold;">Array</span><span style="color:#006600; font-weight:bold;">&#40;</span>config<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:session_servers</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> config<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:session_servers</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></td></tr></table></div>

<p>Now this deal is real (and sucks). After couple of minutes of hacking, I took out cache_fu config file out of svn,wrote code to generate it on the fly during deployment. Now, pigs can fly.</p>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2008/08/20/rails-debugging-warmhole/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Screencasting from Linux/Ubuntu</title>
		<link>http://gnufied.org/2008/07/14/screencasting-from-linuxubuntu/</link>
		<comments>http://gnufied.org/2008/07/14/screencasting-from-linuxubuntu/#comments</comments>
		<pubDate>Mon, 14 Jul 2008 19:08:59 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
		
		<category><![CDATA[linux]]></category>

		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://gnufied.org/2008/07/14/screencasting-from-linuxubuntu/</guid>
		<description><![CDATA[Sorry for yet another do this - do that, crap of screencasting in Linux. Well, most of the tools out there simply doesn&#8217;t work (yeah, I have tried plenty of them).
Finally, I have settled down on ffmpeg, which works perfectly well for screencasts with both audio and video.
Here are step by step instructions on How [...]]]></description>
			<content:encoded><![CDATA[<p>Sorry for yet another do this - do that, crap of screencasting in Linux. Well, most of the tools out there simply doesn&#8217;t work (yeah, I have tried plenty of them).</p>
<p>Finally, I have settled down on ffmpeg, which works perfectly well for screencasts with both audio and video.</p>
<p>Here are step by step instructions on How to make Screencasts in Linux(Ubuntu 8.04 in my case, but you get the idea) using ffmpeg.</p>
<ul>
<li>  Get ffmpeg dependencis :

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #c20cb9; font-weight: bold;">sudo</span> aptitude <span style="color: #c20cb9; font-weight: bold;">install</span> build-essential subversion zlib1g-dev \
  checkinstall libgpac-dev libfaad-dev libfaac-dev liblame-dev \
  libtheora-dev libvorbis-dev gpac</pre></div></div>

</li>
<li> Get X11 dependencies :

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #c20cb9; font-weight: bold;">sudo</span> aptitude <span style="color: #c20cb9; font-weight: bold;">install</span> libx11-dev xlibs-static-dev \
  x11proto-input-dev</pre></div></div>

</li>
<li> Get ffmpeg code from svn, compile and install:

<div class="wp_syntax"><div class="code"><pre class="bash">svn checkout svn:<span style="color: #000000; font-weight: bold;">//</span>svn.mplayerhq.hu<span style="color: #000000; font-weight: bold;">/</span>ffmpeg<span style="color: #000000; font-weight: bold;">/</span>trunk ffmpeg
<span style="color: #7a0874; font-weight: bold;">cd</span> ffmpeg
.<span style="color: #000000; font-weight: bold;">/</span>configure --<span style="color: #007800;">prefix=</span><span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>ffmpeg --enable-gpl --enable-postproc \
  --enable-libvorbis --enable-libtheora --disable-debug \
  --enable-libmp3lame --enable-libfaad --enable-libfaac \
  --enable-pthreads --enable-x11grab
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

</li>
<li> Record high quality screencasts with:

<div class="wp_syntax"><div class="code"><pre class="bash">ffmpeg -f oss -i <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>dsp -f x11grab -s 1024x768 -r ntsc \
  -sameq -i :<span style="color: #000000;">0.0</span> foo.avi</pre></div></div>

</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2008/07/14/screencasting-from-linuxubuntu/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Making Ruby Bacon play with Mocha</title>
		<link>http://gnufied.org/2008/06/12/making-ruby-bacon-play-with-mocha/</link>
		<comments>http://gnufied.org/2008/06/12/making-ruby-bacon-play-with-mocha/#comments</comments>
		<pubDate>Thu, 12 Jun 2008 21:01:24 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
		
		<category><![CDATA[rails]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gnufied.org/2008/06/12/making-ruby-bacon-play-with-mocha/</guid>
		<description><![CDATA[This post is not about pork and coffee. So, stay clear, if google has landed you here thinking I am going to describe some sort of recipe for making nice mocha coffee with chunky bacons.
Its about using shiny new testing library called Bacon by Chris. Mocha is of course, venerable mocking library for Ruby and [...]]]></description>
			<content:encoded><![CDATA[<p>This post is not about pork and coffee. So, stay clear, if google has landed you here thinking I am going to describe some sort of recipe for making nice mocha coffee with chunky bacons.</p>
<p>Its about using shiny new testing library called <a href="http://bacon.rubyforge.org" target="_blank">Bacon</a> by Chris. <a href="http://mocha.rubyforge.org" target="_blank">Mocha</a> is of course, venerable mocking library for Ruby and Rails.Here is a tiny bit of code that will make you started with bacon and mocha:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="ruby"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;rubygems&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;bacon&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;mocha/standalone&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;mocha/object&quot;</span>
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">Bacon::Context</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Mocha::Standalone</span>
  alias_method <span style="color:#ff3333; font-weight:bold;">:old_it</span>,:it
  <span style="color:#9966CC; font-weight:bold;">def</span> it description,<span style="color:#006600; font-weight:bold;">&amp;</span>block
    mocha_setup
    old_it<span style="color:#006600; font-weight:bold;">&#40;</span>description,<span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    mocha_verify
    mocha_teardown
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Thats it. Happy baking.</p>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2008/06/12/making-ruby-bacon-play-with-mocha/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Unthreaded threads of hobbiton</title>
		<link>http://gnufied.org/2008/06/12/unthreaded-threads-of-hobbiton/</link>
		<comments>http://gnufied.org/2008/06/12/unthreaded-threads-of-hobbiton/#comments</comments>
		<pubDate>Thu, 12 Jun 2008 19:46:20 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
		
		<category><![CDATA[backgroundrb]]></category>

		<category><![CDATA[rails]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gnufied.org/2008/06/12/unthreaded-threads-of-hobbiton/</guid>
		<description><![CDATA[Update: With 1.0.4 release, this method has been removed. It was introduced as a workaround for thread unsafe register_status, but its no longer required, since result caching is anyway threadsafe in this version.
You know the story too well, in your BackgrounDRb worker, you want to run 10 tasks concurrently using thread pool and collect the [...]]]></description>
			<content:encoded><![CDATA[<p><b>Update:</b> With 1.0.4 release, this method has been removed. It was introduced as a workaround for thread unsafe register_status, but its no longer required, since result caching is anyway threadsafe in this version.</p>
<p>You know the story too well, in your BackgrounDRb worker, you want to run 10 tasks concurrently using thread pool and collect the results back in a instance variable and return it. Now, threads are funny little beasts and simplest of things can easily go out of hand. For example, one of BackgrounDRb users wrote something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="ruby">pages = <span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span>
pages_to_scrape.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>url<span style="color:#006600; font-weight:bold;">|</span>
  thread_pool.<span style="color:#9900CC;">defer</span><span style="color:#006600; font-weight:bold;">&#40;</span>url<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>url<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      <span style="color:#008000; font-style:italic;"># model object performs the scraping</span>
      page = ScrapedPage.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>page.<span style="color:#9900CC;">url</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      pages <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> page
    <span style="color:#9966CC; font-weight:bold;">rescue</span>
      logger.<span style="color:#9900CC;">info</span> <span style="color:#996600;">&quot;page scrape failed&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#0000FF; font-weight:bold;">return</span> pages</pre></td></tr></table></div>

<p>There are many things thats wrong with above code, and one of them is, it modifies a shared variable without acquiring a lock. Remember anything inside thread_pool.defer happens inside a separate thread and hence should be thread safe.</p>
<p>Another use case, will be, say you don&#8217;t want to spawn gazillions of workers and rather within one worker, you want to process requests from n users and save the results back using &#8220;register_status&#8221;  with user_id as identifier. thread_pool.defer is for fire and forget kind of jobs and using register_status within block supplied to thread_pool.defer is dangerous.</p>
<p>Enter thread_pool.fetch_parallely(args,request_proc,response_proc). Lets have an example going. Inside one of your workers:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="ruby">  <span style="color:#9966CC; font-weight:bold;">def</span> barbar args
    request_proc = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>foo<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#CC0066; font-weight:bold;">sleep</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#996600;">&quot;Hello #{foo}&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    callback = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>result<span style="color:#006600; font-weight:bold;">|</span>
      register_status<span style="color:#006600; font-weight:bold;">&#40;</span>result<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    thread_pool.<span style="color:#9900CC;">fetch_parallely</span><span style="color:#006600; font-weight:bold;">&#40;</span>args,request_proc,callback<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>First argument to fetch_parallely is some data argument that will be simply passed to request_proc. Note that, its necessary and you should not rely on the fact that a closure captures the local scope. Within threads, it could be dangerous. Now, last return value of request_proc will be passed as an argument to callback proc, when request_proc finishes execution and is ready with result.</p>
<p>The difference here is, although request_proc runs within new thread, calback proc gets executed within main thread and hence need not be thread safe. You can use pretty much do anything you want within callback proc. </p>
<p>This is available in git version of BackgrounDRb. Here is the link on, how to install git version of BackgrounDRb.</p>
<p><a href="http://gnufied.org/2008/05/21/bleeding-edge-version-of-backgroundrb-for-better-memory-usage/">http://gnufied.org/2008/05/21/bleeding-edge-version-of-backgroundrb-for-better-memory-usage/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2008/06/12/unthreaded-threads-of-hobbiton/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
