schleyfox-lockfile 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 CleverUA
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,56 @@
1
+ = lockfile
2
+
3
+ * http://github.com/cleverua/lockfile
4
+
5
+ == DESCRIPTION:
6
+
7
+ This ruby gem allows you create a lock file on disk right before it yields given code block. It would not allow to yield the block again while the first one finished (ie while lock file exists). This gem currently works on UNIX operating systems. This gem should not be used on NFS (http://lwn.net/Articles/251004/)
8
+
9
+ == USAGE EXAMPLE
10
+
11
+ === GENERAL USAGE
12
+
13
+ include Lockfile
14
+
15
+ with_lock_file(LOCK_FILE_PATH) do
16
+ ... code that should not be launched simultaneously
17
+ end
18
+
19
+ === TO FORCE STOP
20
+ include Lockfile
21
+
22
+ Lockfile.force_stop(LOCK_FILE_PATH)
23
+
24
+ == REQUIREMENTS:
25
+
26
+ * a UNIX operating system
27
+
28
+ == INSTALL:
29
+
30
+ sudo gem install cleverua-lockfile
31
+
32
+ == LICENSE:
33
+
34
+ (The MIT License)
35
+
36
+ Copyright (c) 2010 CleverUA
37
+
38
+ Permission is hereby granted, free of charge, to any person obtaining
39
+ a copy of this software and associated documentation files (the
40
+ 'Software'), to deal in the Software without restriction, including
41
+ without limitation the rights to use, copy, modify, merge, publish,
42
+ distribute, sublicense, and/or sell copies of the Software, and to
43
+ permit persons to whom the Software is furnished to do so, subject to
44
+ the following conditions:
45
+
46
+ The above copyright notice and this permission notice shall be
47
+ included in all copies or substantial portions of the Software.
48
+
49
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
50
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
51
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
52
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
53
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
54
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
55
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
56
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "cleverua-lockfile"
8
+ gem.summary = %Q{Helps you to maintain lock files that indicate that some resource is occupied by an other process}
9
+ gem.description = %Q{This Ruby gem helps to manage processes in your application so that new process won’t start while the previous one is still running. It uses so known 'lock file' approach to figure out whether a process is running or not.}
10
+ gem.email = "pavlo@cleverua.com"
11
+ gem.homepage = "http://github.com/cleverua/lockfile"
12
+ gem.authors = ["pavlo"]
13
+ #gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
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 = "lockfile #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/lib/lockfile.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'fileutils'
2
+
3
+ module Lockfile
4
+
5
+ def self.ensure_no_lock_file_exists(lock_file_name)
6
+ FileUtils.remove_file(lock_file_name, true)
7
+ end
8
+
9
+ def with_lock_file(lock_file_path, write_pid = true)
10
+ return false unless obtain_lock(lock_file_path, write_pid)
11
+ begin
12
+ yield
13
+ ensure
14
+ release_lock(lock_file_path)
15
+ end
16
+ end
17
+
18
+ private
19
+ def obtain_lock(lf, write_pid)
20
+ File.open(lf, File::CREAT | File::EXCL | File::WRONLY) do |o|
21
+ o.write(Process.pid) if write_pid
22
+ end
23
+ return true
24
+ rescue
25
+ return false
26
+ end
27
+
28
+ def release_lock(lf)
29
+ FileUtils.rm(lf, :force => true)
30
+ end
31
+
32
+ def has_lock?(lock_file_path)
33
+ return File.exist?(lock_file_path)
34
+ end
35
+
36
+ end
37
+
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'lockfile'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,55 @@
1
+ require 'helper'
2
+
3
+ class TestLockfile < Test::Unit::TestCase
4
+
5
+ LOCK_FILE = File.dirname(__FILE__) + 'test.lock'
6
+
7
+ class TestProcess
8
+ include Lockfile
9
+
10
+ def initialize
11
+ @launched = false
12
+ end
13
+
14
+ def launch
15
+ @launched = with_lock_file(LOCK_FILE, false) do
16
+ sleep 2
17
+ end
18
+ end
19
+
20
+ def launched?
21
+ @launched
22
+ end
23
+ end
24
+
25
+ class Lockfile::File < ::File
26
+ #slow open down once to demonstrate race condition
27
+ def self.open(*args)
28
+ unless @no_sleep
29
+ @no_sleep = true
30
+ sleep(1)
31
+ end
32
+ super
33
+ end
34
+ end
35
+
36
+
37
+ should "not allow to launch other processes while the previous one has lock" do
38
+ Lockfile.ensure_no_lock_file_exists(LOCK_FILE)
39
+
40
+ p1 = TestProcess.new
41
+ p2 = TestProcess.new
42
+ p3 = TestProcess.new
43
+
44
+ t1 = Thread.new { p1.launch }
45
+ t2 = Thread.new { p2.launch }
46
+ t3 = Thread.new { p3.launch }
47
+
48
+ t1.join
49
+ t2.join
50
+ t3.join
51
+
52
+ #only one should launch
53
+ assert_equal 1, [p1, p2, p3].select{|i| i.launched? }.size
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: schleyfox-lockfile
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - pavlo
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-28 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: "This Ruby gem helps to manage processes in your application so that new process won\xE2\x80\x99t start while the previous one is still running. It uses so known 'lock file' approach to figure out whether a process is running or not."
22
+ email: pavlo@cleverua.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.rdoc
30
+ files:
31
+ - .document
32
+ - .gitignore
33
+ - LICENSE
34
+ - README.rdoc
35
+ - Rakefile
36
+ - VERSION
37
+ - lib/lockfile.rb
38
+ - test/helper.rb
39
+ - test/test_lockfile.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/cleverua/lockfile
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.6
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Because everyone needs their own lockfile implementation. Helps you to maintain lock files that indicate that some resource is occupied by an other process
70
+ test_files:
71
+ - test/helper.rb
72
+ - test/test_lockfile.rb