Skip to content

neopoly/retryable_record

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

retryable_record

Travis Gem Version Code Climate Test Coverage Inline docs

Gem | Source | Documentation

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

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

Option attempts

There is also an option attempts to limit the number of retries. If no attempts option is specified, it's assumed to be possibly infinte attempts until an ActiveRecord::StaleObjectError is not raised. The attempts option works in all three forms.

Here is the Module inclusion example with an attempts option used.

require 'retryable_record'

class User < ActiveRecord::Base
  include RetryableRecord
end

user = User.first

user.retryable(:attempts => 5) do
  user.username = "foo"
  user.save!
end

After 5 attempts, this will just re-raise the ActiveRecord::StaleObjectError anyway.

Optimistic locking (lock_version column)

ActiveRecord migration needs to support optimistic locking. See http://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

Contributors

Thanks to all contributions from awesome people!

TODO

  • Improve README example
  • Add Changelog!
  • 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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages