2012-10-24 04:03:09 -04:00
|
|
|
require 'active_record'
|
2015-07-01 07:41:44 -04:00
|
|
|
|
2012-10-24 04:03:09 -04:00
|
|
|
class Transactor < ActiveRecord::Base
|
|
|
|
|
|
|
|
belongs_to :worker
|
|
|
|
|
|
|
|
include AASM
|
|
|
|
aasm :column => :status do
|
|
|
|
state :sleeping, :initial => true
|
|
|
|
state :running, :before_enter => :start_worker, :after_enter => :fail
|
|
|
|
|
|
|
|
event :run do
|
|
|
|
transitions :to => :running, :from => :sleeping
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def start_worker
|
|
|
|
worker.update_attribute(:status, 'running')
|
|
|
|
end
|
|
|
|
|
|
|
|
def fail
|
|
|
|
raise StandardError.new('failed on purpose')
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2015-07-01 07:41:44 -04:00
|
|
|
|
|
|
|
class MultipleTransactor < ActiveRecord::Base
|
|
|
|
|
|
|
|
belongs_to :worker
|
|
|
|
|
|
|
|
include AASM
|
|
|
|
aasm :left, :column => :status do
|
|
|
|
state :sleeping, :initial => true
|
|
|
|
state :running, :before_enter => :start_worker, :after_enter => :fail
|
|
|
|
|
|
|
|
event :run do
|
|
|
|
transitions :to => :running, :from => :sleeping
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def start_worker
|
|
|
|
worker.update_attribute(:status, 'running')
|
|
|
|
end
|
|
|
|
|
|
|
|
def fail
|
|
|
|
raise StandardError.new('failed on purpose')
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|