2020-05-24 23:13:21 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class GroupImportState < ApplicationRecord
|
|
|
|
self.primary_key = :group_id
|
|
|
|
|
|
|
|
belongs_to :group, inverse_of: :import_state
|
2021-01-03 14:25:43 +05:30
|
|
|
belongs_to :user, optional: false
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
validates :group, :status, :user, presence: true
|
2020-06-23 00:09:42 +05:30
|
|
|
validates :jid, presence: true, if: -> { started? || finished? }
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
state.update_column(:last_error, last_error) if last_error
|
|
|
|
end
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
def in_progress?
|
|
|
|
created? || started?
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|