librelist archives

« back to archive

resque-workflow: A DSL for describing workflows whose steps are Resque jobs

resque-workflow: A DSL for describing workflows whose steps are Resque jobs

From:
Tony Arcieri
Date:
2010-10-22 @ 02:44
We use Resque to manage complex, multi-step workflows, and right now our
means of doing this is pretty ad hoc. We have idempotent jobs that can be
safely auto-retried in the case of failures (e.g. unresolved bugs or remote
service failures), and I'd like a DSL to describe our workflows and step
through them. To further complexify things, some of the steps of our jobs
require manual intervention, in which case our Rails app would need to
advance the state of the workflow after some form of user interaction.
I've been sort of hacking on a gem to do this for awhile, it's available
here:

http://github.com/tarcieri/resque-workflow

The state of the workflow is persisted in a database via ActiveRecord. I've
designed Resque::Workflow as a module you can include in an
ActiveRecord::Base-derived class.

So far all it does is build classes for Resque jobs based on the workflow
you describe. Trying to spec things out becomes a bit complex when Resque
itself and its asynchronous nature becomes involved... I might try mocking
out the interaction with Resque itself.

Anyway, if anyone else is interested in something like this, I just wanted
you all to know I'm taking a stab at it. Below is the workflow I've been
using in the spec:

class Cat < ActiveRecord::Base
  include Resque::Workflow

  states :new, :caught, :in_bathroom, :washed, :default => :new
  workflow do
    # Catch the cat
    job_for :new, :success => :caught do |cat|
      puts "Catching the cat..."
      cat.grab
    end

    # Walk to bathroom
    job_for :caught, :success => :in_bathroom, :fail => :new do |cat|
      puts "Carrying cat to the bathroom"
      cat.carry_to_bathroom
    end

    # Wash cat
    job_for :in_bathroom, :success => :washed do |cat|
      puts "Washing cat"
      cat.wash
    end
  end

  def grab
    puts "Cat caught!"
  end

  def carry_to_bathroom
    puts "Cat carried to bathroom!"
  end

  def wash
    puts "Cat washed!"
  end
end

-- 
Tony Arcieri
Medioh! A Kudelski Brand

Re: [resque] resque-workflow: A DSL for describing workflows whose steps are Resque jobs

From:
Thibaut Barrère
Date:
2010-10-22 @ 07:54
Hi

Thanks for sharing, I'll have a closer look.

-- Thibaut