<?xml version="1.0" encoding="UTF-8"?>
<post>
  <content>&lt;p&gt;In a project I'm working on, when a report was published it would generate a PDF (using the very convenient &lt;a href=&quot;http://prawn.majesticseacreature.com/&quot;&gt;Prawn&lt;/a&gt;) and then send that PDF to half a dozen subscribers. This took a while (around 5 seconds) and made the response to the user a little slow. Actually, it was even worse than that. If I sent the PDF to 5 subscribers then I generated the PDF 5 times (and again each time it was viewed online).&lt;/p&gt;

&lt;p&gt;My first step was to write the PDF out to the local filesystem so that I only ever had to generate it once. For further peace of mind, I also copied the PDF to S3.&lt;/p&gt;

&lt;p&gt;I then chose &lt;a href=&quot;http://github.com/collectiveidea/delayed_job&quot;&gt;Delayed Job&lt;/a&gt; (the CollectiveIdea fork) to start processing everything in the background. Now, when a report is published then the a job is created and the user gets a snappy response.&lt;/p&gt;

&lt;p&gt;Following advice from Ryan Bates (&lt;a href=&quot;http://railscasts.com/episodes/171-delayed-job&quot;&gt;Railscast 171&lt;/a&gt;) I started to think about what to do if the PDF publiction job failed half way through. The risk here is that if you email 5 people and the fifth one fails then the whole job fails and it is rerun a bit later. This would mean repeated emails to the first 4. My first plan was a new table that recorded which PDF had been sent to whom - then it occurred to me that I could just 'spawn' new jobs for each separate email. That way, only the failed email gets reattempted. So now I have a primary job that gets run which generates the PDF and then creates a handful of other jobs that actually send the emails out. Nice.&lt;/p&gt;

&lt;p&gt;P.S. Exactly one year since my last post and its title is &quot;Delayed Job&quot;. How appropriate.&lt;/p&gt;</content>
  <created-at type="datetime">2009-11-30T02:49:07+00:00</created-at>
  <excerpt>Using the delayed_job Rails plugin for background tasks, sending emails. I love it when code gets simpler.</excerpt>
  <id type="integer">18</id>
  <image>/home/logical/rails/shared/images/post/image/18/rails.png</image>
  <pub-date type="date">2009-11-30</pub-date>
  <title>Delayed Job</title>
  <updated-at type="datetime">2009-11-30T02:50:56+00:00</updated-at>
  <user-id type="integer">1</user-id>
</post>
