<?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>Dyn Solutions &#187; ruby on rails</title>
	<atom:link href="http://blog.dyn-solutions.com/category/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dyn-solutions.com</link>
	<description>dyn's blog</description>
	<pubDate>Fri, 11 Mar 2011 17:50:47 +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>Rails Delayed Job</title>
		<link>http://blog.dyn-solutions.com/2010/03/rails-delayed-job/</link>
		<comments>http://blog.dyn-solutions.com/2010/03/rails-delayed-job/#comments</comments>
		<pubDate>Thu, 25 Mar 2010 23:44:29 +0000</pubDate>
		<dc:creator>dyn</dc:creator>
		
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://blog.dyn-solutions.com/?p=35</guid>
		<description><![CDATA[When building a web application, you always try to make it fast and responsive. Sometimes this task can be difficult because of some long running requests like: file uploads, sending email, updating sphinx indexes, running reports, etc.
Enter Delayed Job.
First, we must install the plugin and migrate the delayed_job table that handles the work:
1script/plugin install git://github.com/collectiveidea/delayed_job.git
Then [...]]]></description>
			<content:encoded><![CDATA[<p>When building a web application, you always try to make it fast and responsive. Sometimes this task can be difficult because of some long running requests like: file uploads, sending email, updating sphinx indexes, running reports, etc.</p>
<p>Enter <a href="http://github.com/collectiveidea/delayed_job" target="_blank">Delayed Job</a>.</p>
<p>First, we must install the plugin and migrate the delayed_job table that handles the work:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">script/plugin install git://github.com/collectiveidea/delayed_job.git</div></td></tr></tbody></table></div>
<p>Then we create the table:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">script/generate delayed_job<br />
rake db:migrate</div></td></tr></tbody></table></div>
<p>Now we are ready to enqueue jobs. Let&#8217;s say we want to send an email with delayed_job:</p>
<div class="codecolorer-container ruby blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Emailer.<span style="color:#9900CC;">deliver_contact</span><span style="color:#006600; font-weight:bold;">&#40;</span>@comments<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#old way </span><br />
<br />
Emailer.<span style="color:#9900CC;">send_later</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:deliver_contact</span>, <span style="color:#0066ff; font-weight:bold;">@comments</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#delayed_job way</span></div></td></tr></tbody></table></div>
<p>Now you check your email and you can&#8217;t find anything. That&#8217;s because we need to run the delayed_job <em>daemon</em>.</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">script/delayed_job start</div></td></tr></tbody></table></div>
<p>Now you can see how the daemon sends the email. Instead of the send_later method, you can &#8220;mark&#8221; a method to always run in background:</p>
<div class="codecolorer-container ruby blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> Photo<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> generate_thumbnails<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#008000; font-style:italic;">#generate all the thumbnails</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; handle_asynchronously <span style="color:#ff3333; font-weight:bold;">:generate_thumbnails</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#0066ff; font-weight:bold;">@photo</span> = Photo.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:photo</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#0066ff; font-weight:bold;">@photo</span>.<span style="color:#9900CC;">generate_thumbnails</span> <span style="color:#008000; font-style:italic;">#this will run throught delayed_job</span></div></td></tr></tbody></table></div>
<p>Now you need to run delayed_job in your production environment, with  <a href="http://www.capify.org" target="_blank">capistrano</a> you should have something like:</p>
<div class="codecolorer-container ruby blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">namespace <span style="color:#ff3333; font-weight:bold;">:delayed_job</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; desc <span style="color:#996600;">&quot;Start delayed_job process&quot;</span><br />
&nbsp; task <span style="color:#ff3333; font-weight:bold;">:start</span>, <span style="color:#ff3333; font-weight:bold;">:roles</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#ff3333; font-weight:bold;">:app</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; run <span style="color:#996600;">&quot;cd #{current_path}; RAILS_ENV=production script/delayed_job start&quot;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; desc <span style="color:#996600;">&quot;Stop delayed_job process&quot;</span><br />
&nbsp; task <span style="color:#ff3333; font-weight:bold;">:stop</span>, <span style="color:#ff3333; font-weight:bold;">:roles</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#ff3333; font-weight:bold;">:app</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; run <span style="color:#996600;">&quot;cd #{current_path}; RAILS_ENV=production script/delayed_job stop&quot;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; desc <span style="color:#996600;">&quot;Restart delayed_job process&quot;</span><br />
&nbsp; task <span style="color:#ff3333; font-weight:bold;">:restart</span>, <span style="color:#ff3333; font-weight:bold;">:roles</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#ff3333; font-weight:bold;">:app</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; run <span style="color:#996600;">&quot;cd #{current_path}; RAILS_ENV=production script/delayed_job restart&quot;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
after <span style="color:#996600;">&quot;deploy:start&quot;</span>, <span style="color:#996600;">&quot;delayed_job:start&quot;</span><br />
after <span style="color:#996600;">&quot;deploy:stop&quot;</span>, <span style="color:#996600;">&quot;delayed_job:stop&quot;</span><br />
after <span style="color:#996600;">&quot;deploy:restart&quot;</span>, <span style="color:#996600;">&quot;delayed_job:restart&quot;</span></div></td></tr></tbody></table></div>
<p>If there&#8217;s an error when launching the daemon in production, that might be because you need the <strong>daemons</strong> gem. I had problems with the <em>daemons 1.0.11 so</em> I went for the<em> ghazel-daemons</em> and everything worked fine.</p>
<p>We highly recommend to use <a href="http://mmonit.com/monit/" target="_blank">monit</a> to monitor and restart the delayed_job process in case something goes wrong. You can check other options like custom jobs and configuration parameters in the docs.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dyn-solutions.com/2010/03/rails-delayed-job/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

