I’m sorry, I don’t remember authorising anyone to make it the end of August already. The last 30 days have been CRAZY. In no particular order:
The business and mobile phone versions of 88 Miles was released, so not you can track your time when you are out of the office. The business version allows one person to manage the time of many other people, which makes running a small office much easier.
My laptop video card decided to goto the big ol’ interweb in the sky, so I have been with out it for the last two weeks. Apparently trying to source a motherboard for a computer that is barely two years old is a less that trivial task. This has meant that I have had to use my Linux machine for day-to-day use (other than at work) which has been interesting to say the least. One tidbit of wisdom - don’t try running major OS updates of a production server late on a Saturday night. It results in spending much of Sunday testing backup systems 
The WA Web Awards have come and gone. I had the honour of chairing the awards this year, and might I say, even though a stupid amount of time went into organising it, it was well worth it. We had 130 enteries, sold out all 114 tickets in a week and managed to pull the whole thing together all whilst working fulltime.
The new sit down dinner format really added to the night, it felt so much more awards night like. I must put out a big thankyou to the rest of the committee - there were some hairy moments in the last couple of weeks, but it all came together on the night. Congratulations to all of the winners - especially Freckle Creative, who took out the coverted “Best overall” award. There are photos for your viewing pleasure on Flickr.
The Sunday after the web awards (that is 2 days after the web awards) Bam Creative moved offices. And, I must say that I like the new digs - they have a much better feel about them and it seems to be much more condusive to work (Which is a handy thing for a work place).
Work at Bam has been non-stop of the past two weeks, the three of four major projects I’ve been working on have pushed the boundries of my sanity, but since some would argue I was already insane, there may not be too much difference… On top of that my MadPilot work still seems to be trickling in, regardless of how much I tell people that I’m working full time now.
Luckily, next month is Web Directions, so I can at least enjoy a couple of days off. I’m really excited about the event, it’ll be great to meet some of the big names in the web industry. I find these sorts of events really inspiring and invigorating. I’ll try and blog my way through it, just like every other man and his dog!
Web developers deal with scheduled jobs a lot. Any online application that deals with paid subscriptions needs to remind users when they need to pay up. Other apps may require data mining services to be run on a nightly basis.
This is pretty easy to do - run a cronjob under *nix or a scheduled task under windows. However, where things get tricky is when you have spent an in ordinate amount of time coding business logic into you application. Duplicating this logic for an external script to be run by cron is pretty silly, not to mention bug-prone. However there is a quick and simple solutions: Web services!
By setting up a web service that does your maintainence, you can leverage the classes and business rules that you have already written. For example, if we take a Model-View-Controller framework (such as Rails or CakePHP) we may have logic that will do data manipulations before the data is saved to the database. There is no way I would want to try and emulate what these frameworks do in an external script. By using web services, you are calling the framework natively and you avoid all of these problems.
How it works
It is really simple:
- Setup a webservice that will perform the functions you need to run on a regular basis.
- Create a simple script that calls the web services.
- Add this script to you crontab or task scheduler
The beauty is that you don’t even need to bother setting up the web service using SOAP or XML-RPC - REST will do the job quite nicely, especially since the web service is designed to only be used by you. However, when using rails I like to use SOAP, because it is so easy to setup and use. Here is a simple example:
housekeeping_controller.rb
-
class HousekeepingController < ApplicationController
-
wsdl_service_name ‘Housekeeping’
-
web_service_api HouseKeepingAPI
-
-
def maintain()
-
# Run you maintainence script here
-
end
-
end
This file is the webservices controller. You would add your logic here - this may be expiring users or automatically checking email.
housekeeping_api.rb
-
class HousekeepinApi < ActionWebService::API::Base
-
api_method :maintain, :returns => [:string]
-
end
This file defines the method to the web service caller. I usually set the return to type :string so I can output statistical messages or errors that occur during the job.
housekeeping_job.rb
-
#!/usr/bin/ruby -w
-
-
require ’soap/wsdlDriver’
-
include SOAP
-
-
begin
-
wsdl = ‘path_to_wsdl_file’
-
factory = WSDLDriverFactory.new(wsdl)
-
housekeeping = factory.create_rpc_driver
-
out = housekeeping.maintain
-
-
put_s out
-
rescue Exception
-
$stderr.print “An error occured: #{$!}\n“
-
$stderr.print “Detailes error description, if any:\n“
-
raise
-
end
This file is called by the cronjob. You will need to replace the string path_to_wsdl_file with the path to the real WSDL file. You get get the WSDL file from a Rails web service by querying /:controller/wsdl (so in this example http://www.yoursite.com/housekeeping/wsdl - pipe the returned xml to a file and save it.
Then it is just a matter of adding the housekeeping_run.rb command to you cronjob! Cron will even email you the results that get returned by your service - Nifty!
Nice an easy, eh?