Module: SmsOnRails::LockableRecord::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#delete_stale_recordsObject



168
169
170
# File 'lib/sms_on_rails/activerecord_extensions/lockable_record.rb', line 168

def delete_stale_records
  delete_all stale_record_conditions
end

#lockable_record(options = {}) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/sms_on_rails/activerecord_extensions/lockable_record.rb', line 150

def lockable_record(options={})
  self.locrec_options = ActiveRecord::Base.locrec_options.merge(options)
  [:columns, :status].each do |method|
    if options[method]
      self.locrec_options[method] = ActiveRecord::Base.locrec_options[method].merge(options[method])
    end
  end
end

#locrec_columnsObject



159
# File 'lib/sms_on_rails/activerecord_extensions/lockable_record.rb', line 159

def locrec_columns; locrec_options[:columns]; end

#locrec_statusObject



160
# File 'lib/sms_on_rails/activerecord_extensions/lockable_record.rb', line 160

def locrec_status; locrec_options[:status]; end

#recover_stale_recordsObject



162
163
164
165
# File 'lib/sms_on_rails/activerecord_extensions/lockable_record.rb', line 162

def recover_stale_records
  update_all(["#{locrec_columns[:status]} = ?", locrec_status[:not_processed]],
             stale_record_conditions)
end

#stale_record_conditionsObject



172
173
174
175
176
177
178
179
# File 'lib/sms_on_rails/activerecord_extensions/lockable_record.rb', line 172

def stale_record_conditions
  query = []
  query << "#{locrec_columns[:status]} = :status"
  query << "#{locrec_columns[:processed_on]} < now() - interval :min minute"
  map = {:status => locrec_status[:processing],
         :min => locrec_options[:recover_stalled_minutes]}
  [query.join(' AND '), map]
end