2020-04-25 11:09:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class GroupImportState < ApplicationRecord
|
|
|
|
self.primary_key = :group_id
|
|
|
|
|
2020-12-03 10:09:46 -05:00
|
|
|
MAX_ERROR_LENGTH = 255
|
|
|
|
|
2020-04-25 11:09:53 -04:00
|
|
|
belongs_to :group, inverse_of: :import_state
|
2020-09-21 14:09:58 -04:00
|
|
|
belongs_to :user, optional: false
|
2020-04-25 11:09:53 -04:00
|
|
|
|
2020-09-21 14:09:58 -04:00
|
|
|
validates :group, :status, :user, presence: true
|
2020-06-09 20:08:55 -04:00
|
|
|
validates :jid, presence: true, if: -> { started? || finished? }
|
2020-04-25 11:09:53 -04:00
|
|
|
|
|
|
|
state_machine :status, initial: :created do
|
|
|
|
state :created, value: 0
|
|
|
|
state :started, value: 1
|
|
|
|
state :finished, value: 2
|
|
|
|
state :failed, value: -1
|
|
|
|
|
|
|
|
event :start do
|
|
|
|
transition created: :started
|
|
|
|
end
|
|
|
|
|
|
|
|
event :finish do
|
|
|
|
transition started: :finished
|
|
|
|
end
|
|
|
|
|
|
|
|
event :fail_op do
|
|
|
|
transition any => :failed
|
|
|
|
end
|
|
|
|
|
|
|
|
after_transition any => :failed do |state, transition|
|
|
|
|
last_error = transition.args.first
|
|
|
|
|
2020-12-03 10:09:46 -05:00
|
|
|
state.update_column(:last_error, last_error.truncate(MAX_ERROR_LENGTH)) if last_error
|
2020-04-25 11:09:53 -04:00
|
|
|
end
|
|
|
|
end
|
2020-06-14 20:08:43 -04:00
|
|
|
|
|
|
|
def in_progress?
|
|
|
|
created? || started?
|
|
|
|
end
|
2020-04-25 11:09:53 -04:00
|
|
|
end
|