Category: General

PHP Design Patterns – Observer Pattern

By , Tuesday 29th December 2009 10:02 pm

I’ve been reading Head First Design Patterns recently and have decided to write some of the patterns as PHP examples for my own benefit. The first one that I’ve decided to code up is the Observer Pattern. The formal definition of the Observer Pattern is:

The observer pattern (a subset of the asynchronous publish/subscribe pattern) is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.

As systems become more loosely coupled making sure that when an event happens all systems that require knowledge of these updates are informed. For example, a blog post, after saving a post we may need to update a search engine (e.g. Lucene), update our sitemap, tags, email subscribed users, etc. The observer pattern allows developers to add additional listeners without editing their observable object. By injecting observers (i.e. a search engine update observer, a sitemap generator, etc) into a subject (i.e. blog post editing system) we can allow the it to perform all the necessary updates without any changes.

Continue reading 'PHP Design Patterns – Observer Pattern'»

Office Grid Computing using Virtual environments – Part 3

By , Friday 4th December 2009 11:37 pm

Introduction

I work in a company where we run many batch jobs processing millions of records of data each day and I’ve been thinking recently about all the machines that sit around each and every day doing nothing for several hours. Wouldn’t it be good if we could use those machines to bolster the processing power of our systems? In this set of articles I’m going to look at the potential benefits of employing an office grid using virtualised environments.

In part 2 we looked at the jobs a server will run, and how jobs should be configured in order to achieve greatest amount of processing whilst ensuring that each job is processed without fail.

Setting up your worker – or LiMP server

The next step in the process is to set up your virtual workers. For this I’m going to use an installation of centOS using VirtualBox. I’m going to install mySQL and PHP on the server, also known as a LiMP (Linux, mySQL, PHP) Server  (I may have made that name up).

  • Install VirtualBox on your windows machine (follow link)
  • Download and install centOS (current version 5.3) within a created virtual machine

There’s no point me going to this there’s probably 1,000′s of great tutorials out there (ok, here’s one: Creating and Managing  centOS virtual machine under virtualbox). The important point to note I suppose is that I called my virtual machine GridMachine.

As far as my choices of virtualisation client and operating system go there is no big compelling reason for each choice. VirtualBox is something I use on my home machine and is supported by the three major operating systems. I chose centOS as its a good stable OS and I use it on my own web server. I am a great believer in the right tools for the job (although I’m applying ‘use the quickest and easiest for you’ mentality here), so if operating system X runs your code quicker and more efficiently use that instead :)

Importantly make sure that your VM uses DHCP, otherwise for each new virtual machine would need to be configured separately which is something we don’t want.By using DHCP we don’t need to configure network settings individually for worker machines, DHCP will hand out IPs for you. Therefore you can copy your virtual machine about the office without worrying about setting each one up (this improves scalability and reduces worker administration).

The process you should aim to achieve would be to obtain a new physical machine, install VirtualBox, and then pretty much deploy the virtual image without much else. It might be wise to setup all your workers on a different subnet so that you can at least see how many machines are running. You’ll also need to set up your machines on a long lease or unlimited lease DHCP.

How to run Jobs on the worker

This is an interesting area and there are several valid methods for processing jobs on the worker. Here I’ll just discuss the two most obvious:

  • Perpetually running script: A script, be it a shell script, or a PHP script is executed once on the worker and runs as part of an infinite loop. I’ve discounted this method as one crash of the script and potentially your workers will cease to run without some sort of intervention.
  • Cron based script execution: Every X minutes the cron daemon kicks off a call to your script to get things going. Without some checking this could lead to many many copies of your worker script running.

My decision was to go with cron which kicks off a shell script every 10 minutes.  My shell script performs the following tasks:

  1. Get a process list and grep this for ‘php’. If not found then continue.
  2. Call your job code, in my case this would be something PHP based
  3. Worker script completes its run
  4. Ready to go again on the next appropriate call

My bash script looks something like the following:

#!/bin/sh
if ps ax | grep -v grep | grep php > /dev/null
then
    echo "Job is currently processing, exit"
else
    echo "Job is not running, start now"
    php yourJobProcessingScript.php
fi

Note: the echo’s are almost completely pointless, but may help the next person who comes along to try and edit them.

That concludes the set up of the worker virtual machine, quick, simple, and easy to copy to each new piece of hardware that is received. The ‘cleverness’ of the grid system really isn’t in the visualised OS, its all to do with the code created to process jobs, the job configuration, and in making sure that the job runs when appropriate (i.e. when the host is idle).

Setting up Windows to Initialise Workers

The first task is to work out the command required to run the virtual machine from the windows command line. If you’ve installed virtualBox in the default location and you’ve named your worker GridMachine then the command required to load up your worker is:

"C:\Program Files\Sun\VirtualBox\VBoxManage.exe" startvm GridMachine

However to run the script in a ‘headless’ state we need to use:

"C:\Program Files\Sun\VirtualBox\VBoxHeadless.exe" -startvm GridMachine --vrdp=off

This will start the virtual machine without the GUI and allow it to save state gracefully. The second argument turns off RDP so it doesn’t conflict with windows RDP, or give you a message about listening on port 3389. The virtual machine name is cAsE sEnSiTiVe!

Next, we’ll need to set windows up to kick off our worker VM once the machine has been idle. To do this (on Windows XP) you’ll need to go Start -> All Programs -> Accessories -> System Tools -> Scheduled Tasks as below:

scheduled tasks

Next click on ‘Add Scheduled Task’ followed by browse to add a custom program. Navigate to your VBoxManage script and click ok. Schedule your task for any of the options (we’ll change this in a minute) and continue. After skipping the next screen windows will ask you who you want to run this task, I’d suggest either ‘Administrator’ or creating a new privileged user. Remember we don’t want to interfere with the standard staff account on the machine at any point. Click next and check show advanced options for this task.

To the end of the run textbox add our ‘startvm GridMachine‘ string and ensure that run only when logged in is left unticked. Visit the schedule task next and change the schedule drop down to the option ‘when idle’, choose the amount of time you’d like the machine to be idle before moving on to the next tab.

Finally untick the option which states stop the task if it has been running X amount of time, but do tick the option to stop the task if the machine is no longer idle.

schedule

That’s it then for the windows host setup!

Summary

In this part we have set up a virtual machine to act as a worker, as well as the way in which we call and execute our job processing scripts (for myself a PHP script). From here we look at how to set up our copies of windows to start up the virtual machine in headless mode when the computer becomes idle, and save its state when the user resumes usage of the machine. Hopefully at this point you’re seeing how simple it is to set up such a system and are itching to get some experiments going yourself!

Next time

In Part 4 we’ll be looking at using tools to ensure that you’re running the latest version of the code and data sources so that obtained results are always up-to-date with the latest business information and logic.

UK Names Directory and Facebook Application

By , Friday 10th April 2009 9:33 am

One of the fun things I’ve been involved in whilst working for Tracesmart is the names directory. Basically we took our huge information database and pulled out a big pile of stats about various names.

The process is all one big SEO effort in order to draw more traffic to the site, but it also provides some interesting information and name statistics.

Here’s an example for my name Lloyd Watkin, or I suppose more correctly Steven Watkin. Take a minute to look up your name on the Tracesmart site, might be interesting ;)

We’ve also got names statistics data on the names directory page which tells you what names have been searched and how recently. Its quite interesting to see famous people’s names fly up the stats when they appear in the news. A recent example is that of Liam Neeson who’s wife died from a skiing accident recently, although obviously people aren’t quite right on the spelling (hence the huge numbers of hits on this name) — Liam Nilson.

Facebook Application

Once the names directory was created we set about creating a facebook application which we duely named ‘My Name‘. The facebook application is an extension of the names directory and provides some additional statistics. In order to add the names directory to your facebook account please visit:

http://apps.facebook.com/my-name/

Flickr and Yahoo!: Forgotten Login Details

By , Thursday 2nd April 2009 7:31 pm

A few months ago I changed my Flickr password so that a friend could upload some shots to it. A couple of months later I find I’ve forgotten that password and trying to remember all my secret details from Yahoo! is just a nightmare. I had my Yahoo! email address for several years and I wasn’t sure when or where (I was living) when I signed up – oh just to make this clearer Flickr uses Yahoo! login system :)

Yahoo! were no help either, they wanted exact details and would reject anything that didn’t match. Plus I wasn’t allowed to provide a list of possible matching details it had to be right on every detail otherwise it would be rejected. I can understand the security but this was getting a pain in the arse, especially as I could retrieve my user name to my usual email address without any trouble (why couldn’t you send a reset there!?!?!). On top of all this they wanted the details sent to their offices in California either by mail or fax, boo!

Step back to Flickr who kindly send an email to my registered email address asking me to detach my Yahoo! account from my Flickr account. Hooray I could then sign up with a new Yahoo! account and reattach my Flickr account to this new login.

After waiting at least a week each time for Yahoo! to reply Flickr got this all sorted within 2-3 hours!

Well done Flickr, thankfully you’re out there looking after your customers :)

Now for everyone’s boredom here’s yet another link to my Flickr account

Inspired… and guilted!

By , Wednesday 11th March 2009 8:51 pm

I’ve recently set up a blog for one of the guys in work – Matthew Hopkins – and seeing his first few posts has made me think that maybe I should add a few posts as its been a good while and lots has happened over 2008. Another one of the guys in work does himself a blog to, good old Stephen Griffiths so take a look there too.

So I’m going to plod on and add some retrospective posts about what 2008 held for me and how 2009 is shaping up :)

2008 WTF?!?

By , Thursday 1st January 2009 12:00 am

2008 was a strange old year, there’s several things I really enjoyed about it,  and there’s some things that have now been forgotten and moved on with….

Diving

I haven’t done much in the way of scuba diving I think I managed around about 10 dives all year. One of those dives was the scuba santas event at Vobster Quay which was more successful than my wildest dreams. In the end we raised over £1000 for the RNLI, got coverage on BBC and ITV, and had around 80 divers in the water. Vobster was a sea of scuba diving santas for the day! I really hope I get to do mroe diving in 2009 (lots more!), getting back into it will involve lots of training to get comfortable again. I miss the days of getting on the boat, sun beaming down, grabbing a lobster, packing up   and heading home for a few beers.

Running

At the end of January 2008 I was involved in a car crash, hit from behind whilst stopped, nothing major or so I thought. Over several months my neck pain got progressively worse stopping me running. In the end I contacted an accident claims company who started getting me treatment with a chiropractor in Cardiff which I’m still attending (March 2009 – the claim still isn’t settled either). Running is another one of those things I want to get back to, I did start with Miles but between illnesses, injuries, and him eventually moving to Cambridge we only got out a handful of times. I really need a good kick up the bum to get going again, although my one consolation is that I have been cycling back and fore to work for months which is doing me some good.

Moved

In July 2008 I moved into a flat with Megan, we’re getting through the painting and decorating and getting it looking really nice  – although we’re sort of dragging our heels with the painting….still. The flat is in Adamsdown in Cardiff (I never seem to move far) and is suiting us just great.

Work

Work was all over the place in 2008, I started off drifting a little before taking up work at a letting agent as manager. This helped no end with the estate and letting agency software that I was developing. My plan for the estate and letting agency software is to release it as a low cost product when I have more time to clear it up. From what I’m told about competing products on the market it is easier to use and performs many or more of the same functions in a better way :)

During April 2008 coming up to my 22nd birthday both Megan and my mother started pushing for me to ‘get a career’, which a hard idea to swallow was really what I needed. Running my own company was fun but didn’t provide the stability needed and earning a pittance at the letting agency wasn’t a long term career goal. It was always a case of wait until I get my software finished and I’ll start selling it, but time was always against me.

What followed was several interviews with some great companies in some great areas, full of promise of moving to a new area, fresh start seemed great. The best job offer was with a large hosting provider in the Worcester area and seemed ideal. So what happened? Well I had developed an online SMS solution back around christmas that had been sitting around not doing much which investors were found for. Excellent I thought and off we went. All didn’t end well and after several months of broken promises and rising stress levels I made a stand and walked out. Working 60 hours a week, coming home and doing another 3-4 hours plus working all weekend and for all the stress it was one of the best decisions I’ve ever made. (Here I’d like to thank Megan, my parents, and my friends that stood by me, I was a stress-headed dick at times, and I’m sorry)

Four weeks later I landed myself a job with a company called Tracesmart in Cardiff Bay as a PHP Developer. After passing the tests and the interviews I started with a whole pile of enthusiasm (as you can imagine things had been a bit low for sometime). Tracesmart perform people searching, people tracing, electoral roll data handling, involving huge quantities of data. I’ve been there now for about 5 months (given I’m actually writing this in March 2009) and I love every minute of it. I’m pushing myself every day, have a great working environment (my desk overlooks the bay!), good colleagues! I’ve been involved in some good projects (other posts to follow) and I’m really proud of the work I’m doing there.

2009….

Despite starting 2009 in Germany suffering from the Novo virus 2009 is great so far. Megan and I spent  New Years time in Germany and Amsterdam. Amsterdam is beautiful and the Anne Frank house had a real profound effect on me, somewhere we’ll definately be returning to. Work is going really well. My neck is getting better and better so in a couple of weeks time I’ll head out for those first few awful runs once again. The only thing I really need now is a good few dives (or a dive holiday) to get back into the swing of things!

Custom php.ini with Plesk

By , Wednesday 13th August 2008 3:53 pm

We have a dedicated server with ukFast on which we run a number of domains that exist on virtual servers running under Plesk. Anyway, on most of my domains I want to hide away warnings and for security keep errors to the log files, however on some of my domains (such as those I’m developing on) I want to show every little nasty warning and error that appears.

If I edit my PHP.INI”>php.ini this affects each and every domain on the server, not a good idea especially when several of the sites are fully live versions  So in order to get around this you need to put a file called vhosts.conf in your conf directory, for example, /var/www/vhosts/<my_domain>/conf/vhost.conf.

Once you’ve added your additional code, again for example I included,

php_value error_reporting E_ALL
php_value display_errors On

followed by running the following command (this reconfigures all of your domains its just quicker lazier than typing in the domain name ,

# /usr/local/psa/admin/bin/websrvmng -a

If you’re feeling a bit nimble then you can always type out the full version which is,

# /usr/local/psa/admin/sbin/websrvmng -u --vhost-name=<my_domain>

After this command if you look to httpd.include file, will see that your httpd.include will have an include line for your vhost.conf, something like this:

Include /srv/www/vhosts/domain.com/conf/vhost.conf

				

Bolometer Theory

By , Thursday 21st June 2007 1:18 pm

I’ve had tons of emails (well about 5 :$) asking about the introduction to ‘bolometer theory’ page I used to have up on the site. So here it is just as before but with some of the links to other bits of parts of the old site removed :)

Introduction to bolometer theory













Panorama Theme by Themocracy

5 visitors online now
2 guests, 3 bots, 0 members
Max visitors today: 16 at 05:07 pm UTC
This month: 23 at 05-02-2012 10:05 am UTC
This year: 54 at 09-01-2012 07:56 pm UTC
All time: 130 at 28-03-2011 10:40 pm UTC