Created
October 30, 2008 18:45
-
-
Save collin/21100 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This is a really crappy way to do job runners. | |
#But I'd never used a job queue before, so I | |
#wanted to do it at least once the wrong way | |
#to figure it out. | |
require 'colored' | |
class Job | |
include DataMapper::Resource | |
property :id, Serial | |
property :name, String | |
property :params, Object | |
property :failed, Boolean | |
class NilJobFailure < StandardError; end | |
def self.next_job | |
j = first :failed => nil | |
j.failed = true | |
j.save | |
j | |
end | |
def self.pull_job | |
job = next_job | |
transaction do | |
raise NilJobFailure.new if job.nil? | |
begin | |
puts "~ Precessing #{job.id} #{job} #{job.name} #{job.params}".bold | |
send job.name, *job.params | |
job.destroy | |
rescue => e | |
puts e.message.red.bold | |
job.failed = true | |
job.save | |
rollback | |
end | |
end | |
pull_job | |
end | |
def self.rescue_failed_jobs | |
all(:failed => true).each do |failed| | |
failed.update_attributes :failed => nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment