Cron in the Container
Bob McWhirter
14 November 2008

Per the discussion in Nobody should need cron, I've added scheduled-task deployment capability to the JBoss Rails deployer.  Currently this functionality is only in the Git repository, but the next beta binary release will certainly include it.

Instead of editing crontabs, you now work with RAILS_ROOT/config/jboss-scheduler.yml, a simple YAML file.

It defines all the scheduled tasks you want to be managed by the container.  For example, the jboss-scheduler.yml for Odd Thesis looks like this currently:

# Field Name        Allowed Values      Allowed Special Characters
# ----------------------------------------------------------------
# Seconds           0-59                , - * /
# Minutes           0-59                , - * /
# Hours             0-23                , - * /
# Day-of-month      1-31                , - * ? / L W
# Month             1-12 or JAN-DEC     , - * /
# Day-of-Week       1-7 or SUN-SAT      , - * ? / L #
# Year (Optional)   empty, 1970-2099    , - * /

github.commit.poller:
  description: Poll projects for GitHub commits
  cron: 0 */15 * * * ?
  task: Github::CommitPoller

That names a task ("github.commit.poller"), sets the cron spec (every 15 minutes), and wires up an associated Ruby class to do the work (Github::CommitPoller).

The container loads up the rails environment once for each task, and keeps it handy and fresh for each triggering of the task. 

It looks for tasks under RAILS_ROOT/app/scheduler/.  In the above example, it's ultimately using RAILS_ROOT/app/scheduler/github/commit_poller.rb.

The task class itself has no particular requirements, other than being able to respond to run() called with no arguments.

module Github
class CommitPoller
def run()
# do work, use ActiveRecord models, etc
end
end
end

Since you don't want to fire up the whole container to execute your task, perhaps if you're testing, or need an extra execution of it, there's a magic Rake task you can execute:

rake jboss:scheduler:run:commit_poller

The JBoss-Rails Plugin automatically scans your scheduled-tasks and creates rake tasks to run them from the command-line.  It currently just uses the last portion of the name ("commit_poller"), so it's up to you to avoid any naming conflicts.  Having foo/poller.rb and bar/poller.rb will only result in sadness.

I'm not necessarily super-happy with the name of the deployment descriptor (jboss-scheduler.yml) nor of the task class location (app/scheduler/**.rb).  I'm open to suggestions and discussions.

Check it out from GitHub if you're brave, else a binary release should be forthcoming before too long.

And yes, I advise you to use this wisely.  It's not an acceptable way to do very important sysadmin chores.  But if you have some recurring activity that's not either a user thread or runnable as a daemon, I think this can be quite useful.

Check out the Odd Thesis Commits page for an example.  Every 15 minutes it polls GitHub for a YAML feed of recent commits, and then jams them into our database.  It's not a critical task, but one I don't want to screw with Vixie cron to handle.

  • Next Downtime, Updates
  • Previous List Archives
Copyright 2008-2010 Red Hat, Inc.