db_lock 0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a417593c5982747526ac5920330834da914b6738
4
+ data.tar.gz: 4bb0bcb0d544ee9caa69898d2490d84236788674
5
+ SHA512:
6
+ metadata.gz: 0845da08c077e9e781142b5933227918661b2865a18bc46f9f2b70702a86658556cf5124296ede7fa4d05b310f7b930805bce70942986683cc6e6e3f44209390
7
+ data.tar.gz: 036c960adfe8d81385a1eeaac2cd8ccb8d696dab8b73fb18152adbc6169db9676954c5862682770db57c0695940030c1ef5960b30eaf4751ff07c4fd1f07ea86
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # DBLock
2
+
3
+ Gem to obtain and release manual mysql db locks. This can be utilized for example to make sure that certain rake tasks do not run in parallel on the same database (for exampel when corn jobs run for too long or are accidnetially started multiple times).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'db_lock'
10
+
11
+ then run `bundle`
12
+
13
+ ## Usage
14
+
15
+ ```ruby
16
+ DBLock::Lock.get('name_of_lock', 5) do
17
+ # code here
18
+ end
19
+ ```
20
+
21
+ Before the code block is executed, it will attempt to aquire a mysql db lock for X seconds (5 in this example). If this fails it will raise an `DBLock::AlreadyLocked` error. The lock is released after the block is executed, even if the block raised an error itself.
22
+
23
+ ## Smart lock name
24
+
25
+ If you prefix the lock with a `.` in a Rails application, `.` will be automatically replaced with `YourAppName.`.
data/lib/db_lock.rb ADDED
@@ -0,0 +1,38 @@
1
+ module DBLock
2
+ class AlreadyLocked < StandardError; end
3
+
4
+ class Lock
5
+ class << self
6
+ def get(name, timeout=0)
7
+ timeout = timeout.to_i # catches nil
8
+ timeout = 0 if timeout < 0
9
+ raise "invalid lock name: #{name.inspect}" if !name or name.to_s.length == 0
10
+ raise AlreadyLocked.new("Already lock in progress") if locked?
11
+ name = prefixed_lock_name(name)
12
+ res = ActiveRecord::Base.connection.execute("SELECT GET_LOCK('#{name}', #{timeout})")
13
+ if @locked = (res and res.first and res.first[0] == 1)
14
+ yield
15
+ else
16
+ raise AlreadyLocked.new("Unable to obtain lock '#{name}' within #{timeout} seconds") unless @locked
17
+ end
18
+ ensure
19
+ ActiveRecord::Base.connection.execute("SELECT RELEASE_LOCK('#{name}')") if @locked
20
+ @locked = false
21
+ end
22
+
23
+ def locked?
24
+ @locked ||= false
25
+ end
26
+
27
+ private
28
+
29
+ def prefixed_lock_name(name)
30
+ if name[0] == "." and defined? Rails
31
+ name = "#{Rails.application.class.parent_name}.#{name}"
32
+ else
33
+ name
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module DBLock
2
+ VERSION = "0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: db_lock
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - mkon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-14 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: '3.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.0'
47
+ description: Obtain manual db locks to guard blocks of code from parallel execution.
48
+ Currently only supports mysql.
49
+ email:
50
+ - konstantin@munteanu.de
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - MIT-LICENSE
56
+ - README.md
57
+ - lib/db_lock.rb
58
+ - lib/db_lock/version.rb
59
+ homepage: https://github.com/mkon/db_lock
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.2.2
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Obtain manual db/mysql locks
83
+ test_files: []