retryable_record

Retries an operation on an ActiveRecord until no StaleObjectError is being raised.

Build Status Gem Version

Gem | Source | Documentation

Usage

You can use retryable_record in 3 different ways:

Module function

require 'retryable_record'

RetryableRecord.retry(user) do
  user.username = "foo"
  user.save!
end

Kernel import

require 'retryable_record/import'

RetryableRecord(user) do
  user.username = "foo"
  user.save!
end

Module inclusion

require 'retryable_record'

class User < ActiveRecord::Base
  include RetryableRecord
end

user = User.first

user.retryable do
  user.username = "foo"
  user.save!
end

Optimistic locking (lock_version column)

ActiveRecord migration needs to support optimistic locking. See api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.integer :lock_version

      t.timestamps
    end
  end
end

Credits

Inspired by

TODO

  • Better example in README

  • Intergration test with ActiveRecord

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don't break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.