run_checker 0.1.0

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: d84a1c4bfcaaa5ae2eabae3c2533340e731e138b
4
+ data.tar.gz: 4a3330e8ffb7b73bebce4ddddaf450e6fe1635fe
5
+ SHA512:
6
+ metadata.gz: 3c6c91d717434d64170d2f6191336c43f539270d0b548474c14bdf0a9b80f2a57a70395f64d467d6d843c7d3547ed0799a7dc3506cd72bd828581b172783184c
7
+ data.tar.gz: 5e1e9c03b335bde5df919fe943ea97f46f111af946896e43e7e425eed249485201a5a3a6607c526886a7b902ba943aaacf611bed4945cc7078c97649d8ec8f12
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'log4r'
4
+
5
+ gem 'rubocop', group: :development
6
+ gem 'rspec', group: :development
data/Gemfile.lock ADDED
@@ -0,0 +1,41 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ ast (2.0.0)
5
+ astrolabe (1.3.0)
6
+ parser (>= 2.2.0.pre.3, < 3.0)
7
+ diff-lcs (1.2.5)
8
+ log4r (1.1.10)
9
+ parser (2.2.0.pre.5)
10
+ ast (>= 1.1, < 3.0)
11
+ slop (~> 3.4, >= 3.4.5)
12
+ powerpack (0.0.9)
13
+ rainbow (2.0.0)
14
+ rspec (3.1.0)
15
+ rspec-core (~> 3.1.0)
16
+ rspec-expectations (~> 3.1.0)
17
+ rspec-mocks (~> 3.1.0)
18
+ rspec-core (3.1.7)
19
+ rspec-support (~> 3.1.0)
20
+ rspec-expectations (3.1.2)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.1.0)
23
+ rspec-mocks (3.1.3)
24
+ rspec-support (~> 3.1.0)
25
+ rspec-support (3.1.2)
26
+ rubocop (0.26.1)
27
+ astrolabe (~> 1.3)
28
+ parser (>= 2.2.0.pre.4, < 3.0)
29
+ powerpack (~> 0.0.6)
30
+ rainbow (>= 1.99.1, < 3.0)
31
+ ruby-progressbar (~> 1.4)
32
+ ruby-progressbar (1.6.0)
33
+ slop (3.6.0)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ log4r
40
+ rspec
41
+ rubocop
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2009 byplayer and takayoshi kohayakawa
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.
21
+
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ This is library to check duplicate running
2
+
3
+ This program is provided under MIT license.
data/Rakefile ADDED
@@ -0,0 +1,85 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'rdoc'
3
+ require 'rake'
4
+ require 'rdoc/task'
5
+ require 'rubygems/package_task'
6
+ require 'bundler/setup'
7
+
8
+ lib = File.expand_path(File.join('..', 'lib/'), __FILE__)
9
+ $LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib)
10
+ require 'run_checker/version'
11
+
12
+ begin
13
+ require 'rspec/core/rake_task'
14
+ rescue LoadError
15
+ puts 'no rspec'
16
+ else
17
+ RSpec::Core::RakeTask.new(:spec) do |t|
18
+ t.ruby_opts = '-w'
19
+ end
20
+
21
+ task default: :spec
22
+ end
23
+
24
+ begin
25
+ require 'rubocop/rake_task'
26
+ rescue LoadError
27
+ puts 'no rubocop'
28
+ else
29
+ RuboCop::RakeTask.new
30
+ end
31
+
32
+ Rake::RDocTask.new do |rdoc|
33
+ begin
34
+ version = File.read('VERSION').chomp
35
+ rescue
36
+ version = '0.0.0'
37
+ puts "No version is set in file VERSION. Set by default to #{version}"
38
+ end
39
+
40
+ rdoc.rdoc_dir = 'rdoc'
41
+ rdoc.title = "run_checker #{version}"
42
+ rdoc.rdoc_files.include('README*')
43
+ rdoc.rdoc_files.include('lib/**/*.rb')
44
+
45
+ rdoc.options += [
46
+ '-H', '-a', '-t', '-d',
47
+ '-f', 'darkfish', # This is the important bit
48
+ ]
49
+ end
50
+
51
+ # gem task
52
+ gemspec = Gem::Specification.new do |s|
53
+ s.name = 'run_checker'
54
+ s.version = RunChecker::VERSION::STRING
55
+ s.authors = ['byplayer']
56
+ s.date = '2014-10-07'
57
+ s.description = 'The duplicated running checker to support batch process'
58
+ s.email = ['byplayer100@gmail.com']
59
+ s.licenses = 'MIT license'
60
+ s.extra_rdoc_files = [
61
+ 'README.rdoc'
62
+ ]
63
+ s.files = FileList[
64
+ '[A-Z]*',
65
+ 'bin/**/*',
66
+ 'lib/**/*.rb',
67
+ 'test/**/*.rb',
68
+ 'doc/**/*',
69
+ 'spec/**/*.rb',
70
+ ]
71
+ s.homepage = 'https://github.com/byplayer/run_checker'
72
+ s.rdoc_options = ['--charset=UTF-8', '--line-numbers', '--inline-source',
73
+ '--main', 'README.rdoc', '-c UTF-8']
74
+ s.require_paths = ['lib']
75
+ s.rubyforge_project = 'run_checker'
76
+ s.rubygems_version = '0.1.0'
77
+ s.summary = 'The duplicated running checker'
78
+ s.test_files = Dir.glob('spec/**/*')
79
+
80
+ s.add_dependency('log4r', '~> 0')
81
+ end
82
+
83
+ Gem::PackageTask.new(gemspec) do |pkg|
84
+ pkg.gem_spec = gemspec
85
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,85 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'log4r'
3
+ require 'English'
4
+
5
+ # This is class to check duplicated run
6
+ class RunChecker
7
+ # [lock_path]
8
+ # lock file path
9
+ # [logger]
10
+ # logger instance, it is expected a instance of Logger in Log4r.
11
+ # If you don't set logger or you set nil for this param,
12
+ # log information will be put to stdout via StdoutOutputter.
13
+ def initialize(lock_path, logger = nil)
14
+ @lock_path = lock_path
15
+ @logger = logger
16
+
17
+ return if @logger
18
+
19
+ formatter = Log4r::PatternFormatter.new(pattern: '%d [%l]:%p: %M',
20
+ date_format: '%Y.%m.%d %H:%M:%S')
21
+ outputter = Log4r::StdoutOutputter.new('RunCheckerOutPutter',
22
+ formatter: formatter)
23
+ @logger = Log4r::Logger.new('RunChecker')
24
+ @logger.add(outputter)
25
+ end
26
+
27
+ # lock
28
+ # [return]
29
+ # If this method returns true, other process didn't run.
30
+ # If this method returns false, other process runs.
31
+ def lock
32
+ if File.exist? @lock_path
33
+ pid = 0
34
+ File.open(@lock_path, 'r') do |f|
35
+ pid = f.read.chomp!.to_i
36
+ end
37
+
38
+ if exist_process(pid)
39
+ @logger.info("other process is running: pid(#{pid})")
40
+ return false
41
+ else
42
+ @logger.warn("process was finished pid(#{pid}), " +
43
+ 'cleanup lock file and start new process')
44
+ File.delete(@lock_path)
45
+ end
46
+ end
47
+
48
+ File.open(@lock_path, 'w') do |f|
49
+ locked = f.flock(File::LOCK_EX | File::LOCK_NB)
50
+ if locked
51
+ f.puts $PROCESS_ID
52
+ return true
53
+ else
54
+ @logger.error("lock failed -> pid: #{$PID}")
55
+ return false
56
+ end
57
+ end
58
+ end
59
+
60
+ def cleanup
61
+ return unless File.exist? @lock_path
62
+
63
+ pid = 0
64
+ File.open(@lock_path, 'r') do |f|
65
+ pid = f.read.chomp!.to_i
66
+ end
67
+
68
+ if pid == $PROCESS_ID
69
+ File.delete(@lock_path)
70
+ else
71
+ @logger.info('lock file was created by other process')
72
+ end
73
+ end
74
+
75
+ private
76
+
77
+ def exist_process(pid)
78
+ Process.getpgid(pid)
79
+ return true
80
+ rescue => ex
81
+ @logger.info("check process error pid(#{pid}): #{ex}\n" +
82
+ ex.backtrace.join("\n "))
83
+ return false
84
+ end
85
+ end
@@ -0,0 +1,19 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ class RunChecker
4
+ # version information module
5
+ module VERSION
6
+ STRING =
7
+ File.open(
8
+ File.join(
9
+ File.dirname(__FILE__), '..', '..', 'VERSION'), 'r') do |f|
10
+ f.gets.chomp
11
+ end
12
+
13
+ if STRING =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)/
14
+ MAJOR = Regexp.last_match(1).to_i
15
+ MINOR = Regexp.last_match(2).to_i
16
+ TINY = Regexp.last_match(3).to_i
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,2 @@
1
+ require_relative File.join('run_checker', 'version')
2
+ require_relative File.join('run_checker', 'run_checker')
@@ -0,0 +1,50 @@
1
+ describe RunChecker do
2
+ before(:each) do
3
+ @lock_file_path =
4
+ File.expand_path(File.join('..', 'test.lock'),
5
+ File.dirname(__FILE__))
6
+ @run_checker = RunChecker.new(@lock_file_path)
7
+ end
8
+
9
+ after(:each) do
10
+ File.delete(@lock_file_path) if File.exist?(@lock_file_path)
11
+ end
12
+
13
+ it { expect(@run_checker.lock).to eq(true) }
14
+
15
+ it do
16
+ File.open(@lock_file_path, 'w') do |f|
17
+ f.puts 1
18
+ end
19
+
20
+ expect(@run_checker.lock).to eq(false)
21
+ end
22
+
23
+ it 'dead process' do
24
+ File.open(@lock_file_path, 'w') do |f|
25
+ f.puts 999_999_999
26
+ end
27
+
28
+ expect(@run_checker.lock).to eq(true)
29
+ end
30
+
31
+ it do
32
+ File.open(@lock_file_path, 'w') do |f|
33
+ f.puts $PROCESS_ID
34
+ end
35
+
36
+ @run_checker.cleanup
37
+
38
+ expect(File.exist?(@lock_file_path)).to eq(false)
39
+ end
40
+
41
+ it do
42
+ File.open(@lock_file_path, 'w') do |f|
43
+ f.puts 999_999_999
44
+ end
45
+
46
+ @run_checker.cleanup
47
+
48
+ expect(File.exist?(@lock_file_path)).to eq(true)
49
+ end
50
+ end
@@ -0,0 +1,96 @@
1
+ $LOAD_PATH.unshift(File.expand_path(File.join('..', 'lib')),
2
+ File.dirname(__FILE__))
3
+ require 'run_checker'
4
+
5
+ # This file was generated by the `rspec --init` command. Conventionally, all
6
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
7
+ # The generated `.rspec` file contains `--require spec_helper` which will
8
+ # cause this file to always be loaded, without a need to explicitly r
9
+ # equire it in any files.
10
+ #
11
+ # Given that it is always loaded, you are encouraged to keep this file as
12
+ # light-weight as possible. Requiring heavyweight dependencies from this file
13
+ # will add to the boot time of your test suite on EVERY test run, even for an
14
+ # individual file that may not need all of that loaded. Instead, consider making
15
+ # a separate helper file that requires the additional dependencies and performs
16
+ # the additional setup, and require it from the spec files that actually
17
+ # need it.
18
+ #
19
+ # The `.rspec` file also contains a few flags that are not defaults but that
20
+ # users commonly want.
21
+ #
22
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
23
+ RSpec.configure do |config|
24
+ # rspec-expectations config goes here. You can use an alternate
25
+ # assertion/expectation library such as wrong or the stdlib/minitest
26
+ # assertions if you prefer.
27
+ config.expect_with :rspec do |expectations|
28
+ # This option will default to `true` in RSpec 4. It makes the `description`
29
+ # and `failure_message` of custom matchers include text for helper methods
30
+ # defined using `chain`, e.g.:
31
+ # be_bigger_than(2).and_smaller_than(4).description
32
+ # # => "be bigger than 2 and smaller than 4"
33
+ # ...rather than:
34
+ # # => "be bigger than 2"
35
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
36
+ end
37
+
38
+ # rspec-mocks config goes here. You can use an alternate test double
39
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
40
+ config.mock_with :rspec do |mocks|
41
+ # Prevents you from mocking or stubbing a method that does not exist on
42
+ # a real object. This is generally recommended, and will default to
43
+ # `true` in RSpec 4.
44
+ mocks.verify_partial_doubles = true
45
+ end
46
+
47
+ # The settings below are suggested to provide a good initial experience
48
+ # with RSpec, but feel free to customize to your heart's content.
49
+ # # These two settings work together to allow you to limit a spec run
50
+ # # to individual examples or groups you care about by tagging them with
51
+ # # `:focus` metadata. When nothing is tagged with `:focus`, all examples
52
+ # # get run.
53
+ # config.filter_run :focus
54
+ # config.run_all_when_everything_filtered = true
55
+ #
56
+ # # Limits the available syntax to the non-monkey patched syntax that is
57
+ # # recommended.
58
+ # # For more details, see:
59
+ # # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
60
+ # # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
61
+ # # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
62
+ # config.disable_monkey_patching!
63
+ #
64
+ # # This setting enables warnings. It's recommended, but in some cases may
65
+ # # be too noisy due to issues in dependencies.
66
+ # config.warnings = true
67
+ #
68
+ # # Many RSpec users commonly either run the entire suite or an individual
69
+ # # file, and it's useful to allow more verbose output when running an
70
+ # # individual spec file.
71
+ # if config.files_to_run.one?
72
+ # # Use the documentation formatter for detailed output,
73
+ # # unless a formatter has already been configured
74
+ # # (e.g. via a command-line flag).
75
+ # config.default_formatter = 'doc'
76
+ # end
77
+ #
78
+ # # Print the 10 slowest examples and example groups at the
79
+ # # end of the spec run, to help surface which specs are running
80
+ # # particularly slow.
81
+ # config.profile_examples = 10
82
+ #
83
+ # # Run specs in random order to surface order dependencies. If you find an
84
+ # # order dependency and want to debug it, you can fix the order by
85
+ # # providing
86
+ # # the seed, which is printed after each run.
87
+ # # --seed 1234
88
+ # config.order = :random
89
+ #
90
+ # # Seed global randomization in this process using the `--seed` CLI option.
91
+ # # Setting this allows you to use `--seed` to deterministically reproduce
92
+ # # test failures related to randomization by passing the same
93
+ # # `--seed` value
94
+ # # as the one that triggered the failure.
95
+ # Kernel.srand config.seed
96
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: run_checker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - byplayer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: log4r
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: The duplicated running checker to support batch process
28
+ email:
29
+ - byplayer100@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files:
33
+ - README.rdoc
34
+ files:
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - MIT-LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/run_checker.rb
42
+ - lib/run_checker/run_checker.rb
43
+ - lib/run_checker/version.rb
44
+ - spec/run_checker/run_checker_spec.rb
45
+ - spec/spec_helper.rb
46
+ homepage: https://github.com/byplayer/run_checker
47
+ licenses:
48
+ - MIT license
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options:
52
+ - "--charset=UTF-8"
53
+ - "--line-numbers"
54
+ - "--inline-source"
55
+ - "--main"
56
+ - README.rdoc
57
+ - "-c UTF-8"
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project: run_checker
72
+ rubygems_version: 2.2.2
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: The duplicated running checker
76
+ test_files:
77
+ - spec/run_checker/run_checker_spec.rb
78
+ - spec/spec_helper.rb