<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Peter Tellgren]]></title>
  <link href="http://tellgren.com/atom.xml" rel="self"/>
  <link href="http://tellgren.com/"/>
  <updated>2011-10-12T15:26:05+02:00</updated>
  <id>http://tellgren.com/</id>
  <author>
    <name><![CDATA[Peter Tellgren]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Adding dynamic view paths to ActionMailer in Rails 2.3.11]]></title>
    <link href="http://tellgren.com/blog/2011/09/22/adding-view-paths-to-actionmailer/"/>
    <updated>2011-09-22T17:52:21+02:00</updated>
    <id>http://tellgren.com/blog/2011/09/22/adding-view-paths-to-actionmailer</id>
    <content type="html"><![CDATA[<p>When I recently rewrote our app at work into a multi tenant app I ran in to the problem of the action_mailer views. Since the app
started out it&#8217;s life as a single tenant app most of the mails had to be changed but also had to remain the same for the app still there</p>

<p>My first idea was to just configure the template_root of ActionMailer in my new Environment but just after this was done the requirement
came that each tenant might want to customize these views. so instead of changing the template_root depending on the tenant I googled around
and found a solution for adding the nice view_path method we already have for the ActionController.</p>

<p>The original post can be found <a href="http://www.quirkey.com/blog/2008/08/28/actionmailer-hacking-multiple-template-paths/">here</a> but I thought I would show my implementation below. Basically a compilation of the original poster and the comments given
to that post</p>

<figure class='code'><figcaption><span>lib/action_mailer_extensions.rb </span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">ActionMailer</span>
</span><span class='line'>  <span class="k">class</span> <span class="nc">Base</span>
</span><span class='line'>    <span class="n">class_inheritable_accessor</span> <span class="ss">:view_paths</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">prepend_view_path</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
</span><span class='line'>      <span class="n">view_paths</span><span class="o">.</span><span class="n">unshift</span><span class="p">(</span><span class="o">*</span><span class="n">path</span><span class="p">)</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">append_view_path</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
</span><span class='line'>      <span class="n">view_paths</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="o">*</span><span class="n">path</span><span class="p">)</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">def</span> <span class="nf">view_paths</span>
</span><span class='line'>      <span class="nb">self</span><span class="o">.</span><span class="n">class</span><span class="o">.</span><span class="n">view_paths</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>    <span class="kp">private</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">view_paths</span>
</span><span class='line'>      <span class="vc">@@view_paths</span> <span class="o">||=</span> <span class="no">ActionController</span><span class="o">::</span><span class="no">Base</span><span class="o">.</span><span class="n">view_paths</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">def</span> <span class="nf">initialize_template_class_without_helpers</span><span class="p">(</span><span class="n">assigns</span><span class="p">)</span>
</span><span class='line'>      <span class="no">ActionView</span><span class="o">::</span><span class="no">Base</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">view_paths</span><span class="p">,</span> <span class="n">assigns</span><span class="p">,</span> <span class="nb">self</span><span class="p">)</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>I made sure that my lib dir was included in my load paths and then I could mange the view paths as follows</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">ActionMailer</span><span class="o">::</span><span class="no">Base</span><span class="o">.</span><span class="n">prepend_view_path</span><span class="p">(</span><span class="no">RAILS_ROOT</span> <span class="o">+</span> <span class="s1">&#39;/app/views/...&#39;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Trouble with ImageMagick on a VPS]]></title>
    <link href="http://tellgren.com/blog/2011/03/24/trouble-with-imagemagick-on-vps/"/>
    <updated>2011-03-24T17:45:00+01:00</updated>
    <id>http://tellgren.com/blog/2011/03/24/trouble-with-imagemagick-on-vps</id>
    <content type="html"><![CDATA[<p>This week I was adding a galleries to a rails site that I am working on and for some reason on my production server, each time I tried to upload an image it failed and I got returned a 502 response from my server. The thing was that even though I was looking at my logs I saw no request even being made to the server.
I finally got an image to upload and could see in my logs that the action took in excess of  one minute to complete which led my Rails worker (unicorn) to time out and return the aforementioned 502 error response. As I went through the call stack I could determine that the bottleneck was ImageMagick and especially the calls to the convert method.</p>

<p>I did some testing and could confirm what I had determined in the logs:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ time utilities/convert 'image.jpg' -resize "x60" -crop "60x60" +repage 'thumb'
</span><span class='line'>real    0m11.602s
</span><span class='line'>user    0m11.414s
</span><span class='line'>sys  0m0.069s</span></code></pre></td></tr></table></div></figure>


<p><strong>11 seconds to scale and crop an image, that’s just insanly slow!!!</strong></p>

<p>I did some googleing and found rumors that openmp, that is activated by default in ImageMagick and helps to make use of modern CPUs multi core architecture sometimes could cause issues in a virtualized environment wich is exactly what I am running on.</p>

<p>So I went out and got the latest version of the ImageMagick source, compiled it with the —disable-openmp flag and redid the test with this binary:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ time utilities/convert 'image.jpg' -resize "x60" -crop "60x60" +repage 'thumb'
</span><span class='line'>real     0m0.077s
</span><span class='line'>user    0m0.058s
</span><span class='line'>sys     0m0.019s</span></code></pre></td></tr></table></div></figure>


<p>That’s more like it, don’t you agree. All I know had to do was to install this version on to my server. However as I am running Ubuntu 10.04 so I’d like to keep with the package system. For some reason I was not able to get the 10.04 package of ImageMagick to accept the —disable-openmp flag so, in the end I backported the package from Ubuntu 10.10 instead. please find the steps below:</p>

<h3>1. Download the following files:</h3>

<ul>
<li><a href="http://archive.ubuntu.com/ubuntu/pool/main/i/imagemagick/imagemagick_6.6.2.6-1ubuntu1.1.dsc">imagemagick_6.6.2.6-1ubuntu1.1.dsc</a></li>
<li><a href="http://archive.ubuntu.com/ubuntu/pool/main/i/imagemagick/imagemagick_6.6.2.6.orig.tar.bz2">imagemagick_6.6.2.6.orig.tar.bz2</a></li>
<li><a href="http://archive.ubuntu.com/ubuntu/pool/main/i/imagemagick/imagemagick_6.6.2.6-1ubuntu1.1.debian.tar.bz2">imagemagick_6.6.2.6-1ubuntu1.1.debian.tar.bz2</a></li>
</ul>


<h3>2. Unpack</h3>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ dpkg-source -x imagemagick_6.6.2.6-1ubuntu1.1.dsc</span></code></pre></td></tr></table></div></figure>


<h3>3. Edit the build rules</h3>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ cd imagemagick-6.6.2.6
</span><span class='line'>$ vim debian/rules</span></code></pre></td></tr></table></div></figure>


<p>Add the the following line to the ./configure statement on line 25-39. I added mine on line 34.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>34: —disable-openmp \</span></code></pre></td></tr></table></div></figure>


<h3>4. Add dependencies and build ( I needed these dependencies)</h3>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ sudo apt-get install liblqr-1-0-dev librsvg2-dev
</span><span class='line'>$ dpkg-buildpackage -b</span></code></pre></td></tr></table></div></figure>


<h3>5. Out with the old, in with the new</h3>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ sudo apt-get remove —purge imagemagick
</span><span class='line'>$ sudo dpkg -i libmagickcore3_6.6.2.6-1ubuntu1.1_amd64.deb
</span><span class='line'>$ sudo dpkg -i libmagickwand3_6.6.2.6-1ubuntu1.1_amd64.deb
</span><span class='line'>$ sudo dpkg -i imagemagick_6.6.2.6-1ubuntu1.1_amd64.deb</span></code></pre></td></tr></table></div></figure>


<p>So there we are, my gallery is now up and running and preforming as should.</p>
]]></content>
  </entry>
  
</feed>

