@madpilot makes

Make scheduling jobs easier – use web services!

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:

  1. Setup a webservice that will perform the functions you need to run on a regular basis.
  2. Create a simple script that calls the web services.
  3. 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:

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.

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.

#!/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?