Dealing with MySQL Gone Away in Zend Framework

I wrote recently about having gearman in my application, however I have been seeing problems with the long-running PHP worker scripts. My logs had entries like this:

SQLSTATE[HY000]: General error: 2006 MySQL server has gone away

The worker is a Zend Framework application, run from the CLI, and it seemed like the Zend_Db_Adapter had no way of knowing when MySQL had let go of its end of the connection. I tried a few different things, including Zend_Db_Adapter::getConnection(), but without success – until I dug through the source code (with some help from a friend) and realised that ZF was not reconnecting at all if it thought it already had a connection. So instead, I expressly disconnected and reconnected the database handler. At bootstrap time, I place my database handle into the registry, so I simply added this at the start of the actual function that the gearman worker calls:

$db = Zend_Registry::get('db');
$db->getConnection();

At the end of my script, before it returns to the loop waiting for another gearman job, I just disconnect my database:

$db->closeConnection();

Now Zend_Db_Adapter knows that when I ask it to connect, it needs to go off and make a new connection, and everything works really well! I was seeing the errors because I’m still only testing the system so it can go days between getting any new jobs, and the timeout on MySQL is shorter than that.

A Tale of Two User Groups

This post is probably only relevant if you’re interested in PHP and UK-based. In the next few weeks I’ll be at some user groups that I don’t manage to visit often. On Thursday, 3rd March, I’ll be at PHP East Midlands to talk about Design Patterns. On Tuesday, 12th April, I’ll be at PHP West Midlands, also speaking but this time about OAuth. I may make it to one or other of PHPNW and LeedsPHP user groups in that time as well … and all of those are within 2 hours’ driving of my home! PHP is alive and well where I live, my great respect and thanks goes to all the community leaders who make these groups happen – thank you all :)

PHP Days and PHP Unconference Europe

I spend most of the last week or so over in Manchester for a combination of excellent PHP-related events: PHP Unconference Europe on Saturday and Sunday (with a rather excellent warm-up party on Friday night!), followed by the PHP Days training on Monday and Tuesday.

PHP Unconference Europe

A large crowd gathered very early on Saturday morning at the Pitcher and Piano, and got our briefing on how session voting works (everyone gets 4 stickers, you stick the stickers onto the sessions you want to see, the ones with the most get scheduled).
DSCF4430.JPG

Continue reading

Mercurial Primer

I am a source control nut, I’ve been speaking about Subversion for years, I co-lead an active open source project which uses git and GtiHub, and I’ve also dabbled with Bazaar. So far I’m feeling the limits of Subversion, loving the code-hosting features of some of the DVCS tools, and hating git and github in equal measure (don’t bother to try to talk me out of this, I’m well aware the problems are mostly on my side). I don’t know anyone else using Bazaar in the PHP community but I do know quite a few people raving about Mercurial, or Hg. This post is a quick introduction to the commands I have been using since I started trying out Hg and BitBucket for myself.

Continue reading

Using Gearman from PHP

I’ve introduced Gearman into a project I’m working on, and since a few people have asked I thought I’d share my experiences. Basically, this application generates some PDFs from a variety of data sources, makes images, and emails it. Since the whole data processing, image handling, PDF generation process is fairly heavy, I’m putting the requests to generate these onto a gearman queue and having some workers process the jobs. The eventual aim is to bring up EC2 instances (or php-specific cloud hosting perhaps? Recommendations gratefully received!) to do this work but right now I have one worker and it’s all on one server.

Continue reading

Callbacks in PHP

Recently I was working on something and I wanted to call an object method as a callback, but got confused when I realised the method had been caused statically. This was caused by my inability to RTFM and I wondered how I’d come so far without actually coming across the many and varied things you can pass into any place a callback is needed. I think we’ve all seen a function callback; something like this:

function myExcitingFunction() {
  // do something remarkable
}

call_user_func('myExcitingFunction');

You can also call methods of objects rather than just plain functions, and this is where I tripped myself up.
Continue reading

Ticket Giveaway: DC4D:2

Did you know that there’s a second edition of the DayCamp 4 Developers event coming up in March? The daycamps are a chance for developers to invest a day in their careers, wherever they are, focussing on the soft skills we need to grow beyond coding monkeys and into accomplished and upwardly-mobile professionals. It’s a virtual conference, so you can join us from anywhere in the world!

In the new edition of DC4D, I’m giving a session entitled “Could You Telecommute?”. I have worked from home for three years and if there’s one thing I’ve learned along the way, it’s that it isn’t always easy! Telecommuting doesn’t suit everyone so if you think you’d like to work this way one day, then I hope to give some pointers for how to tell if it will work out, or how to make it work for you. The event is on March 5th but there are also video-only tickets for those people who would rather watch their sessions back at their own pace.

I have a ticket to give away, so if you want to be my guest, leave me a comment and tell me why I should choose you! NB the tickets are only $35 so this isn’t quite as impressive as it might sound, sorry!

I’ll pick winners on 26th February, with a week to go to the event.

Book Review: The Passionate Programmer


I’ve been putting off writing this post, because I wasn’t sure I could do the book justice, but I read and really enjoyed “The Passionate Programmer” last summer, and I’ve been dipping into it again and again ever since. The book was actually a recommendation from Travis Swicegood, after he saw me give my talk Open Source Your Career. It seems like it’s not a well-known title so I thought I’d share my thoughts on the book and what I got from it.
Continue reading

Installing Gearman for PHP and Ubuntu

I’ve been using Gearman lately in a project that I’m working on, and of course a month later when I came to deploy the code, I had to look up all over again what was required for a gearman server in order to put it on the new platform. Here is the short version for my future reference (and yours, if you like)

Continue reading

Why I Love Unconferences

I’m seeing increasing numbers of unconferences popping up and I must say I’m quite enjoying them. Last year I went to OggCamp, we included an unconference at DPC, and now there’s a PHP-specific event coming up in Manchester: PHP Unconference Europe or phpuceu. I really like unconferences but I think sometimes people don’t know what to expect, so here’s an outline.

Continue reading