2020-09-11 22:08:03 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class MyModuleStatusConsequencesJob < ApplicationJob
|
|
|
|
queue_as :high_priority
|
|
|
|
|
2020-11-30 19:28:29 +08:00
|
|
|
def perform(my_module, my_module_status_consequences, status_changing_direction)
|
2022-03-25 22:38:15 +08:00
|
|
|
error = nil
|
|
|
|
my_module.transaction(requires_new: true) do
|
2020-09-11 22:08:03 +08:00
|
|
|
my_module_status_consequences.each do |consequence|
|
2020-12-01 22:07:01 +08:00
|
|
|
consequence.public_send(status_changing_direction, my_module)
|
2020-09-11 22:08:03 +08:00
|
|
|
end
|
|
|
|
my_module.update!(status_changing: false)
|
2022-04-04 17:04:03 +08:00
|
|
|
|
|
|
|
# don't clear error if in transition error rollback state
|
|
|
|
my_module.update!(last_transition_error: nil) unless my_module.transition_error_rollback
|
2020-09-11 22:08:03 +08:00
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.error(e.message)
|
|
|
|
Rails.logger.error(e.backtrace.join("\n"))
|
2022-03-25 22:38:15 +08:00
|
|
|
error = if e.is_a?(MyModuleStatus::MyModuleStatusTransitionError)
|
|
|
|
e.error
|
|
|
|
else
|
|
|
|
{ type: :general, message: e.message }
|
|
|
|
end
|
|
|
|
raise ActiveRecord::Rollback
|
2020-09-11 22:08:03 +08:00
|
|
|
end
|
2022-03-25 22:38:15 +08:00
|
|
|
if error.present?
|
2022-04-04 17:04:03 +08:00
|
|
|
my_module.transition_error_rollback = true
|
2020-09-11 22:08:03 +08:00
|
|
|
my_module.my_module_status = my_module.changing_from_my_module_status
|
2022-03-25 22:38:15 +08:00
|
|
|
my_module.last_transition_error = error
|
2020-09-11 22:08:03 +08:00
|
|
|
my_module.status_changing = false
|
|
|
|
my_module.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|