activerecord-deadlock_retry 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 97884d7318a921aa4fb0215ef76bda5765bd4870
4
+ data.tar.gz: 2e0af2a0d9739491aadff728095e386ed794c57c
5
+ SHA512:
6
+ metadata.gz: 79b0ca54b8c311e423f0352c8f845d20e8dbb7d1e5700751cf3452b36fedf37d6ff4c2e054225f22d554c1a5b22c7d2e933fc976d558bf12ff3a6036aa861587
7
+ data.tar.gz: a5bc8ba00a65683e863689ebe22dddf535ffaa2d957d982d4d8f919feeaf08c383aeccd0d90d142177f9a0fbb56238c974baf29106f594c666af52c92611aa15
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-deadlock_retry.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Helge Rausch
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ # ActiveRecord::DeadlockRetry
2
+
3
+ Retries a transaction when a deadlock exception is returned from the database.
4
+ Currently works with MySQL.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'activerecord-deadlock_retry'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install activerecord-deadlock_retry
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ ActiveRecord::DeadlockRetry.transaction do
24
+ # Your possibly deadlocking code here
25
+ end
26
+ ```
27
+
28
+ `ActiveRecord::DeadlockRetry.transaction` optionally takes an options hash with
29
+ the following options:
30
+
31
+ * `:class` - Class `transaction` (or `:method`) is called on. Defaults to
32
+ `ActiveRecord::Base`.
33
+ * `:method` - Method to call on `:class` (or `ActiveRecord::Base`). Defauls to
34
+ `transaction`.
35
+ * `:retries` - Number of retries. Defaults to `3`.
36
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "activerecord-deadlock_retry"
7
+ spec.version = "0.0.3"
8
+ spec.authors = ["ad2games"]
9
+ spec.email = ["devops@ad2games.com"]
10
+ spec.homepage = "http://ad2games.com"
11
+ spec.summary = %q{Retry deadlocked ActiveRecord transactions}
12
+ spec.description = %q{Retry deadlocked ActiveRecord transactions}
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency "activerecord"
20
+ spec.add_development_dependency "bundler"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec"
23
+ end
@@ -0,0 +1,7 @@
1
+ deployment:
2
+ production:
3
+ branch: master
4
+ commands:
5
+ - gem install geminabox
6
+ - gem build activerecord-deadlock_retry.gemspec
7
+ - gem inabox activerecord-deadlock_retry-*.gem --host $GEMINABOX_URL
@@ -0,0 +1,36 @@
1
+ module ActiveRecord
2
+ class DeadlockRetry
3
+ LOG_DIR = 'log'
4
+
5
+ def self.transaction(opts={}, &block)
6
+ klass = opts[:class] || ActiveRecord::Base
7
+ method = opts[:method] || :transaction
8
+ retries = opts[:retries] || 5
9
+ begin
10
+ klass.send(method, &block)
11
+ rescue ActiveRecord::StatementInvalid => e
12
+ if e.message =~ /Deadlock found when trying to get lock/ && retries > 0
13
+ retries -= 1
14
+ logger.info("ActiveRecord::DeadlockRetry: retrying transaction after deadlock (#{retries} retries left)")
15
+ sleep 5
16
+ retry
17
+ end
18
+ raise
19
+ end
20
+ end
21
+
22
+ def self.logger
23
+ return @logger if @logger
24
+ if defined?(Rails)
25
+ @logger = Rails.logger
26
+ else
27
+ Dir.mkdir(LOG_DIR) unless Dir.exist?(LOG_DIR)
28
+ @logger = Logger.new(File.join(LOG_DIR, 'deadlock_retry.log'))
29
+ end
30
+ end
31
+
32
+ def self.logger=(logger)
33
+ @logger = logger
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveRecord::DeadlockRetry do
4
+
5
+ let(:block) { Proc.new {} }
6
+
7
+ it "runs a transaction" do
8
+ expect(ActiveRecord::Base).to receive(:transaction).with(&block)
9
+ ActiveRecord::DeadlockRetry.transaction(&block)
10
+ end
11
+
12
+ it "returns from a transaction when there was one deadlock" do
13
+ expect(ActiveRecord::Base).to receive(:transaction).
14
+ and_raise(ActiveRecord::StatementInvalid,
15
+ "Mysql2::Error: Deadlock found when trying to get lock; ...")
16
+ expect(ActiveRecord::Base).to receive(:transaction)
17
+ ActiveRecord::DeadlockRetry.transaction(&block)
18
+ end
19
+
20
+ it "raises the error, if all retries were unsuccessful" do
21
+ expect(ActiveRecord::Base).to receive(:transaction).exactly(6).times.
22
+ and_raise(ActiveRecord::StatementInvalid,
23
+ "Mysql2::Error: Deadlock found when trying to get lock; ...")
24
+ expect {
25
+ ActiveRecord::DeadlockRetry.transaction(&block)
26
+ }.to raise_error(ActiveRecord::StatementInvalid)
27
+ end
28
+
29
+ it "accepts a different class then the default" do
30
+ expect(OtherClass).to receive(:transaction).with(&block)
31
+ ActiveRecord::DeadlockRetry.transaction(class: OtherClass, &block)
32
+ end
33
+
34
+ it "accepts a different retry count than the default" do
35
+ expect(ActiveRecord::Base).to receive(:transaction).exactly(7).times.
36
+ and_raise(ActiveRecord::StatementInvalid,
37
+ "Mysql2::Error: Deadlock found when trying to get lock; ...")
38
+ expect {
39
+ ActiveRecord::DeadlockRetry.transaction(retries: 6, &block)
40
+ }.to raise_error(ActiveRecord::StatementInvalid)
41
+ end
42
+
43
+ it "accepts a different method than the default" do
44
+ expect(ActiveRecord::Base).to receive(:foo).with(&block)
45
+ ActiveRecord::DeadlockRetry.transaction(method: :foo, &block)
46
+ end
47
+ end
48
+
49
+ class OtherClass < ActiveRecord::Base
50
+ end
@@ -0,0 +1,4 @@
1
+ require 'bundler/setup'
2
+ require 'rspec'
3
+ require 'active_record'
4
+ require './lib/activerecord/deadlock_retry'
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-deadlock_retry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - ad2games
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Retry deadlocked ActiveRecord transactions
70
+ email:
71
+ - devops@ad2games.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - activerecord-deadlock_retry.gemspec
82
+ - circle.yml
83
+ - lib/activerecord/deadlock_retry.rb
84
+ - spec/activerecord_deadlock_retry_spec.rb
85
+ - spec/spec_helper.rb
86
+ homepage: http://ad2games.com
87
+ licenses: []
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.2.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Retry deadlocked ActiveRecord transactions
109
+ test_files:
110
+ - spec/activerecord_deadlock_retry_spec.rb
111
+ - spec/spec_helper.rb