Class: Jettywrapper

Inherits:
Object
  • Object
show all
Includes:
Loggable, Singleton
Defined in:
lib/jettywrapper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Jettywrapper

configure the singleton with some defaults



29
30
31
32
33
34
35
36
37
# File 'lib/jettywrapper.rb', line 29

def initialize(params = {})
  if defined?(Rails.root)
    @base_path = Rails.root
  else
    @base_path = "."
  end

  logger.debug 'Initializing jettywrapper'
end

Instance Attribute Details

#base_pathObject

The root of the application. Used for determining where log files and PID files should go.



25
26
27
# File 'lib/jettywrapper.rb', line 25

def base_path
  @base_path
end

#java_optsObject

Options to pass to java (ex. [“-Xmx512mb”, “-Xms128mb”])



26
27
28
# File 'lib/jettywrapper.rb', line 26

def java_opts
  @java_opts
end

#jetty_homeObject

Where is jetty located?



21
22
23
# File 'lib/jettywrapper.rb', line 21

def jetty_home
  @jetty_home
end

#pidObject

the process id of the currently running jetty instance



19
20
21
# File 'lib/jettywrapper.rb', line 19

def pid
  @pid
end

#portObject

What port should jetty start on? Default is 8888



20
21
22
# File 'lib/jettywrapper.rb', line 20

def port
  @port
end

#quietObject

Keep quiet about jetty output?



23
24
25
# File 'lib/jettywrapper.rb', line 23

def quiet
  @quiet
end

#solr_homeObject

Where is solr located? Default is jetty_home/solr



24
25
26
# File 'lib/jettywrapper.rb', line 24

def solr_home
  @solr_home
end

#startup_waitObject

After jetty starts, how long to wait until starting the tests?



22
23
24
# File 'lib/jettywrapper.rb', line 22

def startup_wait
  @startup_wait
end

Class Method Details

.configure(params = {}) ⇒ Object

Set the jetty parameters. It accepts a Hash of symbols.

Parameters:

  • params (Hash<Symbol>) (defaults to: {})
  • :jetty_home (Symbol)

    Required. Where is jetty located?

  • :jetty_port (Symbol)

    What port should jetty start on? Default is 8888

  • :startup_wait (Symbol)

    After jetty starts, how long to wait before running tests? If you don’t let jetty start all the way before running the tests, they’ll fail because they can’t reach jetty.

  • :solr_home (Symbol)

    Where is solr? Default is jetty_home/solr

  • :quiet (Symbol)

    Keep quiet about jetty output? Default is true.

  • :java_opts (Symbol)

    A list of options to pass to the jvm



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/jettywrapper.rb', line 72

def configure(params = {})
  hydra_server = self.instance
  hydra_server.quiet = params[:quiet].nil? ? true : params[:quiet]
  if defined?(Rails.root)
   base_path = Rails.root
  elsif defined?(APP_ROOT)
   base_path = APP_ROOT
  else
   raise "You must set either Rails.root, APP_ROOT or pass :jetty_home as a parameter so I know where jetty is" unless params[:jetty_home]
  end
  hydra_server.jetty_home = params[:jetty_home] || File.expand_path(File.join(base_path, 'jetty'))
  hydra_server.solr_home = params[:solr_home]  || File.join( hydra_server.jetty_home, "solr")
  hydra_server.port = params[:jetty_port] || 8888
  hydra_server.startup_wait = params[:startup_wait] || 5
  hydra_server.java_opts = params[:java_opts] || []
  return hydra_server
end

.is_jetty_running?(params) ⇒ Boolean

Determine whether the jetty at the given jetty_home is running

Examples:

Jettywrapper.is_jetty_running?(:jetty_home => '/path/to/jetty')

Parameters:

  • params: (Hash)

    :jetty_home is required. Which jetty do you want to check the status of?

Returns:

  • (Boolean)


163
164
165
166
167
168
# File 'lib/jettywrapper.rb', line 163

def is_jetty_running?(params)      
  Jettywrapper.configure(params)
  pid = Jettywrapper.instance.pid
  return false unless pid
  true
end

.is_pid_running?(pid) ⇒ Boolean

Check to see if the pid is actually running. This only works on unix.

Returns:

  • (Boolean)


207
208
209
210
211
212
213
# File 'lib/jettywrapper.rb', line 207

def is_pid_running?(pid)
  begin
    return Process.getpgid(pid) != -1
  rescue Errno::ESRCH
    return false
  end
end

.is_port_in_use?(port) ⇒ Boolean

Check to see if the port is open so we can raise an error if we have a conflict

Examples:

Jettywrapper.is_port_open?(8983)

Parameters:

  • port (Fixnum)

    the port to check

Returns:

  • (Boolean)


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/jettywrapper.rb', line 187

def is_port_in_use?(port)
  begin
    Timeout::timeout(1) do
      begin
        s = TCPSocket.new('127.0.0.1', port)
        s.close
        return true
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
        return false
      rescue
        return false
      end
    end
  rescue Timeout::Error
  end

  return false
end

.load_configObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jettywrapper.rb', line 43

def load_config
  if defined? Rails 
    config_name =  Rails.env 
    app_root = Rails.root
  else 
    config_name =  ENV['environment']
    app_root = ENV['APP_ROOT']
    app_root ||= '.'
  end
  filename = "#{app_root}/config/jetty.yml"
  begin
    file = YAML.load_file(filename)
  rescue Exception => e
    logger.warn "Didn't find expected jettywrapper config file at #{filename}, using default file instead."
    file ||= YAML.load_file(File.join(File.dirname(__FILE__),"../config/jetty.yml"))
    #raise "Unable to load: #{file}" unless file
  end
  file.with_indifferent_access
end

.pid(params) ⇒ Fixnum

Return the pid of the specified jetty, or return nil if it isn’t running

Examples:

Jettywrapper.pid(:jetty_home => '/path/to/jetty')

Parameters:

  • params: (Hash)

    :jetty_home is required.

Returns:

  • (Fixnum)

    or [nil]



175
176
177
178
179
180
# File 'lib/jettywrapper.rb', line 175

def pid(params)
  Jettywrapper.configure(params)
  pid = Jettywrapper.instance.pid
  return nil unless pid
  pid
end

.start(params) ⇒ Object

Convenience method for configuring and starting jetty with one command

Examples:

Jettywrapper.start_with_params(:jetty_home => '/path/to/jetty', :jetty_port => '8983')

Parameters:

  • params: (Hash)

    The configuration to use for starting jetty



139
140
141
142
143
# File 'lib/jettywrapper.rb', line 139

def start(params)
   Jettywrapper.configure(params)
   Jettywrapper.instance.start
   return Jettywrapper.instance
end

.stop(params) ⇒ Jettywrapper.instance

Convenience method for configuring and starting jetty with one command. Note that for stopping, only the :jetty_home value is required (including other values won’t hurt anything, though).

Examples:

Jettywrapper.stop_with_params(:jetty_home => '/path/to/jetty')

Parameters:

  • params: (Hash)

    The jetty_home to use for stopping jetty

Returns:



152
153
154
155
156
# File 'lib/jettywrapper.rb', line 152

def stop(params)
   Jettywrapper.configure(params)
   Jettywrapper.instance.stop
   return Jettywrapper.instance
end

.wrap(params) ⇒ Object

Wrap the tests. Startup jetty, yield to the test task, capture any errors, shutdown jetty, and return the error.

Examples:

Using this method in a rake task

require 'jettywrapper'
desc "Spin up jetty and run tests against it"
task :newtest do
  jetty_params = { 
    :jetty_home => "/path/to/jetty", 
    :quiet => false, 
    :jetty_port => 8983, 
    :startup_wait => 30
  }
  error = Jettywrapper.wrap(jetty_params) do   
    Rake::Task["rake:spec"].invoke 
    Rake::Task["rake:cucumber"].invoke 
  end 
  raise "test failures: #{error}" if error
end


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/jettywrapper.rb', line 109

def wrap(params)
  error = false
  jetty_server = self.configure(params)
  # jetty_server = self.instance
  # jetty_server.quiet = params[:quiet] || true
  # jetty_server.jetty_home = params[:jetty_home]
  # jetty_server.solr_home = params[:solr_home]
  # jetty_server.port = params[:jetty_port] || 8888
  # jetty_server.startup_wait = params[:startup_wait] || 5

  begin
    # puts "starting jetty on #{RUBY_PLATFORM}"
    jetty_server.start
    sleep jetty_server.startup_wait
    yield
  rescue
    error = $!
    puts "*** Error starting hydra-jetty: #{error}"
  ensure
    # puts "stopping jetty server"
    jetty_server.stop
  end

  return error
end

Instance Method Details

#build_processObject



272
273
274
275
276
277
# File 'lib/jettywrapper.rb', line 272

def build_process
  process = ChildProcess.build(jetty_command)
  process.io.inherit!
  process.detach = true
  process.start
end

#java_variablesObject



224
225
226
227
# File 'lib/jettywrapper.rb', line 224

def java_variables
  ["-Djetty.port=#{@port}",
   "-Dsolr.solr.home=#{@solr_home}"]
end

#jetty_commandObject

What command is being run to invoke jetty?



219
220
221
222
# File 'lib/jettywrapper.rb', line 219

def jetty_command
  opts = (java_variables + java_opts).join(' ')
  "java #{opts} -jar start.jar"
end

#jetty_home_to_pid_file(jetty_home) ⇒ String

Take the @jetty_home value and transform it into a legal filename

Examples:

/usr/local/jetty1 => _usr_local_jetty1.pid

Returns:

  • (String)

    the name of the pid_file



313
314
315
316
317
318
319
320
# File 'lib/jettywrapper.rb', line 313

def jetty_home_to_pid_file(jetty_home)
  begin
    jetty_home.gsub(/\//,'_') << ".pid"
  rescue
    raise "Couldn't make a pid file for jetty_home value #{jetty_home}"
    raise $!
  end
end

#pid_dirObject

The directory where the pid_file will be written



323
324
325
# File 'lib/jettywrapper.rb', line 323

def pid_dir
  File.expand_path(File.join(@base_path,'tmp','pids'))
end

#pid_fileObject

The file where the process ID will be written



305
306
307
# File 'lib/jettywrapper.rb', line 305

def pid_file
  jetty_home_to_pid_file(@jetty_home)
end

#pid_file?Boolean

Check to see if there is a pid file already

Returns:

  • (Boolean)

    true if the file exists, otherwise false



329
330
331
332
# File 'lib/jettywrapper.rb', line 329

def pid_file?
   return true if File.exist?(pid_path)
   false
end

#pid_pathObject

The fully qualified path to the pid_file



300
301
302
# File 'lib/jettywrapper.rb', line 300

def pid_path
  File.join(pid_dir, pid_file)
end

#startObject

Start the jetty server. Check the pid file to see if it is running already, and stop it if so. After you start jetty, write the PID to a file. This is the instance start method. It must be called on Jettywrapper.instance You’re probably better off using Jettywrapper.start(:jetty_home => “/path/to/jetty”)

Examples:

Jettywrapper.configure(params)
Jettywrapper.instance.start
return Jettywrapper.instance


237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/jettywrapper.rb', line 237

def start
  logger.debug "Starting jetty with these values: "
  logger.debug "jetty_home: #{@jetty_home}"
  logger.debug "solr_home: #{@solr_home}"
  logger.debug "jetty_command: #{jetty_command}"
  
  # Check to see if we can start.
  # 1. If there is a pid, check to see if it is really running
  # 2. Check to see if anything is blocking the port we want to use     
  if pid
    if Jettywrapper.is_pid_running?(pid)
      raise("Server is already running with PID #{pid}")
    else
      logger.warn "Removing stale PID file at #{pid_path}"
      File.delete(pid_path)
    end
    if Jettywrapper.is_port_in_use?(@jetty_port)
      raise("Port #{self.jetty_port} is already in use.")
    end
  end
  Dir.chdir(@jetty_home) do
    process = build_process
    @pid = process.pid
  end
  File.makedirs(pid_dir) unless File.directory?(pid_dir)
  begin
    f = File.new(pid_path,  "w")
  rescue Errno::ENOENT, Errno::EACCES
    f = File.new(File.join(@base_path,'tmp',pid_file),"w")
  end
  f.puts "#{@pid}"
  f.close
  logger.debug "Wrote pid file to #{pid_path} with value #{@pid}"
end

#stopObject

Instance stop method. Must be called on Jettywrapper.instance You’re probably better off using Jettywrapper.stop(:jetty_home => “/path/to/jetty”)

Examples:

Jettywrapper.configure(params)
Jettywrapper.instance.stop
return Jettywrapper.instance


284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/jettywrapper.rb', line 284

def stop    
  logger.debug "Instance stop method called for pid #{pid}"
  if pid
    process = ChildProcess.new
    process.instance_variable_set(:@pid, pid)
    process.instance_variable_set(:@started, true)
    process.stop 
    begin
      File.delete(pid_path)
    rescue
    end
  end
end