rake_pid 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rake-pid.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 kenshin54
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,61 @@
1
+ # RakePid
2
+
3
+ RakePid is use to write a pid file when you run a rask task, and auto delete pid file when task is end or abort. It was useful when you need to monitor your rake task with [monit](http://mmonit.com/monit/) or [god](http://godrb.com/).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rake_pid'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rake_pid
18
+
19
+ ## Usage
20
+
21
+ ### Configure the directory
22
+
23
+ If no directory be configured, RakePid would write pid file to current directory.
24
+
25
+ ```ruby
26
+ RakePid.configure do
27
+ path 'tmp/pids'
28
+ end
29
+ ```
30
+
31
+ ### Write rake task
32
+
33
+ ```ruby
34
+ task :hello do |task|
35
+ with_pid(task) do
36
+ puts "world"
37
+ end
38
+ end
39
+ ```
40
+
41
+ ### Override pid file name
42
+
43
+ You can override pid file name by set PIDFILE
44
+
45
+ ```bash
46
+ rake hello PIDFILE=foo.pid
47
+ ```
48
+
49
+ ### Your can force delete pid file if something is going wrong by set FORCE to true
50
+
51
+ ```bash
52
+ rake hello FORCE=true
53
+ ```
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create new Pull Request
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib' << 'test'
6
+ test.pattern = 'test/**/*_test.rb'
7
+ test.verbose = false
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,8 @@
1
+ require 'rake-pid'
2
+
3
+ task :hello do |t|
4
+ with_pid(t) do
5
+ puts "world"
6
+ sleep(5)
7
+ end
8
+ end
@@ -0,0 +1,15 @@
1
+ require "rake_pid/version"
2
+ require 'rake_pid/core'
3
+
4
+ module RakePid
5
+
6
+ class TaskMissingError < StandardError; end
7
+ class FileAlreadyExistError < StandardError; end
8
+
9
+ def self.configure(&block)
10
+ Configuration.class_eval(&block)
11
+ end
12
+
13
+ end
14
+
15
+ self.extend RakePid::Core
@@ -0,0 +1,10 @@
1
+ module RakePid
2
+
3
+ class Configuration
4
+
5
+ def self.path(path = nil)
6
+ @path = path || @path || './'
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,34 @@
1
+ require 'rake_pid/configuration'
2
+
3
+ module RakePid
4
+
5
+ module Core
6
+
7
+ def with_pid(task)
8
+ if task.nil? || !task.respond_to?(:name)
9
+ raise TaskMissingError.new("You need to pass the task object to with_pid.")
10
+ end
11
+
12
+ pid_file_name = ENV['PIDFILE'] ? ENV['PIDFILE'] : "#{task.name.gsub(':', '-')}.pid"
13
+ pid_file = File.join(Configuration.path, pid_file_name)
14
+
15
+ if File.exists?(pid_file)
16
+ if ENV['FORCE'] == 'true'
17
+ File.delete(pid_file)
18
+ else
19
+ raise FileAlreadyExistError.new("The pid file '#{pid_file}' already exist, delete it and run task again.")
20
+ end
21
+ end
22
+
23
+ File.open(pid_file, 'w') { |f| f << Process.pid }
24
+ at_exit do
25
+ File.delete(pid_file) if File.exists?(pid_file)
26
+ end
27
+
28
+ yield
29
+
30
+ pid_file_name
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module RakePid
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rake_pid/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rake_pid"
8
+ gem.version = RakePid::VERSION
9
+ gem.authors = ["kenshin54"]
10
+ gem.email = ["himurakenshin54@gmail.com"]
11
+ gem.description = %q{Manage pid file when run a rake task}
12
+ gem.summary = %q{Manage pid file when run a rake task}
13
+ gem.homepage = "http://github.com/kenshin54/rake_pid"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency 'rake', '~> 0.9.2'
21
+ gem.add_development_dependency 'bundler', '~> 1.1'
22
+ gem.add_development_dependency 'minitest', '~> 3.3.0'
23
+ gem.add_development_dependency 'mocha', '~> 0.13.1'
24
+ end
@@ -0,0 +1,7 @@
1
+ require 'bundler/setup'
2
+ require 'minitest/spec'
3
+ require 'minitest/pride'
4
+ require 'minitest/autorun'
5
+ require 'mocha/setup'
6
+
7
+ require 'rake_pid'
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ describe RakePid::Core do
4
+ before(:each) do
5
+ @obj = Object.new
6
+ @obj.extend RakePid::Core
7
+ MyTask = Struct.new(:name) unless Kernel.const_defined?(:MyTask)
8
+ @task = MyTask.new('foo')
9
+ File.delete('./foo.pid') if File.exists?('./foo.pid')
10
+ end
11
+
12
+ it "should raise TaskMissingError when the argument is nil" do
13
+ proc { @obj.with_pid(nil) }.must_raise RakePid::TaskMissingError
14
+ end
15
+
16
+ it "should raise TaskMissingError when the argument didn't respond to :name" do
17
+ proc { @obj.with_pid(Object.new) }.must_raise RakePid::TaskMissingError
18
+ end
19
+
20
+ it "pid file name should same as task name" do
21
+ @obj.with_pid(@task) {}.must_equal 'foo.pid'
22
+ end
23
+
24
+ it "should override pid file name when PIDFILE environment variable has been seted" do
25
+ ENV['PIDFILE'] = 'bar.pid'
26
+ @obj.with_pid(@task) {}.must_equal 'bar.pid'
27
+ ENV['PIDFILE'] = nil
28
+ end
29
+
30
+ it "should raise FileAlreadyExistError when pid file already exist" do
31
+ FileUtils.touch './foo.pid'
32
+ at_exit do
33
+ File.delete('./foo.pid') if File.exists?('./foo.pid')
34
+ end
35
+ proc { @obj.with_pid(@task) {} }.must_raise RakePid::FileAlreadyExistError
36
+ end
37
+
38
+ it "should delete pid file when FORCE environment variable has been set to true even pid file already exist" do
39
+ FileUtils.touch './foo.pid'
40
+ at_exit do
41
+ File.delete('./foo.pid') if File.exists?('./foo.pid')
42
+ end
43
+ ENV['FORCE'] = 'true'
44
+ File.expects(:delete).with('./foo.pid')
45
+ @obj.with_pid(@task) {}
46
+ ENV['FORCE'] = nil
47
+ end
48
+
49
+ it "should create pid file and delete pid file when task is end" do
50
+ File.expects(:open).with('./foo.pid', 'w')
51
+ @obj.with_pid(@task) {}
52
+ File.exists?('./foo.pid').must_equal false
53
+ end
54
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rake_pid
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - kenshin54
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.2
20
+ none: false
21
+ prerelease: false
22
+ name: rake
23
+ requirement: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: 0.9.2
28
+ none: false
29
+ type: :development
30
+ - !ruby/object:Gem::Dependency
31
+ version_requirements: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: '1.1'
36
+ none: false
37
+ prerelease: false
38
+ name: bundler
39
+ requirement: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '1.1'
44
+ none: false
45
+ type: :development
46
+ - !ruby/object:Gem::Dependency
47
+ version_requirements: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ~>
50
+ - !ruby/object:Gem::Version
51
+ version: 3.3.0
52
+ none: false
53
+ prerelease: false
54
+ name: minitest
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ version: 3.3.0
60
+ none: false
61
+ type: :development
62
+ - !ruby/object:Gem::Dependency
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: 0.13.1
68
+ none: false
69
+ prerelease: false
70
+ name: mocha
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 0.13.1
76
+ none: false
77
+ type: :development
78
+ description: Manage pid file when run a rake task
79
+ email:
80
+ - himurakenshin54@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - example/rakefile
91
+ - lib/rake_pid.rb
92
+ - lib/rake_pid/configuration.rb
93
+ - lib/rake_pid/core.rb
94
+ - lib/rake_pid/version.rb
95
+ - rake_pid.gemspec
96
+ - test/test_helper.rb
97
+ - test/unit/core_test.rb
98
+ homepage: http://github.com/kenshin54/rake_pid
99
+ licenses: []
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ segments:
110
+ - 0
111
+ hash: 4094812768928479822
112
+ none: false
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ segments:
119
+ - 0
120
+ hash: 4094812768928479822
121
+ none: false
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 1.8.24
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: Manage pid file when run a rake task
128
+ test_files:
129
+ - test/test_helper.rb
130
+ - test/unit/core_test.rb