jettywrapper 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/.gitmodules +0 -3
- data/.rspec +1 -0
- data/.travis.yml +16 -0
- data/CONTRIBUTING.md +113 -0
- data/Gemfile +5 -1
- data/History.txt +25 -0
- data/LICENSE +1086 -0
- data/README.textile +30 -60
- data/Rakefile +14 -10
- data/config/jetty.yml +5 -4
- data/gemfiles/rails4.1.gemfile +10 -0
- data/gemfiles/rails4.2.beta.gemfile +10 -0
- data/jettywrapper.gemspec +8 -15
- data/lib/jettywrapper/version.rb +3 -1
- data/lib/jettywrapper.rb +344 -217
- data/lib/tasks/jettywrapper.rake +17 -1
- data/spec/lib/jettywrapper_integration_spec.rb +49 -26
- data/spec/lib/jettywrapper_spec.rb +332 -78
- data/spec/spec_helper.rb +19 -5
- metadata +116 -178
- data/.rvmrc +0 -1
data/lib/jettywrapper.rb
CHANGED
|
@@ -1,172 +1,262 @@
|
|
|
1
|
-
# Jettywrapper is a Singleton class, so you can only create one jetty instance at a time.
|
|
2
|
-
#require 'logger'
|
|
3
|
-
require 'loggable'
|
|
4
1
|
require 'singleton'
|
|
5
|
-
require '
|
|
2
|
+
require 'fileutils'
|
|
3
|
+
require 'shellwords'
|
|
6
4
|
require 'socket'
|
|
7
5
|
require 'timeout'
|
|
8
6
|
require 'childprocess'
|
|
7
|
+
require 'active_support/benchmarkable'
|
|
9
8
|
require 'active_support/core_ext/hash'
|
|
9
|
+
require 'erb'
|
|
10
|
+
require 'yaml'
|
|
11
|
+
require 'logger'
|
|
10
12
|
|
|
11
13
|
Dir[File.expand_path(File.join(File.dirname(__FILE__),"tasks/*.rake"))].each { |ext| load ext } if defined?(Rake)
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
# Jettywrapper is a Singleton class, so you can only create one jetty instance at a time.
|
|
14
16
|
class Jettywrapper
|
|
15
|
-
|
|
17
|
+
|
|
16
18
|
include Singleton
|
|
17
|
-
include
|
|
18
|
-
|
|
19
|
-
attr_accessor :
|
|
20
|
-
attr_accessor :port #
|
|
21
|
-
attr_accessor :
|
|
22
|
-
attr_accessor :
|
|
23
|
-
attr_accessor :
|
|
24
|
-
attr_accessor :solr_home # Where is solr located? Default is jetty_home/solr
|
|
19
|
+
include ActiveSupport::Benchmarkable
|
|
20
|
+
|
|
21
|
+
attr_accessor :jetty_home # Jetty's home directory
|
|
22
|
+
attr_accessor :port # Jetty's port. Default is 8888. Note that attribute is named port, but params passed in expect :jetty_port
|
|
23
|
+
attr_accessor :startup_wait # How many seconds to wait for jetty to spin up. Default is 5.
|
|
24
|
+
attr_accessor :quiet # true (default) to reduce Jetty's output
|
|
25
|
+
attr_accessor :solr_home # Solr's home directory. Default is jetty_home/solr
|
|
25
26
|
attr_accessor :base_path # The root of the application. Used for determining where log files and PID files should go.
|
|
26
27
|
attr_accessor :java_opts # Options to pass to java (ex. ["-Xmx512mb", "-Xms128mb"])
|
|
27
|
-
|
|
28
|
+
attr_accessor :jetty_opts # Options to pass to jetty (ex. ["etc/my_jetty.xml", "etc/other.xml"] as in http://wiki.eclipse.org/Jetty/Reference/jetty.xml_usage
|
|
29
|
+
|
|
28
30
|
# configure the singleton with some defaults
|
|
29
31
|
def initialize(params = {})
|
|
30
|
-
|
|
31
|
-
@base_path = Rails.root
|
|
32
|
-
else
|
|
33
|
-
@base_path = "."
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
logger.debug 'Initializing jettywrapper'
|
|
32
|
+
self.base_path = self.class.app_root
|
|
37
33
|
end
|
|
38
|
-
|
|
39
|
-
# Methods inside of the class << self block can be called directly on Jettywrapper, as class methods.
|
|
34
|
+
|
|
35
|
+
# Methods inside of the class << self block can be called directly on Jettywrapper, as class methods.
|
|
40
36
|
# Methods outside the class << self block must be called on Jettywrapper.instance, as instance methods.
|
|
41
37
|
class << self
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
38
|
+
|
|
39
|
+
attr_writer :hydra_jetty_version, :url, :tmp_dir, :jetty_dir, :env
|
|
40
|
+
|
|
41
|
+
def hydra_jetty_version
|
|
42
|
+
@hydra_jetty_version ||= 'v8.1.1'
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def url
|
|
46
|
+
@url ||= defined?(ZIP_URL) ? ZIP_URL : "https://github.com/projecthydra/hydra-jetty/archive/#{hydra_jetty_version}.zip"
|
|
47
|
+
@url
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def tmp_dir
|
|
51
|
+
@tmp_dir ||= 'tmp'
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def zip_file
|
|
55
|
+
ENV['JETTY_ZIP'] || File.join(tmp_dir, url.split('/').last)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def jetty_dir
|
|
59
|
+
@jetty_dir ||= 'jetty'
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def download(url = nil)
|
|
63
|
+
return if File.exists? zip_file
|
|
64
|
+
self.url = url if url
|
|
65
|
+
logger.info "Downloading jetty at #{self.url} ..."
|
|
66
|
+
FileUtils.mkdir tmp_dir unless File.exists? tmp_dir
|
|
67
|
+
system "curl -L #{self.url} -o #{zip_file}"
|
|
68
|
+
abort "Unable to download jetty from #{self.url}" unless $?.success?
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def unzip
|
|
72
|
+
download unless File.exists? zip_file
|
|
73
|
+
logger.info "Unpacking #{zip_file}..."
|
|
74
|
+
tmp_save_dir = File.join tmp_dir, 'jetty_generator'
|
|
75
|
+
system "unzip -d #{tmp_save_dir} -qo #{zip_file}"
|
|
76
|
+
abort "Unable to unzip #{zip_file} into tmp_save_dir/" unless $?.success?
|
|
77
|
+
|
|
78
|
+
# Remove the old jetty directory if it exists
|
|
79
|
+
system "rm -r #{jetty_dir}" if File.directory?(jetty_dir)
|
|
80
|
+
|
|
81
|
+
# Move the expanded zip file into the final destination.
|
|
82
|
+
expanded_dir = expanded_zip_dir(tmp_save_dir)
|
|
83
|
+
system "mv #{expanded_dir} #{jetty_dir}"
|
|
84
|
+
abort "Unable to move #{expanded_dir} into #{jetty_dir}/" unless $?.success?
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def expanded_zip_dir(tmp_save_dir)
|
|
88
|
+
# This old way is more specific, but won't work for blacklight-jetty
|
|
89
|
+
#expanded_dir = Dir[File.join(tmp_save_dir, "hydra-jetty-*")].first
|
|
90
|
+
Dir[File.join(tmp_save_dir, "*")].first
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def clean
|
|
94
|
+
system "rm -rf #{jetty_dir}"
|
|
95
|
+
unzip
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def reset_config
|
|
99
|
+
@app_root = nil
|
|
100
|
+
@env = nil
|
|
101
|
+
@url = nil
|
|
102
|
+
@hydra_jetty_version = nil
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def app_root
|
|
106
|
+
return @app_root if @app_root
|
|
107
|
+
@app_root = Rails.root if defined?(Rails) and defined?(Rails.root)
|
|
108
|
+
@app_root ||= APP_ROOT if defined?(APP_ROOT)
|
|
109
|
+
@app_root ||= '.'
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def env
|
|
113
|
+
@env ||= begin
|
|
114
|
+
case
|
|
115
|
+
when ENV['JETTYWRAPPER_ENV']
|
|
116
|
+
ENV['JETTYWRAPPER_ENV']
|
|
117
|
+
when defined?(Rails) && Rails.respond_to?(:env)
|
|
118
|
+
Rails.env
|
|
119
|
+
when ENV['RAILS_ENV']
|
|
120
|
+
ENV['RAILS_ENV']
|
|
121
|
+
when ENV['environment']
|
|
122
|
+
ENV['environment']
|
|
123
|
+
else
|
|
124
|
+
default_environment
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def default_environment
|
|
130
|
+
'development'
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def load_config(config_name = env)
|
|
134
|
+
@env = config_name
|
|
135
|
+
jetty_file = "#{app_root}/config/jetty.yml"
|
|
136
|
+
|
|
137
|
+
unless File.exists?(jetty_file)
|
|
138
|
+
logger.warn "Didn't find expected jettywrapper config file at #{jetty_file}, using default file instead."
|
|
139
|
+
jetty_file = File.expand_path("../config/jetty.yml", File.dirname(__FILE__))
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
begin
|
|
143
|
+
jetty_erb = ERB.new(IO.read(jetty_file)).result(binding)
|
|
144
|
+
rescue
|
|
145
|
+
raise("jetty.yml was found, but could not be parsed with ERB. \n#{$!.inspect}")
|
|
51
146
|
end
|
|
52
|
-
|
|
147
|
+
|
|
53
148
|
begin
|
|
54
|
-
|
|
55
|
-
rescue
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
149
|
+
jetty_yml = YAML::load(jetty_erb)
|
|
150
|
+
rescue
|
|
151
|
+
raise("jetty.yml was found, but could not be parsed.\n")
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
if jetty_yml.nil? || !jetty_yml.is_a?(Hash)
|
|
155
|
+
raise("jetty.yml was found, but was blank or malformed.\n")
|
|
59
156
|
end
|
|
60
|
-
|
|
157
|
+
|
|
158
|
+
config = jetty_yml.with_indifferent_access
|
|
159
|
+
config[config_name] || config['default'.freeze]
|
|
61
160
|
end
|
|
62
|
-
|
|
63
161
|
|
|
64
|
-
# Set the jetty parameters. It accepts a Hash of symbols.
|
|
162
|
+
# Set the jetty parameters. It accepts a Hash of symbols.
|
|
65
163
|
# @param [Hash<Symbol>] params
|
|
66
|
-
#
|
|
67
|
-
#
|
|
68
|
-
#
|
|
69
|
-
#
|
|
70
|
-
#
|
|
71
|
-
#
|
|
164
|
+
# :jetty_home Required. Jetty's home direcotry
|
|
165
|
+
# :jetty_port Jetty's port. Default is 8888. Note that attribute is named port, but params passed in expect :jetty_port
|
|
166
|
+
# :startup_wait How many seconds to wait for jetty to spin up. Default is 5. If jetty doesn't finish spinning up, tests can fail because they can't reach jetty.
|
|
167
|
+
# :solr_home Solr's home directory. Default is jetty_home/solr
|
|
168
|
+
# :quiet Keep True(default) to reduce jetty's output
|
|
169
|
+
# :java_opts options to pass to the jvm (ex. ["-Xmx512mb", "-Xms128mb"])
|
|
170
|
+
# :jetty_opts options to pass to jetty (ex. ["etc/my_jetty.xml", "etc/other.xml"] as in http://wiki.eclipse.org/Jetty/Reference/jetty.xml_usage
|
|
72
171
|
def configure(params = {})
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
hydra_server.port = params[:jetty_port] || 8888
|
|
85
|
-
hydra_server.startup_wait = params[:startup_wait] || 5
|
|
86
|
-
hydra_server.java_opts = params[:java_opts] || []
|
|
87
|
-
return hydra_server
|
|
172
|
+
jetty_server = self.instance
|
|
173
|
+
jetty_server.reset_process!
|
|
174
|
+
jetty_server.quiet = params[:quiet].nil? ? true : params[:quiet]
|
|
175
|
+
|
|
176
|
+
jetty_server.jetty_home = params[:jetty_home] || File.expand_path(File.join(app_root, 'jetty'))
|
|
177
|
+
jetty_server.solr_home = params[:solr_home] || File.join( jetty_server.jetty_home, "solr")
|
|
178
|
+
jetty_server.port = params[:jetty_port] || 8888
|
|
179
|
+
jetty_server.startup_wait = params[:startup_wait] || 5
|
|
180
|
+
jetty_server.java_opts = params[:java_opts] || []
|
|
181
|
+
jetty_server.jetty_opts = params[:jetty_opts] || []
|
|
182
|
+
return jetty_server
|
|
88
183
|
end
|
|
89
|
-
|
|
90
|
-
|
|
184
|
+
|
|
91
185
|
# Wrap the tests. Startup jetty, yield to the test task, capture any errors, shutdown
|
|
92
|
-
# jetty, and return the error.
|
|
186
|
+
# jetty, and return the error.
|
|
93
187
|
# @example Using this method in a rake task
|
|
94
188
|
# require 'jettywrapper'
|
|
95
189
|
# desc "Spin up jetty and run tests against it"
|
|
96
190
|
# task :newtest do
|
|
97
|
-
# jetty_params = {
|
|
98
|
-
# :jetty_home => "/path/to/jetty",
|
|
99
|
-
# :quiet => false,
|
|
100
|
-
# :jetty_port => 8983,
|
|
101
|
-
# :startup_wait => 30
|
|
191
|
+
# jetty_params = {
|
|
192
|
+
# :jetty_home => "/path/to/jetty",
|
|
193
|
+
# :quiet => false,
|
|
194
|
+
# :jetty_port => 8983,
|
|
195
|
+
# :startup_wait => 30,
|
|
196
|
+
# :jetty_opts => "/etc/jetty.xml"
|
|
102
197
|
# }
|
|
103
|
-
# error = Jettywrapper.wrap(jetty_params) do
|
|
104
|
-
# Rake::Task["rake:spec"].invoke
|
|
105
|
-
# Rake::Task["rake:cucumber"].invoke
|
|
106
|
-
# end
|
|
198
|
+
# error = Jettywrapper.wrap(jetty_params) do
|
|
199
|
+
# Rake::Task["rake:spec"].invoke
|
|
200
|
+
# Rake::Task["rake:cucumber"].invoke
|
|
201
|
+
# end
|
|
107
202
|
# raise "test failures: #{error}" if error
|
|
108
203
|
# end
|
|
109
204
|
def wrap(params)
|
|
110
205
|
error = false
|
|
111
206
|
jetty_server = self.configure(params)
|
|
112
|
-
# jetty_server = self.instance
|
|
113
|
-
# jetty_server.quiet = params[:quiet] || true
|
|
114
|
-
# jetty_server.jetty_home = params[:jetty_home]
|
|
115
|
-
# jetty_server.solr_home = params[:solr_home]
|
|
116
|
-
# jetty_server.port = params[:jetty_port] || 8888
|
|
117
|
-
# jetty_server.startup_wait = params[:startup_wait] || 5
|
|
118
207
|
|
|
119
208
|
begin
|
|
120
|
-
# puts "starting jetty on #{RUBY_PLATFORM}"
|
|
121
209
|
jetty_server.start
|
|
122
|
-
sleep jetty_server.startup_wait
|
|
123
210
|
yield
|
|
124
211
|
rescue
|
|
125
212
|
error = $!
|
|
126
|
-
|
|
213
|
+
logger.error "*** Error starting jetty: #{error}"
|
|
127
214
|
ensure
|
|
128
215
|
# puts "stopping jetty server"
|
|
129
216
|
jetty_server.stop
|
|
130
217
|
end
|
|
131
218
|
|
|
219
|
+
raise error if error
|
|
220
|
+
|
|
132
221
|
return error
|
|
133
222
|
end
|
|
134
|
-
|
|
223
|
+
|
|
135
224
|
# Convenience method for configuring and starting jetty with one command
|
|
136
225
|
# @param [Hash] params: The configuration to use for starting jetty
|
|
137
|
-
# @example
|
|
138
|
-
# Jettywrapper.
|
|
226
|
+
# @example
|
|
227
|
+
# Jettywrapper.start(:jetty_home => '/path/to/jetty', :jetty_port => '8983')
|
|
139
228
|
def start(params)
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
229
|
+
unzip unless File.exists? jetty_dir
|
|
230
|
+
Jettywrapper.configure(params)
|
|
231
|
+
Jettywrapper.instance.start
|
|
232
|
+
return Jettywrapper.instance
|
|
143
233
|
end
|
|
144
|
-
|
|
234
|
+
|
|
145
235
|
# Convenience method for configuring and starting jetty with one command. Note
|
|
146
|
-
# that for stopping, only the :jetty_home value is required (including other values won't
|
|
147
|
-
# hurt anything, though).
|
|
236
|
+
# that for stopping, only the :jetty_home value is required (including other values won't
|
|
237
|
+
# hurt anything, though).
|
|
148
238
|
# @param [Hash] params: The jetty_home to use for stopping jetty
|
|
149
239
|
# @return [Jettywrapper.instance]
|
|
150
|
-
# @example
|
|
240
|
+
# @example
|
|
151
241
|
# Jettywrapper.stop_with_params(:jetty_home => '/path/to/jetty')
|
|
152
242
|
def stop(params)
|
|
153
243
|
Jettywrapper.configure(params)
|
|
154
244
|
Jettywrapper.instance.stop
|
|
155
245
|
return Jettywrapper.instance
|
|
156
246
|
end
|
|
157
|
-
|
|
247
|
+
|
|
158
248
|
# Determine whether the jetty at the given jetty_home is running
|
|
159
249
|
# @param [Hash] params: :jetty_home is required. Which jetty do you want to check the status of?
|
|
160
250
|
# @return [Boolean]
|
|
161
251
|
# @example
|
|
162
252
|
# Jettywrapper.is_jetty_running?(:jetty_home => '/path/to/jetty')
|
|
163
|
-
def is_jetty_running?(params)
|
|
253
|
+
def is_jetty_running?(params)
|
|
164
254
|
Jettywrapper.configure(params)
|
|
165
255
|
pid = Jettywrapper.instance.pid
|
|
166
256
|
return false unless pid
|
|
167
257
|
true
|
|
168
258
|
end
|
|
169
|
-
|
|
259
|
+
|
|
170
260
|
# Return the pid of the specified jetty, or return nil if it isn't running
|
|
171
261
|
# @param [Hash] params: :jetty_home is required.
|
|
172
262
|
# @return [Fixnum] or [nil]
|
|
@@ -178,7 +268,7 @@ class Jettywrapper
|
|
|
178
268
|
return nil unless pid
|
|
179
269
|
pid
|
|
180
270
|
end
|
|
181
|
-
|
|
271
|
+
|
|
182
272
|
# Check to see if the port is open so we can raise an error if we have a conflict
|
|
183
273
|
# @param [Fixnum] port the port to check
|
|
184
274
|
# @return [Boolean]
|
|
@@ -202,8 +292,8 @@ class Jettywrapper
|
|
|
202
292
|
|
|
203
293
|
return false
|
|
204
294
|
end
|
|
205
|
-
|
|
206
|
-
# Check to see if the pid is actually running. This only works on unix.
|
|
295
|
+
|
|
296
|
+
# Check to see if the pid is actually running. This only works on unix.
|
|
207
297
|
def is_pid_running?(pid)
|
|
208
298
|
begin
|
|
209
299
|
return Process.getpgid(pid) != -1
|
|
@@ -211,129 +301,166 @@ class Jettywrapper
|
|
|
211
301
|
return false
|
|
212
302
|
end
|
|
213
303
|
end
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
304
|
+
|
|
305
|
+
def logger=(logger)
|
|
306
|
+
@@logger = logger
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# If ::Rails.logger is defined and is not nil, it will be returned.
|
|
310
|
+
# If no logger has been defined, a new STDOUT Logger will be created.
|
|
311
|
+
def logger
|
|
312
|
+
@@logger ||= defined?(::Rails) && Rails.logger ? ::Rails.logger : ::Logger.new(STDOUT)
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
end #end of class << self
|
|
316
|
+
|
|
317
|
+
def logger
|
|
318
|
+
self.class.logger
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
# What command is being run to invoke jetty?
|
|
322
|
+
def jetty_command
|
|
323
|
+
["java", java_variables, java_opts, "-jar", "start.jar", jetty_opts].flatten
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
def java_variables
|
|
327
|
+
["-Djetty.port=#{@port}",
|
|
328
|
+
"-Dsolr.solr.home=#{Shellwords.escape(@solr_home)}"]
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
# Start the jetty server. Check the pid file to see if it is running already,
|
|
332
|
+
# and stop it if so. After you start jetty, write the PID to a file.
|
|
333
|
+
# This is the instance start method. It must be called on Jettywrapper.instance
|
|
334
|
+
# You're probably better off using Jettywrapper.start(:jetty_home => "/path/to/jetty")
|
|
335
|
+
# @example
|
|
336
|
+
# Jettywrapper.configure(params)
|
|
337
|
+
# Jettywrapper.instance.start
|
|
338
|
+
# return Jettywrapper.instance
|
|
339
|
+
def start
|
|
340
|
+
logger.debug "Starting jetty with these values: "
|
|
341
|
+
logger.debug "jetty_home: #{@jetty_home}"
|
|
342
|
+
logger.debug "jetty_command: #{jetty_command.join(' ')}"
|
|
343
|
+
|
|
344
|
+
# Check to see if we can start.
|
|
345
|
+
# 1. If there is a pid, check to see if it is really running
|
|
346
|
+
# 2. Check to see if anything is blocking the port we want to use
|
|
347
|
+
if pid
|
|
348
|
+
if Jettywrapper.is_pid_running?(pid)
|
|
349
|
+
raise("Server is already running with PID #{pid}")
|
|
350
|
+
else
|
|
351
|
+
logger.warn "Removing stale PID file at #{pid_path}"
|
|
352
|
+
File.delete(pid_path)
|
|
353
|
+
end
|
|
354
|
+
end
|
|
355
|
+
if Jettywrapper.is_port_in_use?(self.port)
|
|
356
|
+
raise("Port #{self.port} is already in use.")
|
|
357
|
+
end
|
|
358
|
+
benchmark "Started jetty" do
|
|
359
|
+
Dir.chdir(@jetty_home) do
|
|
360
|
+
process.start
|
|
361
|
+
end
|
|
362
|
+
FileUtils.makedirs(pid_dir) unless File.directory?(pid_dir)
|
|
363
|
+
begin
|
|
364
|
+
f = File.new(pid_path, "w")
|
|
365
|
+
rescue Errno::ENOENT, Errno::EACCES
|
|
366
|
+
f = File.new(File.join(base_path,'tmp',pid_file),"w")
|
|
367
|
+
end
|
|
368
|
+
f.puts "#{process.pid}"
|
|
369
|
+
f.close
|
|
370
|
+
logger.debug "Wrote pid file to #{pid_path} with value #{process.pid}"
|
|
371
|
+
startup_wait!
|
|
372
|
+
end
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
# Wait for the jetty server to start and begin listening for requests
|
|
376
|
+
def startup_wait!
|
|
377
|
+
begin
|
|
378
|
+
Timeout::timeout(startup_wait) do
|
|
379
|
+
sleep 1 until (Jettywrapper.is_port_in_use? self.port)
|
|
380
|
+
end
|
|
381
|
+
rescue Timeout::Error
|
|
382
|
+
logger.warn "Waited #{startup_wait} seconds for jetty to start, but it is not yet listening on port #{self.port}. Continuing anyway."
|
|
383
|
+
end
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
def process
|
|
387
|
+
@process ||= begin
|
|
388
|
+
process = ChildProcess.build(*jetty_command)
|
|
389
|
+
if self.quiet
|
|
390
|
+
process.io.stderr = File.open(File.expand_path("jettywrapper.log"), "w+")
|
|
391
|
+
process.io.stdout = process.io.stderr
|
|
392
|
+
logger.warn "Logging jettywrapper stdout to #{File.expand_path(process.io.stderr.path)}"
|
|
249
393
|
else
|
|
250
|
-
|
|
251
|
-
File.delete(pid_path)
|
|
252
|
-
end
|
|
253
|
-
if Jettywrapper.is_port_in_use?(@jetty_port)
|
|
254
|
-
raise("Port #{self.jetty_port} is already in use.")
|
|
255
|
-
end
|
|
256
|
-
end
|
|
257
|
-
Dir.chdir(@jetty_home) do
|
|
258
|
-
process = build_process
|
|
259
|
-
@pid = process.pid
|
|
260
|
-
end
|
|
261
|
-
File.makedirs(pid_dir) unless File.directory?(pid_dir)
|
|
262
|
-
begin
|
|
263
|
-
f = File.new(pid_path, "w")
|
|
264
|
-
rescue Errno::ENOENT, Errno::EACCES
|
|
265
|
-
f = File.new(File.join(@base_path,'tmp',pid_file),"w")
|
|
266
|
-
end
|
|
267
|
-
f.puts "#{@pid}"
|
|
268
|
-
f.close
|
|
269
|
-
logger.debug "Wrote pid file to #{pid_path} with value #{@pid}"
|
|
270
|
-
end
|
|
271
|
-
|
|
272
|
-
def build_process
|
|
273
|
-
process = ChildProcess.build(jetty_command)
|
|
274
|
-
process.io.inherit!
|
|
275
|
-
process.detach = true
|
|
276
|
-
process.start
|
|
277
|
-
end
|
|
278
|
-
# Instance stop method. Must be called on Jettywrapper.instance
|
|
279
|
-
# You're probably better off using Jettywrapper.stop(:jetty_home => "/path/to/jetty")
|
|
280
|
-
# @example
|
|
281
|
-
# Jettywrapper.configure(params)
|
|
282
|
-
# Jettywrapper.instance.stop
|
|
283
|
-
# return Jettywrapper.instance
|
|
284
|
-
def stop
|
|
285
|
-
logger.debug "Instance stop method called for pid #{pid}"
|
|
286
|
-
if pid
|
|
287
|
-
process = ChildProcess.new
|
|
288
|
-
process.instance_variable_set(:@pid, pid)
|
|
289
|
-
process.instance_variable_set(:@started, true)
|
|
290
|
-
process.stop
|
|
291
|
-
begin
|
|
292
|
-
File.delete(pid_path)
|
|
293
|
-
rescue
|
|
394
|
+
process.io.inherit!
|
|
294
395
|
end
|
|
396
|
+
process.detach = true
|
|
397
|
+
|
|
398
|
+
process
|
|
295
399
|
end
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
def reset_process!
|
|
403
|
+
@process = nil
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
# Instance stop method. Must be called on Jettywrapper.instance
|
|
407
|
+
# You're probably better off using Jettywrapper.stop(:jetty_home => "/path/to/jetty")
|
|
408
|
+
# @example
|
|
409
|
+
# Jettywrapper.configure(params)
|
|
410
|
+
# Jettywrapper.instance.stop
|
|
411
|
+
# return Jettywrapper.instance
|
|
412
|
+
def stop
|
|
413
|
+
logger.debug "Instance stop method called for pid '#{pid}'"
|
|
414
|
+
if pid
|
|
415
|
+
if @process
|
|
416
|
+
@process.stop
|
|
417
|
+
else
|
|
418
|
+
Process.kill("KILL", pid) rescue nil
|
|
419
|
+
end
|
|
420
|
+
|
|
314
421
|
begin
|
|
315
|
-
|
|
422
|
+
File.delete(pid_path)
|
|
316
423
|
rescue
|
|
317
|
-
raise "Couldn't make a pid file for jetty_home value #{jetty_home}"
|
|
318
|
-
raise $!
|
|
319
424
|
end
|
|
320
425
|
end
|
|
426
|
+
end
|
|
427
|
+
|
|
428
|
+
# The fully qualified path to the pid_file
|
|
429
|
+
def pid_path
|
|
430
|
+
#need to memoize this, becasuse the base path could be relative and the cwd can change in the yield block of wrap
|
|
431
|
+
@path ||= File.join(pid_dir, pid_file)
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
# The file where the process ID will be written
|
|
435
|
+
def pid_file
|
|
436
|
+
jetty_home_to_pid_file(@jetty_home)
|
|
437
|
+
end
|
|
321
438
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
439
|
+
# Take the @jetty_home value and transform it into a legal filename
|
|
440
|
+
# @return [String] the name of the pid_file
|
|
441
|
+
# @example
|
|
442
|
+
# /usr/local/jetty1 => _usr_local_jetty1.pid
|
|
443
|
+
def jetty_home_to_pid_file(jetty_home)
|
|
444
|
+
begin
|
|
445
|
+
jetty_home.gsub(/\//,'_') << "_#{self.class.env}" << ".pid"
|
|
446
|
+
rescue Exception => e
|
|
447
|
+
raise "Couldn't make a pid file for jetty_home value #{jetty_home}\n Caused by: #{e}"
|
|
448
|
+
end
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
# The directory where the pid_file will be written
|
|
452
|
+
def pid_dir
|
|
453
|
+
File.expand_path(File.join(base_path,'tmp','pids'))
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
# Check to see if there is a pid file already
|
|
457
|
+
# @return true if the file exists, otherwise false
|
|
458
|
+
def pid_file?
|
|
459
|
+
File.exist?(pid_path)
|
|
460
|
+
end
|
|
461
|
+
|
|
462
|
+
# the process id of the currently running jetty instance
|
|
463
|
+
def pid
|
|
464
|
+
File.open( pid_path ) { |f| return f.gets.to_i } if File.exist?(pid_path)
|
|
465
|
+
end
|
|
339
466
|
end
|