<?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>dev.fuzzee.co.uk</title>
	<atom:link href="http://dev.fuzzee.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://dev.fuzzee.co.uk</link>
	<description>stuff that pops into my head ... technical stuff.</description>
	<lastBuildDate>Mon, 23 May 2011 20:43:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Android SDK on 64bit Fedora 14</title>
		<link>http://dev.fuzzee.co.uk/2011/05/android-sdk-on-64bit-fedora-14/</link>
		<comments>http://dev.fuzzee.co.uk/2011/05/android-sdk-on-64bit-fedora-14/#comments</comments>
		<pubDate>Mon, 23 May 2011 20:40:53 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[fedora]]></category>

		<guid isPermaLink="false">http://dev.fuzzee.co.uk/?p=151</guid>
		<description><![CDATA[If you&#8217;re half way through setting up Android SDK on Fedora 14 (and I imagine all other versions apply) on a 64bit install you&#8217;ll get some fairly vague messages back from Ant. Fortunately, they&#8217;re quite easy to fix once you know how. Here&#8217;s the message reported by Ant: Execute failed: java.io.IOException: Cannot run program "/home/adrian/Android/android-sdk-linux_x86/platform-tools/aapt": [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re half way through setting up Android SDK on Fedora 14 (and I imagine all other versions apply) on a 64bit install you&#8217;ll get some fairly vague messages back from Ant. Fortunately, they&#8217;re quite easy to fix once you know how.<br />
<span id="more-151"></span><br />
Here&#8217;s the message reported by Ant:<br />
<code>Execute failed: java.io.IOException: Cannot run program "/home/adrian/Android/android-sdk-linux_x86/platform-tools/aapt": java.io.IOException: error=2, No such file or directory</code></p>
<p>When you actually try and run aapt you might get an even scarier looking error:<br />
<code>bash: /home/adrian/Android/android-sdk-linux_x86/platform-tools/aapt: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory</code></p>
<p>I had a little bit of a panic before I realised that there is officially no 64bit version of the android SDK. After some quick googling, it turns out you need to install some 32bit compat libraries. This process has been documented over on the <a title="HOWTO Setup Android Development" href="https://fedoraproject.org/wiki/User:Hpejakle/Android" target="_blank">Fedora Project Wiki</a>, but I&#8217;ve posted it here in case that page changes:</p>
<p><code>yum install glibc.i686 glibc-devel.i686 libstdc++.i686 zlib-devel.i686 ncurses-devel.i686 libX11-devel.i686 libXrender.i686 libXrandr.i68</code></p>
]]></content:encoded>
			<wfw:commentRss>http://dev.fuzzee.co.uk/2011/05/android-sdk-on-64bit-fedora-14/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>wordpress rewrite rules for lighttpd</title>
		<link>http://dev.fuzzee.co.uk/2011/04/wordpress-rewrite-rules-for-lighttpd/</link>
		<comments>http://dev.fuzzee.co.uk/2011/04/wordpress-rewrite-rules-for-lighttpd/#comments</comments>
		<pubDate>Wed, 06 Apr 2011 22:26:23 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://dev.fuzzee.co.uk/?p=140</guid>
		<description><![CDATA[I recently tried to tidy up my hand crafted rewrite rules for wordpress/lighttpd and after a very quick google I was taken to an article titled &#8220;URL rewriting for wordpress and lighttpd&#8220;. Clearly, I thought my search was over. However, I couldn&#8217;t help but notice that one of my previous ramblings PHP was resulting in [...]]]></description>
			<content:encoded><![CDATA[<p>I recently tried to tidy up my hand crafted rewrite rules for wordpress/lighttpd and after a very quick google I was taken to an article titled &#8220;<a href="http://emil.haukeland.name/webservers/2010/url-rewriting-for-wordpress-and-lighttpd/">URL rewriting for wordpress and lighttpd</a>&#8220;. Clearly, I thought my search was over. However, I couldn&#8217;t help but notice that one of my previous ramblings <a href="http://dev.fuzzee.co.uk/2009/09/avoiding-temporary-array-assignment-in-php/">PHP</a> was resulting in a blank page being rendered.</p>
<p><span id="more-140"></span>There was nothing in the error log (although I&#8217;m still twiddling with php-fpm and lighttpd) but I realised that my post title (and URL) ended with &#8220;php&#8221;. Here&#8217;s the rewrite rule:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">  url.rewrite-final <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>
    # Exclude some directories from rewriting
    <span style="color: #ff0000;">&quot;^/(wp-admin|wp-includes|wp-content|gallery2)/(.*)&quot;</span> <span style="color: #66cc66;">=&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff0000;">&quot;$0&quot;</span>,
    # Exclude .php files at root from rewriting
    <span style="color: #ff0000;">&quot;^/(.*.php)&quot;</span> <span style="color: #66cc66;">=&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff0000;">&quot;$0&quot;</span>,
    # Handle permalinks <span style="color: #b1b100;">and</span> feeds
    <span style="color: #ff0000;">&quot;^/(.*)$&quot;</span> <span style="color: #66cc66;">=&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff0000;">&quot;/index.php/$1&quot;</span>
  <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>But the problem is with:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #ff0000;">&quot;^/(.*.php)&quot;</span> <span style="color: #66cc66;">=&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff0000;">&quot;$0&quot;</span>,</pre></div></div>

<p>The single unescaped &#8220;.&#8221; next to &#8220;php&#8221; will translate to &#8220;any character, followed by php&#8221;. What we need is:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #ff0000;">&quot;^/(.*<span style="color: #000099; font-weight: bold;">\.</span>php)&quot;</span> <span style="color: #66cc66;">=&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff0000;">&quot;$0&quot;</span>,</pre></div></div>

<p>But, as fate would have it, I only found this out because one of my post titles ended with the word &#8220;php&#8221;. Filed under coincidence.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.fuzzee.co.uk/2011/04/wordpress-rewrite-rules-for-lighttpd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Combining Zend_Db_Select and Propel 1.3</title>
		<link>http://dev.fuzzee.co.uk/2010/04/combining-zend_db_select-and-propel-1-3/</link>
		<comments>http://dev.fuzzee.co.uk/2010/04/combining-zend_db_select-and-propel-1-3/#comments</comments>
		<pubDate>Sun, 18 Apr 2010 13:39:16 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[propel]]></category>
		<category><![CDATA[zend-db]]></category>
		<category><![CDATA[zend-framework]]></category>

		<guid isPermaLink="false">http://dev.fuzzee.co.uk/?p=112</guid>
		<description><![CDATA[Propel 1.3 has served me very well over the past few years and I&#8217;m absolutely confident that recent projects would not have been delivered so quickly without it. However, when stuff gets a little crazy, you need to hydrate your objects using hand-crafted SQL. Building up a SQL in a variable and passing it between [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.propelorm.org">Propel</a> 1.3 has served me very well over the past few years and I&#8217;m absolutely confident that recent projects would not have been delivered so quickly without it.  However, when stuff gets a little crazy, you need to hydrate your objects using hand-crafted SQL. Building up a SQL in a variable and passing it between functions can be a nightmare to get right. Since I was already using Zend Framework, I decided to try and get Propel objects back from a Zend_Db_Select generated query. The <a href="http://www.propelorm.org/wiki/Documentation/1.5/ModelCriteria#RelationalAPI">Relational API</a> in the upcoming 1.5 release of Propel would make this possible without the need for hacks and workarounds&#8230;</p>
<p><span id="more-112"></span></p>
<h2 id="toc-the-quick-fix">The quick fix</h2>
<p>The quickest way to combine the two is to use something like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">BookPeer<span style="color: #339933;">::</span><span style="color: #004000;">populateObjects</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$select</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Books'</span><span style="color: #009900;">&#41;</span>
                                 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'author = ?'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'foo'</span><span style="color: #009900;">&#41;</span>
                                 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">order</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title ASC'</span><span style="color: #009900;">&#41;</span>
                                 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                                 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDriverStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The magic part is the call to &#8220;getDriverStatement&#8221; which returns the PDOStatement composed by Zend_Db_Statement_Pdo. The only downside to this is that the query will be executed by Zend_Db&#8217;s connection to the database, rather than Propel&#8217;s. If you can live with that then you may stop reading. If, like me, this sort of negligent waste of resources keeps you awake at night, then you need a rather unsightly hack.</p>
<h2 id="toc-sleepless-nights">Sleepless nights</h2>
<p>Zend_Db doesn&#8217;t support any kind of &#8220;setConnection&#8221; method, where you pass in a PDO object. While Propel <strong>does</strong> support this, it only accepts an instance of PropelPDO. Rather than extending Propel, I thought the easiest way around this would be to create a Zend_Db adapter which, upon calling getConnection(), simply delegates back to Propel::getConnection().</p>
<p>The new adapter required a little hacking to get it right. There are a few methods to castrate because, understandably, the abstract adapter does a lot of checking to make sure it&#8217;s configured correctly. Instead, we&#8217;re using a PDO connection, configured elsewhere so any configuration parameters would simply get in the way.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> App_Db_Adapter_Propel_Mysql <span style="color: #000000; font-weight: bold;">extends</span> Zend_Db_Adapter_Pdo_Mysql <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// configuration-less construction</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getConnection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connection<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #666666; font-style: italic;">// always use Propel's connection</span>
   <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> _connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connection<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connection <span style="color: #339933;">=</span> Propel<span style="color: #339933;">::</span><span style="color: #004000;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #666666; font-style: italic;">// allow for no parameters to the constructor</span>
   <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> __checkRequiredOptions<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we can use the Zend_Db factory (or, because the new adapter does not require any configuration, you could technically just instantiate it directly) to get our new adapter. The select object will use that adapter and we&#8217;ve avoided duplicate db connection attempts.</p>
<h2 id="toc-the-finished-article">The finished article</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'adapterNamespace'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'App_Db_Adapter'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$select</span> <span style="color: #339933;">=</span> Zend_Db<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Propel_Mysql'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
BookPeer<span style="color: #339933;">::</span><span style="color: #004000;">populateObjects</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$select</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Books'</span><span style="color: #009900;">&#41;</span>
                                 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'author = ?'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'foo'</span><span style="color: #009900;">&#41;</span>
                                 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">order</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title ASC'</span><span style="color: #009900;">&#41;</span>
                                 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                                 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDriverStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://dev.fuzzee.co.uk/2010/04/combining-zend_db_select-and-propel-1-3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Avoiding temporary array assignment in PHP</title>
		<link>http://dev.fuzzee.co.uk/2009/09/avoiding-temporary-array-assignment-in-php/</link>
		<comments>http://dev.fuzzee.co.uk/2009/09/avoiding-temporary-array-assignment-in-php/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 20:09:29 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[brainfart]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[zend-framework]]></category>

		<guid isPermaLink="false">http://dev.fuzzee.co.uk/?p=102</guid>
		<description><![CDATA[Even though I&#8217;ve been developing with PHP for a number of years, I still enjoy discovering new ways of working. Even though the stdClass object is about as basic as you can get, it&#8217;s never struck me as being a worthwhile tool. Today, I was storing an associative array of objects in a Zend_Registry. Because [...]]]></description>
			<content:encoded><![CDATA[<p>Even though I&#8217;ve been developing with PHP for a number of years, I still enjoy discovering new ways of working. Even though the stdClass object is about as <strong>basic</strong> as you can get, it&#8217;s never struck me as being a worthwhile tool. <span id="more-102"></span>Today, I was storing an associative array of objects in a <a href="http://framework.zend.com/manual/en/zend.registry.html">Zend_Registry</a>. Because PHP&#8217;s syntax does not support array accessors on method calls, I had to perform a temporary assignation, as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arrayOfObjects</span> <span style="color: #339933;">=</span> Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'stuff'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$object</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$arrayOfObjects</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'key'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$object</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">foo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// of course, I could do:</span>
<span style="color: #666666; font-style: italic;">// $arrayOfObjects['key']-&gt;foo();</span>
<span style="color: #666666; font-style: italic;">// but I still need the temporary assignment</span></pre></div></div>

<p>Because I decided to put a <strong>stdClass</strong> object into the Zend_Registry, the code reads a little more fluently:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'stuff'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">key</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">foo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As far as I&#8217;m aware, there is no significant penalty for using stdClass, but the results are much nicer.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.fuzzee.co.uk/2009/09/avoiding-temporary-array-assignment-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL Replication and disappearing relay logs</title>
		<link>http://dev.fuzzee.co.uk/2009/09/mysql-replication-and-disappearing-relay-logs/</link>
		<comments>http://dev.fuzzee.co.uk/2009/09/mysql-replication-and-disappearing-relay-logs/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 19:52:21 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[replication]]></category>

		<guid isPermaLink="false">http://dev.fuzzee.co.uk/?p=92</guid>
		<description><![CDATA[A recent reboot of a server highlighted an issue with MySQL 5.045 on CentOS 5.3. On this particular setup, MySQL seems to place relay logs in /var/run/mysqld/. Unfortunately, this will mean that when the machine is rebooted, the relay log will get deleted. When MySQL attempts to start up again, it won&#8217;t be able to [...]]]></description>
			<content:encoded><![CDATA[<p>A recent reboot of a server highlighted an issue with MySQL 5.045 on CentOS 5.3. On this particular setup, MySQL seems to place relay logs in <strong>/var/run/mysqld/</strong>. Unfortunately, this will mean that when the machine is rebooted, the relay log will get deleted.<span id="more-92"></span></p>
<p>When MySQL attempts to start up again, it won&#8217;t be able to find the relay log and <strong>replication will fail</strong>. The error messages in /var/log/mysqld.log might look something like:</p>
<pre>[ERROR] Failed to open the relay log '/var/run/mysqld/[relay log filename]'</pre>
<p>The pre-emptive fix is to tell MySQL to store the logs elsewhere (/var/lib/mysql/ perhaps) using the <code>relay-log</code> configuration option. If you&#8217;ve already suffered this problem, then the most straight forward fix is to start again; do a <strong>show master status</strong>, <strong>flush logs</strong> and then a <strong>mysqldump</strong> to get a fresh starting point on the master, and then copy the new dump over to the slave.</p>
<p>More information on  <a href="http://bugs.mysql.com/bug.php?id=28850">the mysql bug report</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.fuzzee.co.uk/2009/09/mysql-replication-and-disappearing-relay-logs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick guide to Linux daemons in PHP</title>
		<link>http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/</link>
		<comments>http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 21:26:04 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[daemon]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[phpnw]]></category>

		<guid isPermaLink="false">http://dev.fuzzee.co.uk/?p=39</guid>
		<description><![CDATA[This guide was originally written for a PHPNW meetup &#8211; more information on the group is available here Whenever we want to carry out lengthy tasks or process large batches of data, we should consider executing that process in the background. The most typical way of doing this is through &#8220;cron jobs&#8221; &#8211; tasks which [...]]]></description>
			<content:encoded><![CDATA[<p><strong>This guide was originally written for a PHPNW meetup &#8211; more information on the group is available <a href="http://www.phpnw.org.uk">here</a></strong></p>
<p>Whenever we want to carry out lengthy tasks or process large batches of data, we should consider executing that process in the background. The most typical way of doing this is through &#8220;cron jobs&#8221; &#8211; tasks which are scheduled to be executed at regular intervals; every 2 minutes, or at 6am every Sunday. Alternatively, you can have the web browser instigate the task by using something like <code>shell_exec()</code>. This guide presents a third alternative, <strong>system level daemons</strong>.<br />
<span id="more-39"></span></p>
<div class="toc">
<ol>
<li><a href="http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/#toc-introduction">Introduction</a></p>
<ol>
<li><a href="http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/#toc-why-is-a-daemon-better-than-a-cron-job">Why is a daemon better than a cron job</a></li>
<li><a href="http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/#toc-why-is-a-daemon-better-than-shell_exec">Why is a daemon better than shell_exec()</a></li>
<li><a href="http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/#toc-single-background-process">Single background process</a></li>
<li><a href="http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/#toc-multiple-background-processes-managed-by-a-parent">Multiple background processes managed by a parent</a></li>
</ol>
</li>
<li><a href="http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/#toc-sample-application">Sample Application</a></li>
<li><a href="http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/#toc-signal-handlers">Signal Handlers</a>
<ol>
<li><a href="http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/#toc-important-tip-for-signals">Important tip for signals</a></li>
</ol>
</li>
<li><a href="http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/#toc-init-d-scripts">Init.d scripts</a></li>
<li><a href="http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/#toc-real-life-examples">Real Life Examples</a>
<ol>
<li><a href="http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/#toc-reporting-system">Reporting System</a></li>
<li><a href="http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/#toc-sms-processing">SMS Processing</a></li>
<li><a href="http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/#toc-co-ordinating-remote-service-requests">Co-ordinating remote service requests</a></li>
</ol>
</li>
<li><a href="http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/#toc-things-to-watch-out-for">Things to watch out for</a></li>
</ol>
</div>
<h2 id="toc-introduction">Introduction</h2>
<p>Put simply, a daemon is a very long running process. Even when it has completed all its tasks, it stays resident, waiting for some work to do. They may spend a lot of their time sleeping, but if designed correctly, they spring to life when required. Of course, they may be constantly listening out for events (by reading a named pipe for example), rather than periodically checking to see if work is available.</p>
<h3 id="toc-why-is-a-daemon-better-than-a-cron-job">Why is a daemon better than a cron job</h3>
<p>For the same reason that web pages are not served by a cronjob running every minute. Daemons are designed to be much more responsive. Cron jobs are very reliable, but sometimes they are less &#8220;elegant&#8221;. </p>
<h3 id="toc-why-is-a-daemon-better-than-shell_exec">Why is a daemon better than shell_exec()</h3>
<p>Irrespective of whether your process is triggered by an HTTP request or some other event, it might not be appropriate to create one process per request. If your process requires considerable resources, then you may end up swamping the server.</p>
<p>Broadly speaking, daemons can come in two flavours:</p>
<ul>
<li>Single background process</li>
<li>Multiple background processes managed by a parent</li>
</ul>
<h3 id="toc-single-background-process">Single background process</h3>
<p>The parent creates a copy of itself (a process known as &#8220;forking&#8221;) and then dies. This leaves the single process running in the background. This type of daemon is very easy to manage and develop. </p>
<h3 id="toc-multiple-background-processes-managed-by-a-parent">Multiple background processes managed by a parent</h3>
<p>The parent creates a child, and then dies. That new child then creates multiple children. Each of these children is a &#8220;worker thread&#8221; and responsible for the heavy lifting. The parent of those children is responsible for distributing their workload amongst them. Apache HTTPd is a prime example of this &#8211; in the configuration you can choose how many children it can create, the minimum number of children to have available, etc.  </p>
<p>Daemons provide a nice balance between the managed rigidity of a cron job while still being responsive.</p>
<h2 id="toc-sample-application">Sample Application</h2>
<p>I have recently found a handy PEAR library for creating PHP daemons &#8211; <a href="http://pear.php.net/package/System_Daemon/">System_Daemon</a>. It greatly simplifies the task of creating a daemon, although the library only supports &#8220;Single background process&#8221; type daemons.</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="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">&quot;System/Daemon.php&quot;</span><span style="color: #339933;">;</span>
System_Daemon<span style="color: #339933;">::</span><span style="color: #004000;">setOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;appName&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;myDaemon&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
System_Daemon<span style="color: #339933;">::</span><span style="color: #004000;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// prevent CPU hogging</span>
   <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mt_rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// simulate a random event which requires handling</span>
      System_Daemon<span style="color: #339933;">::</span><span style="color: #990000;">log</span><span style="color: #009900;">&#40;</span>System_Daemon<span style="color: #339933;">::</span><span style="color: #004000;">LOG_INFO</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Doing something interesting...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This daemon does pretty much nothing, other than stay in the background, slowly adding to a log file. However, it demonstrates that you can have a very &#8220;lightweight&#8221; process listening for system events in only a few lines of code. It&#8217;s also important to have some element of a &#8220;forever&#8221; loop in there to keep the process going.</p>
<h2 id="toc-signal-handlers">Signal Handlers</h2>
<p>Signals are a very basic form of <b>Inter-process communication</b>. In its most common usage, you would send a <b>SIGTERM</b> signal to a process by using the <code>kill</code> command. We can trap <b>SIGTERM</b> signals explicitly and shutdown &#8220;nicely&#8221;. This means any work being carried out can be finished off cleanly, and then the process will terminate. We can trap many other signals, all of which can be sent via &#8220;kill&#8221; on the command line, or more interestingly, the PHP function <b>posix_signal()</b>.</p>
<h4 id="toc-important-tip-for-signals">Important tip for signals</h4>
<p>I discovered this recently : PHP&#8217;s <code>sleep()</code> method is interrupted by signal interrupts. This means you can have a fairly lazy daemon (e.g. sleeps for 5 seconds every interation) and when you want to wake it up, you send it a SIGTERM signal. When interrupted this way, <code>sleep()</code> returns how many seconds were left at the point of interruption.</p>
<p>System_Daemon wraps up <code>pcntl_signal()</code> for us into</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">System_Daemon<span style="color: #339933;">::</span><span style="color: #004000;">setSigHandler</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$signalConstant</span><span style="color: #339933;">,</span> <span style="color: #000088;">$handlerName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2 id="toc-init-d-scripts">Init.d scripts</h2>
<p>The <b>System_Daemon</b> library can generate init.d scripts for us, which means (for me at least) I can and stop/start a daemon with:</p>
<pre>
/sbin/service myDaemon restart
</pre>
<p>Because we studiously assigned a SIGTERM handler, when we ask the daemon to stop using our init.d script, it will shutdown cleanly. </p>
<h2 id="toc-real-life-examples">Real Life Examples</h2>
<h4 id="toc-reporting-system">Reporting System</h4>
<p>Reports can take anywhere between 10 seconds and 3 hours to generate. Requests for report generation were stored in a database and the Reporting Daemon picked them up and processed them. The report was written to disk and made available in a reports area. The system remained responsive irrespective of how many reports were requested. </p>
<h4 id="toc-sms-processing">SMS Processing</h4>
<p>A large, multinational mobile promotions company received hundreds of SMSes a second. In order to be an effective product, the SMS request must have a response as quickly as possible. During promotional periods SMS traffic would sky rocket. Each inbound SMS was written to a database and then awaited processing by the daemon. A similar system was written for outbound SMS messages. The entire SMS platform used about 5 PHP Daemons and was the bread-and-butter of a £40m+ company.</p>
<h4 id="toc-co-ordinating-remote-service-requests">Co-ordinating remote service requests</h4>
<p>On a much smaller scale than the previous two examples, I wrote a simple caching daemon. The daemon would listen out for SIGINT signals (sent by a web page using <code>posix_signal()</code>)  During quiet periods, the daemon would forward requests to the remote service as they came in. During busy periods, the daemon would batch them together. Because the daemon acted as a centralised point for communications, it was the best place to judge work load. For those interested, the web page communicated with the daemon using <b>System V queues</b>, with more information <a href="http://uk.php.net/manual/en/book.sem.php">here</a>.</p>
<h2 id="toc-things-to-watch-out-for">Things to watch out for</h2>
<ul>
<li>The PCNTL library (required for this excercise) is only available when running PHP on the command line (cli mode)</li>
<li>You will soon notice that calls to <code>echo</code> or <code>print</code> are quite distracting when the daemon is running.</li>
<li>Going from personal experience, fatal errors can be hard to track down. Sometimes the process will die with nothing having been written to <code>/var/log/php_error</code>.</li>
<li>Any resources opened before forking, may not be available after the fork (e.g file pointers, db connections). Because pcntl_fork() creates a copy of the current process, when the parent is killed off, all resources are closed. The child, now holding a reference to a closed resource, will trigger an error when that resource is accessed.</li>
<li>Certain resources close after a period of inactivity (I&#8217;m looking at you, MySQL) &#8211; check that your resources are available before using them.</li>
<li>Because the script never shuts down (or shuts down rarely) memory leaks can be problematic.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Centos 5.3 net install on VirtualBox</title>
		<link>http://dev.fuzzee.co.uk/2009/07/centos-5-3-net-install-on-virtualbox/</link>
		<comments>http://dev.fuzzee.co.uk/2009/07/centos-5-3-net-install-on-virtualbox/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 13:03:51 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://dev.fuzzee.co.uk/?p=35</guid>
		<description><![CDATA[I wanted to install CentOS 5.3 as my new development platform (upgrading from 5.2) and I thought I&#8217;d take the lazy route and use the net-install ISO. Although the 5.2 net-install worked flawlessly, the 5.3 failed at the same point time everytime (while downloading &#8220;minstg2.img&#8221;). If you get the following error after having entered your [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to install CentOS 5.3 as my new development platform (upgrading from 5.2) and I thought I&#8217;d take the lazy route and use the <strong>net-install</strong> ISO. Although the 5.2 net-install worked flawlessly, the 5.3 failed at the same point time everytime (while downloading &#8220;minstg2.img&#8221;).</p>
<p><span id="more-35"></span></p>
<p>If you get the following error after having entered your mirror details:
</p>
<p><code>Traceback (most recent call first):<br />
File "/usr/lib/python2.4/subprocess.py", line 975, in _execute_child<br />
raise child_exception<br />
File "/usr/lib/python2.4/subprocess.py", line 542, in __init__<br />
errread, errwrite)<br />
File "/usr/lib/anaconda/iutil.py", line 488, in inVmware<br />
proc = subprocess.Popen(lspci, stdout = subprocess.PIPE)<br />
File "/usr/lib/anaconda/yuminstall.py", line 860, in doGroupSetup<br />
if iutil.inXen() or iutil.inVmware() or \<br />
File "/usr/lib/anaconda/yuminstall.py", line 942, in doRepoSetup<br />
self.doGroupSetup()<br />
File "/usr/lib/anaconda/backend.py", line 172, in doRepoSetup<br />
anaconda.backend.doRepoSetup(anaconda)<br />
File "/usr/lib/anaconda/dispatch.py", line 201, in moveStep<br />
rc = stepFunc(self.anaconda)<br />
File "/usr/lib/anaconda/dispatch.py", line 124, in gotoNext<br />
self.moveStep()<br />
File "/usr/lib/anaconda/text.py", line 721, in run<br />
anaconda.dispatch.gotoNext()<br />
File "/usr/bin/anaconda", line 1006, in ?<br />
anaconda.intf.run(anaconda)<br />
OSError: [Errno 2] No such file or directory</code></p>
<p>Then the fix is to increase the amount of memory available to your VM. 256MB is not enough for a CentOS 5.3 install &#8211; 384MB does the trick. I assume the same applies to any physical, real machine.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.fuzzee.co.uk/2009/07/centos-5-3-net-install-on-virtualbox/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MySQL Replication over SSL</title>
		<link>http://dev.fuzzee.co.uk/2009/06/mysql-replication-over-ssl/</link>
		<comments>http://dev.fuzzee.co.uk/2009/06/mysql-replication-over-ssl/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 17:36:41 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[ssl]]></category>
		<category><![CDATA[stunnel]]></category>

		<guid isPermaLink="false">http://dev.fuzzee.co.uk/?p=9</guid>
		<description><![CDATA[A new project requires MySQL replication to be carried out over the internet. My first thought was to use stunnel, which I quickly implemented. This appeared to work perfectly (after some iptables configuration). However, I found out later that MySQL supports replication over SSL natively. I have made the assumption (perhaps mistakenly) that a native [...]]]></description>
			<content:encoded><![CDATA[<p>A new project requires MySQL replication to be carried out over the internet. My first thought was to use <a href="http://www.stunnel.org">stunnel</a>, which I quickly implemented. This appeared to work perfectly (after some iptables configuration). However, I found out later that MySQL supports replication over SSL natively. I have made the assumption (perhaps mistakenly) that a native solution would be better (more reliable?) than a wrapped solution. Getting MySQL replicating over SSL was perhaps about 30 minutes work in total. This could&#8217;ve been cut down to about 10 minutes if it wasn&#8217;t for some documentation-related issues.<br />
<span id="more-9"></span></p>
<ul>
<li>Read MySQL&#8217;s documentation on <a title="Creating SSL Certificates for MySQL" href="http://dev.mysql.com/doc/refman/5.0/en/secure-create-certs.html">creating SSL certificates</a> &#8211; follow <strong>Example 1</strong>, ignoring the bit about client certificates.</li>
<li>Make sure the slave can connect to the master on port 3306. For the security conscious, I don&#8217;t think this is as bad as it sounds &#8211; we restrict access by IP and  our master MySQL server will require SSL credentials.</li>
<li>Modify your master&#8217;s my.cnf file to include the ssl-* configuration options as per the <a href="http://dev.mysql.com/doc/refman/5.1/en/replication-solutions-ssl.html">Replication over SSL document</a></li>
<li>Copy the master&#8217;s new ca-cert.pem, server-cert.pem and server-key.pem files to your slave machine</li>
<li>This is where I fell over &#8211; specify your certificate/key files in CHANGE MASTER TO as demonstrated <a href="http://dev.mysql.com/doc/refman/5.1/en/replication-solutions-ssl.html">here</a> and <strong>not</strong> in the my.cnf. For one reason or another, only the CHANGE MASTER TO method works.
<li><strong>On the DB Master </strong>:<code><br />
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'slave-ip' IDENTIFIED BY 'slavepass' REQUIRE SSL;<br />
FLUSH PRIVILEGES;<br />
</code></li>
<li><strong>On the DB Slave </strong>: <code><br />
START SLAVE;<br />
SHOW SLAVE STATUS;<br />
</code></li>
</ul>
<p>
   By specifying the certificate/key file paths in the <strong>CHANGE MASTER TO</strong> command, we&#8217;ve made sure that the slave tries to connect over SSL &#8211; this is important because without this, the slave would send user/pass information <strong>in plain text</strong>. By specifying <strong>REQUIRES SSL</strong> in our master&#8217;s <strong>GRANT</strong> we&#8217;ve ensured that account can only be used if the client provides the correct SSL details. Similarly, by specifying the slave-ip for the slave user, we&#8217;ve further secured the master mysql server.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.fuzzee.co.uk/2009/06/mysql-replication-over-ssl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>wordpress, ahoy</title>
		<link>http://dev.fuzzee.co.uk/2009/06/wordpress-ahoy/</link>
		<comments>http://dev.fuzzee.co.uk/2009/06/wordpress-ahoy/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 12:14:11 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://dev.fuzzee.co.uk/?p=3</guid>
		<description><![CDATA[For the past 7 years I have had good intentions for this domain. All I wanted was to fill it with useful stuff. Unfortunately,  I was always busy playing games with work to actually put in enough time to develop a full CMS with useful tools and a nice user interface. Along comes wordpress and [...]]]></description>
			<content:encoded><![CDATA[<p>For the past 7 years I have had good intentions for this domain. All I wanted was to fill it with useful stuff. Unfortunately,  I was always busy <span style="text-decoration: line-through;">playing games</span> with work to actually put in enough time to develop a full CMS with useful tools and a nice user interface.</p>
<p>Along comes <a href="http://www.wordpress.org" target="_blank">wordpress</a> and very deftly puts to shame any half-baked CMS I ever came up with.</p>
<p>Hopefully, wordpress will bolster my rather stop-start blogging career.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.fuzzee.co.uk/2009/06/wordpress-ahoy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

