aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/line_referential_sync.rb
blob: 5b97010b1bb5e98fba48eecd908822dc65cf5c86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class LineReferentialSync < ActiveRecord::Base
  include AASM
  belongs_to :line_referential
  after_commit :perform_sync, :on => :create
  validate :multiple_process_validation, :on => :create

  private
  def perform_sync
    LineReferentialSyncWorker.perform_async(self.id)
  end

  # There can be only one instance running
  def multiple_process_validation
    if self.class.where(status: [:new, :pending], line_referential_id: line_referential_id).count > 0
      errors.add(:base, :multiple_process)
    end
  end

  aasm column: :status do
    state :new, :initial => true
    state :pending
    state :successful
    state :failed

    event :run, after: :log_pending do
      transitions :from => [:new, :failed], :to => :pending
    end

    event :successful, after: :log_successful do
      transitions :from => :pending, :to => :successful
    end

    event :failed, after: :log_failed do
      transitions :from => :pending, :to => :failed
    end
  end

  def log_pending
    logger.debug "#{self.class.name} sync - pending"
    update_attribute(:started_at, Time.now)
  end

  def log_successful
    logger.debug "#{self.class.name} sync - done"
  end

  def log_failed error
    logger.debug e.message
    logger.debug "#{self.class.name} sync - failed"
  end
end