guard-rack 1.2.2 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,36 +1,30 @@
1
1
  require 'fileutils'
2
+ require 'timeout'
3
+ require 'spoon'
2
4
 
3
5
  module Guard
4
6
  class RackRunner
5
7
 
6
- MAX_WAIT_COUNT = 10
7
-
8
- attr_reader :options
8
+ attr_reader :options, :pid
9
9
 
10
10
  def initialize(options)
11
11
  @options = options
12
12
  end
13
13
 
14
- def kill pid
15
- system %{kill -INT #{pid}}
16
- $?.exitstatus
17
- end
18
-
19
14
  def start
20
15
  kill_unmanaged_pid! if options[:force_run]
21
- run_rack_command!
22
- wait_for_pid
16
+ @pid = run_rack_command!
17
+ true
23
18
  end
24
19
 
25
20
  def stop
26
- # Rely on kill_unmanaged_pid if there's no pid file
27
- return true unless File.file?(pid_file)
28
-
29
- if kill(pid) == 0
30
- wait_for_no_pid
31
- remove_pid_file
32
- else
33
- UI.info "Rackup exited with non-zero exit status whilst trying to stop."
21
+ # Rely on kill_unmanaged_pid if there's no pid
22
+ return true unless @pid
23
+
24
+ exitstatus = kill(@pid)
25
+ @pid = nil
26
+ if exitstatus && exitstatus != 0
27
+ UI.info "Rackup exited with non-zero exit status (#{exitstatus}) whilst trying to stop."
34
28
  return false
35
29
  end
36
30
 
@@ -41,86 +35,72 @@ module Guard
41
35
  stop and start
42
36
  end
43
37
 
44
- def build_rack_command
45
- rack_options = [
46
- options[:config],
47
- '--env', options[:environment],
48
- '--port', options[:port],
49
- '--pid', pid_file
50
- ]
51
-
52
- rack_options << '--daemonize' if options[:daemon]
53
- rack_options << '--debug' if options[:debugger]
54
- rack_options << ["--server",options[:server]] if options[:server]
55
-
56
- %{sh -c 'cd #{Dir.pwd} && rackup #{rack_options.join(' ')} &'}
57
- end
58
-
59
- def pid_file
60
- File.expand_path(".guard-rack-#{options[:environment]}.pid")
61
- end
62
-
63
- def pid
64
- File.file?(pid_file) ? File.read(pid_file).to_i : nil
65
- end
66
-
67
- def remove_pid_file
68
- FileUtils.rm pid_file if File.exist? pid_file
69
- end
70
-
71
- def sleep_time
72
- options[:timeout].to_f / MAX_WAIT_COUNT.to_f
73
- end
74
-
75
38
  private
76
-
77
- def run_rack_command!
78
- system build_rack_command
79
- end
80
39
 
81
- def has_pid?
82
- File.file?(pid_file)
83
- end
40
+ def build_rack_command
41
+ command = %w{rackup}
42
+ command.push(
43
+ options[:config],
44
+ '--env', options[:environment].to_s,
45
+ '--port', options[:port].to_s
46
+ )
84
47
 
85
- def wait_for_pid_action
86
- sleep sleep_time
87
- end
48
+ command << '--daemonize' if options[:daemon]
49
+ command << '--debug' if options[:debugger]
50
+ command.push("--server", options[:server].to_s) if options[:server]
88
51
 
89
- def kill_unmanaged_pid!
90
- if pid = unmanaged_pid
91
- kill pid
92
- wait_for_no_pid
93
- remove_pid_file
52
+ command
94
53
  end
95
- end
96
54
 
97
- def unmanaged_pid
98
- %x{lsof -n -i TCP:#{options[:port]}}.each_line { |line|
99
- if line["*:#{options[:port]} "]
100
- return line.split("\s")[1].to_i
55
+ def kill(pid, force = false)
56
+ result = -1
57
+
58
+ UI.debug("Trying to kill Rack (PID #{pid})...")
59
+ if !force
60
+ Process.kill("INT", pid)
61
+ begin
62
+ Timeout.timeout(options[:timeout]) do
63
+ _, status = Process.wait2(pid)
64
+ result = status.exitstatus
65
+ UI.debug("Killed Rack (Exit status: #{result})")
66
+ end
67
+ rescue Timeout::Error
68
+ UI.debug("Couldn't kill Rack with INT, switching to TERM")
69
+ force = true
70
+ end
101
71
  end
102
- }
103
- nil
104
- end
105
72
 
106
- private
73
+ if force
74
+ Process.kill("TERM", pid)
75
+ end
76
+
77
+ result
78
+ end
107
79
 
108
- def wait_for_pid
109
- wait_for_pid_loop
80
+ def run_rack_command!
81
+ command = build_rack_command
82
+ UI.debug("Running Rack with command: #{command.inspect}")
83
+ spawn(*command)
110
84
  end
111
85
 
112
- def wait_for_no_pid
113
- wait_for_pid_loop(false)
86
+ def spawn(* args)
87
+ Spoon.spawnp(* args)
114
88
  end
115
89
 
116
- def wait_for_pid_loop(check_for_existince = true)
117
- count = 0
118
- while !(check_for_existince ? has_pid? : !has_pid?) && count < MAX_WAIT_COUNT
119
- wait_for_pid_action
120
- count += 1
90
+ def kill_unmanaged_pid!
91
+ if pid = unmanaged_pid
92
+ kill(pid, true)
121
93
  end
122
- !(count == MAX_WAIT_COUNT)
123
94
  end
124
-
95
+
96
+ def unmanaged_pid
97
+ %x{lsof -n -i TCP:#{options[:port]}}.each_line { |line|
98
+ if line["*:#{options[:port]} "]
99
+ return line.split("\s")[1].to_i
100
+ end
101
+ }
102
+ nil
103
+ end
104
+
125
105
  end
126
106
  end
@@ -1,6 +1,6 @@
1
1
  module Guard
2
2
  module RackVersion
3
- VERSION = '1.2.2'
3
+ VERSION = '1.3.0'
4
4
  end
5
5
  end
6
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guard-rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-19 00:00:00.000000000 Z
12
+ date: 2013-01-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: guard
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
29
  version: '1.1'
30
+ - !ruby/object:Gem::Dependency
31
+ name: spoon
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.0.1
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.0.1
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: rb-fsevent
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -171,6 +187,22 @@ dependencies:
171
187
  - - ! '>='
172
188
  - !ruby/object:Gem::Version
173
189
  version: '0'
190
+ - !ruby/object:Gem::Dependency
191
+ name: rack
192
+ requirement: !ruby/object:Gem::Requirement
193
+ none: false
194
+ requirements:
195
+ - - ! '>='
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
198
+ type: :development
199
+ prerelease: false
200
+ version_requirements: !ruby/object:Gem::Requirement
201
+ none: false
202
+ requirements:
203
+ - - ! '>='
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
174
206
  description:
175
207
  email: dblock@dblock.org
176
208
  executables: []
@@ -201,7 +233,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
201
233
  version: '0'
202
234
  segments:
203
235
  - 0
204
- hash: -3499392218067664332
236
+ hash: 474712234159174403
205
237
  required_rubygems_version: !ruby/object:Gem::Requirement
206
238
  none: false
207
239
  requirements: