cleverua-lockfile 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -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.
@@ -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 operational systems.
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 operational system
27
+
28
+ == INSTALL:
29
+
30
+ sudo gem install 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
+
@@ -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
@@ -0,0 +1,36 @@
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 if has_lock?(lock_file_path)
11
+
12
+ begin
13
+ obtain_lock(lock_file_path, write_pid)
14
+ yield
15
+ ensure
16
+ release_lock(lock_file_path)
17
+ end
18
+ end
19
+
20
+ private
21
+ def obtain_lock(lf, write_pid)
22
+ open(lf, "w+") do |o|
23
+ o.write(Process.pid) if write_pid
24
+ end
25
+ end
26
+
27
+ def release_lock(lf)
28
+ FileUtils.rm(lf, :force => true)
29
+ end
30
+
31
+ def has_lock?(lock_file_path)
32
+ return File.exist?(lock_file_path)
33
+ end
34
+
35
+ end
36
+
@@ -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,42 @@
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
+ should "not allow to launch other processes while the previous one has lock" do
26
+ p1 = TestProcess.new
27
+ p2 = TestProcess.new
28
+ p3 = TestProcess.new
29
+
30
+ t1 = Thread.new { p1.launch }
31
+ t2 = Thread.new { p2.launch }
32
+ t3 = Thread.new { p3.launch }
33
+
34
+ t1.join
35
+ t2.join
36
+ t3.join
37
+
38
+ assert p1.launched?
39
+ assert !p2.launched?
40
+ assert !p3.launched?
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cleverua-lockfile
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - pavlo
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-06 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ 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."
23
+ email: pavlo@cleverua.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - LICENSE
30
+ - README.rdoc
31
+ files:
32
+ - .document
33
+ - .gitignore
34
+ - LICENSE
35
+ - README.rdoc
36
+ - Rakefile
37
+ - VERSION
38
+ - lib/lockfile.rb
39
+ - test/helper.rb
40
+ - test/test_lockfile.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/cleverua/lockfile
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Helps you to maintain lock files that indicate that some resource is occupied by an other process
75
+ test_files:
76
+ - test/helper.rb
77
+ - test/test_lockfile.rb