ar_lock 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ar_lock.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Roland Guem
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.rdoc ADDED
@@ -0,0 +1,66 @@
1
+ = ArLock for Rails3
2
+
3
+ This Rails3 Gem implements an atomic locking model based on ActiveRecord.
4
+
5
+ == Install
6
+
7
+ Inside your Gemfile:
8
+
9
+ gem "ar_record"
10
+
11
+ and then run:
12
+
13
+ * bundle install
14
+
15
+ === Gem Dependencies
16
+
17
+ Please check if all those requirements are satisfied on your environment.
18
+
19
+ * rails >= 3.0.0
20
+
21
+ === Inside your Application:
22
+
23
+ Geting a lock is always an atomic operation. If you try to get more than one lock (by specifying an array), this is also executed atomic.
24
+
25
+ ==== Get lock(s):
26
+ Lock.get :lock_name
27
+ or
28
+ Lock.get [:lock_name1, :lock_name2]
29
+
30
+ optional you can specify a value (for example the user, which acquired the lock):
31
+ Lock.get :lock_name, :value => 'value'
32
+ or
33
+ Lock.get [:lock_name1, :lock_name2], :value => 'value'
34
+
35
+ You can let Lock.get block, until the lock was acquired:
36
+ Lock.get :lock_name, :blocking => true
37
+ or
38
+ Lock.get [:lock_name1, :lock_name2], :blocking => true
39
+
40
+ ==== Release lock(s):
41
+ Lock.release :lock_name
42
+ or
43
+ Lock.release [:lock_name1, :lock_name2]
44
+
45
+ Only release a lock, if the value matches:
46
+ Lock.release :lock_name, :value => 'value'
47
+ or
48
+ Lock.release [:lock_name1, :lock_name2], :value => 'value'
49
+
50
+ == Generators
51
+
52
+ * rails generate ar_lock:migration
53
+
54
+ === Database Setup
55
+
56
+ Use
57
+
58
+ rails g ar_lock:migration
59
+
60
+ This will create a database migration:
61
+
62
+ TIMESTAMP_add_ar_lock_table.rb
63
+
64
+ == Copyright
65
+
66
+ Copyright (c) 2011 Philip Kurmann. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,119 @@
1
+ class Lock < ActiveRecord::Base
2
+
3
+ # Get an atomic lock if value matches or is nil. Block until lock was successful if args[:blocking] was set to true
4
+ def self.get name, args = {}
5
+ poll_time = args[:poll_time] || 10
6
+ until (lock = get_lock_for(name, args)) || args[:blocking] != true do
7
+ sleep poll_time
8
+ end
9
+ lock
10
+ end
11
+
12
+
13
+ # Release an atomic lock if value matches or is nil
14
+ def self.release names, args = {}
15
+ successful = false
16
+
17
+ if name
18
+ # If names is a single string, transorm it to an array
19
+ names = [names.to_s] if names.class == String || names.class == Symbol
20
+
21
+ self.transaction do
22
+ locks = self.where(:name => names).lock(true).all
23
+
24
+ unless locks.blank?
25
+ successful = true
26
+ # Check, if the value of every selected lock matches the args[:value] unless no args[:value] was specified
27
+ locks.each{ |l| successful = false unless (args[:value].nil? || (args[:value] != nil && args[:value] == l.value)) }
28
+ # Now destroy the locks, if it is successful
29
+ locks.each{ |l| l.destroy } if successful
30
+ end
31
+ end
32
+ end
33
+
34
+ successful
35
+ end
36
+
37
+
38
+ # Release all locks for a given task. This is only working, if the value was set to "task #{task.id}"
39
+ def self.release_for_task task
40
+ self.transaction do
41
+ locks = self.where(:value => "task #{task.id}").lock(true).all
42
+ locks.each{ |l| l.destroy }
43
+ end
44
+ end
45
+
46
+
47
+ # Return value of a lock. When no lock was found, nil will be returned.
48
+ def self.get_value name
49
+ lock = self.where(:name => name).first
50
+ if lock
51
+ lock.value.nil? ? '' : lock.value
52
+ else
53
+ nil
54
+ end
55
+ end
56
+
57
+
58
+ # Destroy all locks in one atomic operation
59
+ def self.release_all
60
+ self.transaction do
61
+ self.all.each { |l| l.destroy }
62
+ end
63
+ end
64
+
65
+
66
+ private
67
+
68
+
69
+ # Get an atomic lock if value matches or is nil
70
+ def self.get_lock_for names, args = {}
71
+ successful_if_value_matches = args[:successful_if].to_s == 'value_matches'
72
+ successful = false
73
+
74
+ if names
75
+ # If names is a single string, transorm it to an array
76
+ names = [names.to_s] if names.class == String || names.class == Symbol
77
+
78
+ self.transaction do
79
+ locks = self.where(:name => names).lock(true).all
80
+
81
+ if locks.blank?
82
+ names.each{ |n| self.create :name => n, :value => args[:value] }
83
+ successful = true
84
+ else
85
+ if args[:force]
86
+ names.each do |n|
87
+ lock = locks.find{ |l| l.name == n }
88
+ if lock
89
+ lock.update_attribute :value, args[:value]
90
+ else
91
+ self.create :name => n, :value => args[:value]
92
+ end
93
+ end
94
+ successful = true
95
+ elsif successful_if_value_matches && args[:value] != nil
96
+ successful = true
97
+ created_locks = []
98
+ names.each do |n|
99
+ lock = locks.find{ |l| l.name == n }
100
+ if lock
101
+ successful = false if lock.value != args[:value]
102
+ else
103
+ created_locks << self.create(:name => n, :value => args[:value])
104
+ end
105
+ end
106
+ # Rollback created locks, if the lock acquire was not successful
107
+ created_locks.each{ |l| l.destroy } unless successful
108
+ end
109
+ end
110
+
111
+ # Wait some secound inside the transaction for debuging and testing purposes
112
+ sleep args[:debug_wait].to_i if args[:debug_wait]
113
+ end
114
+ end
115
+
116
+ successful
117
+ end
118
+
119
+ end
data/ar_lock.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "ar_lock/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ar_lock"
7
+ s.version = ArLock::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Philip Kurmann"]
10
+ s.email = ["philip.kurmann@inwork.ch"]
11
+ s.homepage = ""
12
+ s.summary = %q{Atomic lock model implemented by Active Record}
13
+ s.description = %q{Atomic lock mode which stores its locks in the database.}
14
+
15
+ s.rubyforge_project = "ar_lock"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
data/lib/ar_lock.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'ar_lock'
2
+ require 'rails'
3
+
4
+ module ArLock
5
+ class Engine < ::Rails::Engine
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module ArLock
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,37 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module ArLock
5
+ class MigrationGenerator < Rails::Generators::Base
6
+ desc "The ar_lock migration generator creates a database migration for the lock model."
7
+ include Rails::Generators::Migration
8
+ source_root File.join(File.dirname(__FILE__), 'templates')
9
+
10
+ class_option :ar_lock_table_name,
11
+ :type => :string,
12
+ :desc => "Name for the ArLock Table",
13
+ :required => false,
14
+ :default => "locks"
15
+
16
+ def initialize(args = [], options = {}, config = {})
17
+ super
18
+ end
19
+
20
+ #attr_reader :lock_table_name
21
+
22
+ # Implement the required interface for Rails::Generators::Migration.
23
+ # taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
24
+ def self.next_migration_number(dirname)
25
+ if ActiveRecord::Base.timestamped_migrations
26
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
27
+ else
28
+ "%.3d" % (current_migration_number(dirname) + 1)
29
+ end
30
+ end
31
+
32
+ def create_migration_file
33
+ migration_template 'migration.rb', 'db/migrate/add_ar_lock_table.rb'
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,15 @@
1
+ class AddArLockTable < ActiveRecord::Migration
2
+ def self.up
3
+ create_table "<%= options[:ar_lock_table_name] %>", :force => true do |t|
4
+ t.string :name, :null => false
5
+ t.string :value
6
+
7
+ t.timestamps
8
+ end
9
+ add_index "<%= options[:ar_lock_table_name] %>", :name, :unique => true
10
+ end
11
+
12
+ def self.down
13
+ drop_table "<%= options[:ar_lock_table_name] %>"
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ar_lock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Philip Kurmann
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Atomic lock mode which stores its locks in the database.
15
+ email:
16
+ - philip.kurmann@inwork.ch
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.rdoc
25
+ - Rakefile
26
+ - app/models/lock.rb
27
+ - ar_lock.gemspec
28
+ - lib/ar_lock.rb
29
+ - lib/ar_lock/version.rb
30
+ - lib/generators/ar_lock/migration/migration_generator.rb
31
+ - lib/generators/ar_lock/migration/templates/migration.rb
32
+ homepage: ''
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project: ar_lock
52
+ rubygems_version: 1.8.24
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Atomic lock model implemented by Active Record
56
+ test_files: []