mongoid-semaphore 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +15 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/mongoid-semaphore.rb +1 -0
- data/lib/mongoid/semaphore.rb +48 -0
- data/lib/mongoid/semaphore/sync_methods.rb +42 -0
- data/lib/mongoid/semaphore/unsynchronized_access.rb +11 -0
- data/mongoid-semaphore.gemspec +64 -0
- data/test/helper.rb +18 -0
- data/test/test_mongoid-semaphore.rb +7 -0
- metadata +118 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
gem 'mongoid'
|
7
|
+
|
8
|
+
# Add dependencies to develop your gem here.
|
9
|
+
# Include everything needed to run rake, tests, features, etc.
|
10
|
+
group :development do
|
11
|
+
gem "shoulda", ">= 0"
|
12
|
+
gem "bundler", "~> 1.0.0"
|
13
|
+
gem "jeweler", "~> 1.6.4"
|
14
|
+
gem "rcov", ">= 0"
|
15
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 James Petty
|
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,19 @@
|
|
1
|
+
= mongoid-semaphore
|
2
|
+
|
3
|
+
Mongoid semaphore is a module that allows model instances to behave as synchronized objects.
|
4
|
+
|
5
|
+
== Contributing to mongoid-semaphore
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2012 James Petty. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "mongoid-semaphore"
|
18
|
+
gem.homepage = "http://github.com/pettyjamesm/mongoid-semaphore"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Adds Semaphore-like behaviors to a Mongoid::Document instance}
|
21
|
+
gem.description = %Q{Adds Semaphore-like behaviors to a Mongoid::Document instance}
|
22
|
+
gem.email = "jp@jibe.com"
|
23
|
+
gem.authors = ["James Petty"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
test.rcov_opts << '--exclude "gems/*"'
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "mongoid-semaphore #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'mongoid/semaphore'
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
require 'mongoid/semaphore/unsynchronized_access'
|
3
|
+
require 'mongoid/semaphore/sync_methods'
|
4
|
+
|
5
|
+
module Mongoid
|
6
|
+
module Semaphore
|
7
|
+
#------------------------------------------------------------------------------
|
8
|
+
# => Overridden Instance Attribute Accessors on Volatile Fields
|
9
|
+
#------------------------------------------------------------------------------
|
10
|
+
# => Volatile Value, Refresh Before Returning
|
11
|
+
def sema_count
|
12
|
+
self.save()
|
13
|
+
self.reload
|
14
|
+
return super
|
15
|
+
end
|
16
|
+
# => Volatile Value, Refresh Before Returning
|
17
|
+
def used_by
|
18
|
+
self.save()
|
19
|
+
self.reload
|
20
|
+
return super
|
21
|
+
end
|
22
|
+
#------------------------------------------------------------------------------
|
23
|
+
# => Inclusion hook. Adds fields, and default attributes
|
24
|
+
#------------------------------------------------------------------------------
|
25
|
+
def self.included(base)
|
26
|
+
super if defined?(super)
|
27
|
+
base.class_eval do
|
28
|
+
# => Define necessary fields for synchronization
|
29
|
+
unless base.included_modules.include?(Mongoid::Document)
|
30
|
+
include Mongoid::Document
|
31
|
+
end
|
32
|
+
# => Default Definition for semaphore value
|
33
|
+
def __semaphore_attrs; @_semattr ||= {:count => 1}; end
|
34
|
+
def __semaphore_initial_count; __semaphore_attrs[:count]; end
|
35
|
+
# => Helper method for class to override default count
|
36
|
+
def default_sema_count(count)
|
37
|
+
count = count.to_i
|
38
|
+
__semaphore_attrs[:count] = count
|
39
|
+
return
|
40
|
+
end
|
41
|
+
# => Semaphore Counter
|
42
|
+
field :sema_count, :type => Integer, :default => lambda { __semaphore_initial_count }
|
43
|
+
# => Semaphore is currently held by "<hostname>:<pid>" || nil
|
44
|
+
field :used_by, :type => String, :default => nil
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'process'
|
3
|
+
require 'mongoid/semaphore/unsynchronized_access'
|
4
|
+
|
5
|
+
|
6
|
+
module Mongoid
|
7
|
+
module Semaphore
|
8
|
+
def reset_semaphore!
|
9
|
+
self.update_attributes!({:sema_count => 1, :used_by => nil})
|
10
|
+
end
|
11
|
+
|
12
|
+
def synchronized(&block)
|
13
|
+
begin
|
14
|
+
self.dec(:sema_count, 1)
|
15
|
+
if (self.sema_count == 0)
|
16
|
+
begin
|
17
|
+
self.set(:used_by, "#{Socket.gethostname}:#{Process.pid}")
|
18
|
+
block.call(self)
|
19
|
+
ensure
|
20
|
+
self.set(:used_by, nil)
|
21
|
+
end
|
22
|
+
else
|
23
|
+
raise Mongoid::Semaphore::UnsynchronizedAccess.new(self.sema_count, "Semaphore in Use! (#{self.used_by})")
|
24
|
+
end
|
25
|
+
ensure
|
26
|
+
self.inc(:sema_count, 1)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def try_synchronized(&try_block)
|
31
|
+
return false unless self.sema_count == 1
|
32
|
+
begin
|
33
|
+
self.synchronized do
|
34
|
+
try_block.call()
|
35
|
+
end
|
36
|
+
return true
|
37
|
+
rescue Mongoid::Semaphore::UnsynchronizedAccess => ua
|
38
|
+
return false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "mongoid-semaphore"
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["James Petty"]
|
12
|
+
s.date = "2012-03-26"
|
13
|
+
s.description = "Adds Semaphore-like behaviors to a Mongoid::Document instance"
|
14
|
+
s.email = "jp@jibe.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"LICENSE.txt",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/mongoid-semaphore.rb",
|
27
|
+
"lib/mongoid/semaphore.rb",
|
28
|
+
"lib/mongoid/semaphore/sync_methods.rb",
|
29
|
+
"lib/mongoid/semaphore/unsynchronized_access.rb",
|
30
|
+
"mongoid-semaphore.gemspec",
|
31
|
+
"test/helper.rb",
|
32
|
+
"test/test_mongoid-semaphore.rb"
|
33
|
+
]
|
34
|
+
s.homepage = "http://github.com/pettyjamesm/mongoid-semaphore"
|
35
|
+
s.licenses = ["MIT"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = "1.8.10"
|
38
|
+
s.summary = "Adds Semaphore-like behaviors to a Mongoid::Document instance"
|
39
|
+
|
40
|
+
if s.respond_to? :specification_version then
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
44
|
+
s.add_runtime_dependency(%q<mongoid>, [">= 0"])
|
45
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
46
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
47
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
48
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<mongoid>, [">= 0"])
|
51
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
52
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
53
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
54
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<mongoid>, [">= 0"])
|
58
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
59
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
60
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
61
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'mongoid-semaphore'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-semaphore
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- James Petty
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongoid
|
16
|
+
requirement: &16436380 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *16436380
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: shoulda
|
27
|
+
requirement: &16455160 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *16455160
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: bundler
|
38
|
+
requirement: &16453620 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.0.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *16453620
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: jeweler
|
49
|
+
requirement: &16452440 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.6.4
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *16452440
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rcov
|
60
|
+
requirement: &16451160 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *16451160
|
69
|
+
description: Adds Semaphore-like behaviors to a Mongoid::Document instance
|
70
|
+
email: jp@jibe.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files:
|
74
|
+
- LICENSE.txt
|
75
|
+
- README.rdoc
|
76
|
+
files:
|
77
|
+
- .document
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.rdoc
|
81
|
+
- Rakefile
|
82
|
+
- VERSION
|
83
|
+
- lib/mongoid-semaphore.rb
|
84
|
+
- lib/mongoid/semaphore.rb
|
85
|
+
- lib/mongoid/semaphore/sync_methods.rb
|
86
|
+
- lib/mongoid/semaphore/unsynchronized_access.rb
|
87
|
+
- mongoid-semaphore.gemspec
|
88
|
+
- test/helper.rb
|
89
|
+
- test/test_mongoid-semaphore.rb
|
90
|
+
homepage: http://github.com/pettyjamesm/mongoid-semaphore
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ! '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
hash: 4347539133218194400
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ! '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 1.8.10
|
115
|
+
signing_key:
|
116
|
+
specification_version: 3
|
117
|
+
summary: Adds Semaphore-like behaviors to a Mongoid::Document instance
|
118
|
+
test_files: []
|