Class: Hydra::TestingServer

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
vendor/plugins/hydra_repository/lib/hydra/testing_server.rb

Overview

This class is based on Blacklight’s TestSolrServer

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (TestingServer) initialize(params = {})

configure the singleton with some defaults



26
27
28
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 26

def initialize(params = {})
  @pid = nil
end

Instance Attribute Details

- (Object) fedora_home

Returns the value of attribute fedora_home



23
24
25
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 23

def fedora_home
  @fedora_home
end

- (Object) jetty_home

Returns the value of attribute jetty_home



23
24
25
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 23

def jetty_home
  @jetty_home
end

- (Object) port

Returns the value of attribute port



23
24
25
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 23

def port
  @port
end

- (Object) quiet

Returns the value of attribute quiet



23
24
25
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 23

def quiet
  @quiet
end

- (Object) solr_home

Returns the value of attribute solr_home



23
24
25
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 23

def solr_home
  @solr_home
end

Class Method Details

+ (Object) configure(params = {})



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 31

def configure(params = {})
  hydra_server = self.instance
  hydra_server.quiet = params[:quiet].nil? ? true : params[:quiet]
  if defined?(Rails.root)
    base_path = Rails.root
  else
    base_path = "."
  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.fedora_home = params[:fedora_home] || File.join( hydra_server.jetty_home, "fedora","default")
  hydra_server.port = params[:jetty_port] || 8888
  return hydra_server
end

+ (Object) wrap(params = {})



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 46

def wrap(params = {})
  error = false
  hydra_server = self.configure(params)
  begin
    puts "starting Hydra jetty server on #{RUBY_PLATFORM}"
    hydra_server.start
    sleep params[:startup_wait] || 5
    yield
  rescue
    error = true
  ensure
    puts "stopping Hydra jetty server"
    hydra_server.stop
  end

  return error
end

Instance Method Details

- (Object) jetty_command



65
66
67
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 65

def jetty_command
  "java -Djetty.port=#{@port} -Dsolr.solr.home=#{@solr_home} -Dfedora.home=#{@fedora_home} -jar start.jar"
end

- (Object) nix_process



127
128
129
130
131
132
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 127

def nix_process
  @pid = fork do
    STDERR.close if @quiet
    exec jetty_command
  end
end

- (Object) nix_stop



139
140
141
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 139

def nix_stop
  Process.kill('TERM',pid)
end

- (Object) pid



155
156
157
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 155

def pid
  @pid || File.open( pid_path ) { |f| return f.gets.to_i } if File.exist?(pid_path)
end

- (Object) pid_dir



151
152
153
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 151

def pid_dir
  File.expand_path(@pid_dir || File.join(Rails.root,'tmp','pids'))
end

- (Object) pid_file



147
148
149
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 147

def pid_file
  @pid_file || 'hydra-jetty.pid'
end

- (Object) pid_path



143
144
145
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 143

def pid_path
  File.join(pid_dir, pid_file)
end

- (Object) platform



118
119
120
121
122
123
124
125
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 118

def platform
  case RUBY_PLATFORM
  when /mswin32/
    return 'win'
  else
    return 'nix'
  end
end

- (Object) start



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 69

def start
  puts "jetty_home: #{@jetty_home}"
  puts "solr_home: #{@solr_home}"
  puts "fedora_home: #{@fedora_home}"
  puts "jetty_command: #{jetty_command}"
  if pid
    begin
      Process.kill(0,pid)
      raise("Server is already running with PID #{pid}")
    rescue Errno::ESRCH
      STDERR.puts("Removing stale PID file at #{pid_path}")
      File.delete(pid_path)
    end
  end
  Dir.chdir(@jetty_home) do
    self.send "#{platform}_process".to_sym
  end
  mkpath(pid_dir) unless File.directory?(pid_dir)
  begin
    f = File.new(pid_path,  "w")
  rescue Errno::ENOENT, Errno::EACCES
    f = File.new(File.join(Rails.root,'tmp',pid_file),"w")
  end
  f.puts "#{@pid}"
  f.close
end

- (Object) stop



96
97
98
99
100
101
102
103
104
105
106
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 96

def stop
  puts "stopping"
  if pid
    begin
      self.send "#{platform}_stop".to_sym
    rescue Errno::ESRCH
      STDERR.puts("Removing stale PID file at #{pid_path}")
    end
    FileUtils.rm(pid_path)
  end
end

- (Object) win_process



108
109
110
111
112
113
114
115
116
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 108

def win_process
  @pid = Process.create(
        :app_name         => jetty_command,
        :creation_flags   => Process::DETACHED_PROCESS,
        :process_inherit  => false,
        :thread_inherit   => true,
        :cwd              => "#{@jetty_home}"
     ).process_id
end

- (Object) win_stop

stop a running solr server



135
136
137
# File 'vendor/plugins/hydra_repository/lib/hydra/testing_server.rb', line 135

def win_stop
  Process.kill(1, @pid)
end