Module: SmsOnRails::LockableRecord::InstanceMethods

Defined in:
lib/sms_on_rails/activerecord_extensions/lockable_record.rb

Instance Method Summary collapse

Instance Method Details

#already_processed?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/sms_on_rails/activerecord_extensions/lockable_record.rb', line 43

def already_processed?
  get_locrec_col(:status) != locrec_status[:not_processed]
end

#get_locrec_col(col) ⇒ Object



143
144
145
# File 'lib/sms_on_rails/activerecord_extensions/lockable_record.rb', line 143

def get_locrec_col(col)
  send locrec_columns[col]
end

#lock_recordObject

Lock the record by setting the status from NOT_PROCESSED TO PROCESSED StalerecordErrors are caught and logged



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sms_on_rails/activerecord_extensions/lockable_record.rb', line 48

def lock_record
  if already_processed?
    raise SmsOnRails::LockableRecord::AlreadyProcessed.new(
      "Record #{self.to_param} appears to be processed. #{locrec_columns[:status]}" +
      " is #{get_locrec_col(:status)} instead of #{locrec_status[:not_processed]}"
    )
  end

  begin
    set_locrec_col :processed_on, Time.now
    set_locrec_col :status,       locrec_status[:processing]
    set_locrec_col :retry_count,  get_locrec_col(:retry_count).to_i + 1
    save!
    return true
  #this just means another proc got to it first, skip
  rescue ActiveRecord::StaleObjectError => exc
    if locrec_options[:log_lock_warnings]
      log_locrec_error :level => :warn,
                        :msg => "#{self.class} lock failed",
                        :exception => exc
    end
    raise SmsOnRails::LockableRecord::UnableToLockRecord.new(
        "Unable to set #{locrec_columns[:status]}. Record is stale")

  end
  false
end

#lock_record_and_execute(&block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/sms_on_rails/activerecord_extensions/lockable_record.rb', line 76

def lock_record_and_execute(&block)

  begin
    # Lock the process, execute the block, set the status to PROCESSED
    # If the process could not be locked (stale record exception), continue without error
    # If the process raised any other exception, set the status to FAILED
    if lock_record
      yield

      if get_locrec_col(:status) == locrec_status[:processing]
        set_locrec_col :status, locrec_status[:processed]
        set_locrec_col :processed_on, Time.now if locrec_columns[:processed_on]
        save!
      end
    end
    return true
  # Do not mess with the record status if it is already being processed
  #rescue ActiveRecord::StaleObjectError => soe
  #  reset_status(locrec_status[:failed], soe)
  
  #rescue self.class.locrec_options[:fatal_exception] => ie
  #  reset_status(locrec_status[:failed], ie)
  #  raise ie
  #

  # Set status to failed and reraise exception if the exception is fatal
  # or we wish to throw all errors
  rescue Exception => exc
    reset_locrec_status(locrec_status[:failed], exc) unless exc.is_a?(SmsOnRails::LockableRecord::UnableToLockRecord)
    raise exc
  end
end

#locrec_columnsObject



36
37
38
# File 'lib/sms_on_rails/activerecord_extensions/lockable_record.rb', line 36

def locrec_columns
  self.class.locrec_columns
end

#locrec_statusObject



39
40
41
# File 'lib/sms_on_rails/activerecord_extensions/lockable_record.rb', line 39

def locrec_status
  self.class.locrec_status
end

#log_locrec_error(options = {}) ⇒ Object



133
134
135
136
137
# File 'lib/sms_on_rails/activerecord_extensions/lockable_record.rb', line 133

def log_locrec_error(options={})
  message = options[:msg]||"ERROR LOCKING INSTANCE #{self}"
  message << "\n#{options[:exception].to_s}" if options[:exception]
  logger.send options[:level]||:error, message
end

#reset_locrec_status(current_status, exc) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/sms_on_rails/activerecord_extensions/lockable_record.rb', line 109

def reset_locrec_status(current_status, exc)
  begin
    #log_entry = LogEntry.error("Send #{self.to_s.demodulize} unexpected error: #{self.id}",nil,exc)
    #fresh copy in case it was updated
    reload
    if get_locrec_col(:status) == locrec_status[:processing]

      set_locrec_col :status, current_status

      if locrec_columns[:notes]
        set_locrec_col :notes, "#{current_status}: #{exc.to_s}"
      end

      if locrec_columns[:sub_status] && exc.respond_to?(:sub_status)
        set_locrec_col :sub_status, exc.sub_status.to_s
      end
      
      save!
    end
  rescue Exception => e2
    log_locrec_error(:level => :fatal, :msg => "fatal fatal error in #{self.class}", :exception => e2)
  end
end

#set_locrec_col(col, value) ⇒ Object



139
140
141
# File 'lib/sms_on_rails/activerecord_extensions/lockable_record.rb', line 139

def set_locrec_col(col, value)
  send "#{locrec_columns[col]}=", value
end