<?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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PMA Media Group&#187; workling</title>
	<atom:link href="http://www.pmamediagroup.com/tag/workling/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pmamediagroup.com</link>
	<description>Unique Marketing Techniques and Strategies with Guaranteed Results!</description>
	<lastBuildDate>Fri, 27 Aug 2010 18:14:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Asynchronous Processing with Workling and Starling</title>
		<link>http://www.pmamediagroup.com/2009/06/asynchronous-processing-with-workling-and-starling/</link>
		<comments>http://www.pmamediagroup.com/2009/06/asynchronous-processing-with-workling-and-starling/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 14:40:10 +0000</pubDate>
		<dc:creator>fugufish</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[starling]]></category>
		<category><![CDATA[workling]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=897</guid>
		<description><![CDATA[When working with applications whose actions may take some time to complete, it may be better to  handle the request asynchronously. A quick and easy way to do this is using Starling and Workling. Starling is a light weight message queue based on the Memcache protocol, and Workling is a simple, lightweight consumer. Setup is [...]]]></description>
			<content:encoded><![CDATA[<p>When working with applications whose actions may take some time to complete, it may be better to  handle the request asynchronously. A quick and easy way to do this is using <a title="Starling" href="http://github.com/starling/starling/tree/master" target="_blank">Starling</a> and <a title="Workling" href="http://github.com/purzelrakete/workling/tree/master" target="_blank">Workling</a>. Starling is a light weight message queue based on the Memcache protocol, and Workling is a simple, lightweight consumer. Setup is dead simple:</p>
<p>First, install Starling:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"> <span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> starling</pre></div></div>

<p>This will install Starling and it&#8217;s dependencies (memcache-client and eventmachine) if you don&#8217;t already have them.</p>
<p>Now install Workling. This doesn&#8217;t have a gemspec so we will install it as a plugin:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">cd ~<span style="color:#006600; font-weight:bold;">/</span>path_to_your_project
script<span style="color:#006600; font-weight:bold;">/</span>plugin install git:<span style="color:#006600; font-weight:bold;">//</span>github.<span style="color:#9900CC;">com</span><span style="color:#006600; font-weight:bold;">/</span>purzelrakete<span style="color:#006600; font-weight:bold;">/</span>workling.<span style="color:#9900CC;">git</span></pre></div></div>

<p>Finally, tell Workling, which will want to use Spawn by default if it is installed on your machine, to use Starling by placing this in your environment.rb:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">Workling::Remote</span>.<span style="color:#9900CC;">dispatcher</span> = <span style="color:#6666ff; font-weight:bold;">Workling::Remote::Runners::StarlingRunner</span>.<span style="color:#9900CC;">new</span></pre></div></div>

<p>That is it for the installation process! Easy. Now for actually handling requests. Believe it or  not, it is just as simple as the installation. Say you have a controller that has to do several long running tasks:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> SkinnyController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> fat_action
    .. <span style="color:#9966CC; font-weight:bold;">do</span> some crazy stuff that takes a few minutes ..
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now typically, you should avoid doing things that take longer than a few seconds to complete. And this is okay for most application requirements, however in some cases, it is inevitable that a few tasks will take much longer, such as above. That is where Workling comes in. Simply refactor the code into a worker (conveniently located in app/workers):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># app/workers/fat_worker.rb</span>
<span style="color:#9966CC; font-weight:bold;">class</span> FatWorker <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Workling::Base</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># this method can be named anything you want</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> do_work<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    .. <span style="color:#9900CC;">you</span> get the picture ..
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now, in your controller, call the worker:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> SkinnyController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> fat_action
    FatWorker.<span style="color:#9900CC;">asynch_do_work</span><span style="color:#006600; font-weight:bold;">&#40;</span>some_args<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Just start up starling and workling (starling start, and script/workling_client start respectively) And that is all. You can now handle large tasks asynchronously, and because the tasks are queued with starling, the action can be called multiple times, and it will queue up the worker and process it as soon as the previous tasks are complete. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/06/asynchronous-processing-with-workling-and-starling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: www.pmamediagroup.com @ 2012-02-05 03:22:28 -->
