lib/rocket_job/plugins/state_machine.rb in rocketjob-3.0.5 vs lib/rocket_job/plugins/state_machine.rb in rocketjob-3.1.0
- old
+ new
@@ -1,9 +1,8 @@
require 'thread'
require 'active_support/concern'
require 'aasm'
-require 'rocket_job/extensions/aasm'
module RocketJob
module Plugins
# State machine for RocketJob
#
@@ -26,49 +25,42 @@
# puts "Oh no, the job has failed with an exception"
# end
# end
module StateMachine
extend ActiveSupport::Concern
- @@aasm_mutex = Mutex.new
included do
include AASM
- # Try to make aasm lookup thread safe
- @aasm = Concurrent::Map.new
- # Call aasm to create default instance upfront
- aasm
# Adds a :before or :after callback to an event
# state_machine_add_event_callback(:start, :before, :my_method)
def self.state_machine_add_event_callback(event_name, action, *methods, &block)
raise(ArgumentError, 'Cannot supply both a method name and a block') if (methods.size > 0) && block
raise(ArgumentError, 'Must supply either a method name or a block') unless (methods.size > 0) || block
- @@aasm_mutex.synchronize do
- # TODO Somehow get AASM to support options such as :if and :unless to be consistent with other callbacks
- # For example:
- # before_start :my_callback, unless: :encrypted?
- # before_start :my_callback, if: :encrypted?
- if event = aasm.state_machine.events[event_name]
- values = Array(event.options[action])
- code =
- if block
- block
- else
- # Validate methods are any of Symbol String Proc
- methods.each do |method|
- unless method.is_a?(Symbol) || method.is_a?(String)
- raise(ArgumentError, "#{action}_#{event_name} currently does not support any options. Only Symbol and String method names can be supplied.")
- end
+ # TODO Somehow get AASM to support options such as :if and :unless to be consistent with other callbacks
+ # For example:
+ # before_start :my_callback, unless: :encrypted?
+ # before_start :my_callback, if: :encrypted?
+ if event = aasm.state_machine.events[event_name]
+ values = Array(event.options[action])
+ code =
+ if block
+ block
+ else
+ # Validate methods are any of Symbol String Proc
+ methods.each do |method|
+ unless method.is_a?(Symbol) || method.is_a?(String)
+ raise(ArgumentError, "#{action}_#{event_name} currently does not support any options. Only Symbol and String method names can be supplied.")
end
- methods
end
- action == :before ? values.push(code) : values.unshift(code)
- event.options[action] = values.flatten.uniq
- else
- raise(ArgumentError, "Unknown event: #{event_name.inspect}")
- end
+ methods
+ end
+ action == :before ? values.push(code) : values.unshift(code)
+ event.options[action] = values.flatten.uniq
+ else
+ raise(ArgumentError, "Unknown event: #{event_name.inspect}")
end
end
def self.state_machine_define_event_callbacks(*event_names)
event_names.each do |event_name|
@@ -82,24 +74,9 @@
end
RUBY
end
end
- # Patch AASM so that save! is called instead of save
- # So that validations are run before job.requeue! is completed
- # Otherwise it just fails silently
- def aasm_write_state(state, name=:default)
- attr_name = self.class.aasm(name).attribute_name
- old_value = read_attribute(attr_name)
- write_attribute(attr_name, state)
-
- begin
- save!
- rescue Exception => exc
- write_attribute(attr_name, old_value)
- raise(exc)
- end
- end
end
end
end
end