Class: WebsolrController
- Inherits:
-
Object
show all
- Defined in:
- lib/websolr_controller.rb
Constant Summary
collapse
- COMMANDS =
%w[add list delete configure local:start local:stop]
- SOLR_PORT =
8983
Instance Method Summary
collapse
Constructor Details
Returns a new instance of WebsolrController.
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/websolr_controller.rb', line 24
def initialize(parser)
@options = parser.options
@command = @options.delete(:command)
@parser = parser
@user = @options[:user] ||= ENV["WEBSOLR_USER"]
@pass = @options[:pass] ||= ENV["WEBSOLR_PWD"]
if @user && @pass
@base = "http://#{URI::escape @user}:#{URI::escape @pass}@websolr.com"
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *a, &b) ⇒ Object
229
230
231
232
|
# File 'lib/websolr_controller.rb', line 229
def method_missing(method, *a, &b)
return @options[method] if @options[method]
super(method, *a, &b)
end
|
Instance Method Details
#check_local_solr_conditions ⇒ Object
63
64
65
66
67
68
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
95
96
97
98
99
|
# File 'lib/websolr_controller.rb', line 63
def check_local_solr_conditions
ENV["RAILS_ENV"] = @options[:rails_env] || ENV["RAILS_ENV"] || "development"
begin
require "config/environment"
rescue LoadError
die("I can't find config/environment.rb. Are we in a rails app?")
end
unless ENV["WEBSOLR_URL"]
ENV["WEBSOLR_URL"] = "http://localhost:8983/solr"
puts <<-STR
You haven't configured your app. You might want to do that. I
assume you just want a quick development server, so I'll start
one up for you at http://localhost:8983/solr.
You should let Rails know about it by setting the WEBSOLR_URL, i.e:
> ./script/server WEBSOLR_URL=http://localhost:8983/solr
If you want to set up a full environment, run websolr configure.
STR
puts "Is this what you want? [yes]"
if STDIN.gets.strip =~/^(yes)?$/i
puts "Continuing...."
else
die "Aborted."
end
end
uri = URI.parse(ENV["WEBSOLR_URL"])
@port = uri.port
rescue URI::InvalidURIError => e
die(e.message)
end
|
#cmd_add ⇒ Object
140
141
142
143
144
|
# File 'lib/websolr_controller.rb', line 140
def cmd_add
required_options :name => "-n", :auth => true
doc = post "/slices.xml", {:slice => {:name => name}}
puts "#{x doc, '//name'}\t#{x doc, '//base-url'}"
end
|
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
# File 'lib/websolr_controller.rb', line 183
def cmd_configure
required_options :name => "-n", :auth => true
doc = get "/slices.xml"
found = false
REXML::XPath.each(doc, "//slice") do |node|
if x(node, 'name') == self.name
found = true
FileUtils.mkdir_p "config/initializers"
path = "config/initializers/websolr.rb"
puts "Writing #{path}"
File.open(path, "w") do |f|
str = <<-STR
require 'websolr'
case RAILS_ENV
when 'production'
ENV['WEBSOLR_URL'] ||= '#{x node, 'base-url'}'
else
ENV['WEBSOLR_URL'] ||= 'http://localhost:8983/solr'
end
STR
f.puts str
end
FileUtils.mkdir_p "lib/tasks"
path = "lib/tasks/websolr.rake"
puts "Writing #{path}"
File.open(path, "w") do |f|
f.puts "require 'rubygems'\nrequire 'websolr_rails/tasks'"
end
end
end
unless found
STDERR.puts "Error: Index not found"
exit 1
end
end
|
#cmd_delete ⇒ Object
146
147
148
149
150
|
# File 'lib/websolr_controller.rb', line 146
def cmd_delete
required_options :name => "-n", :auth => true
delete "/slices/#{name}/destroy"
puts "done"
end
|
#cmd_list ⇒ Object
156
157
158
159
160
161
162
|
# File 'lib/websolr_controller.rb', line 156
def cmd_list
required_options :auth => true
doc = get "/slices.xml"
REXML::XPath.each(doc, "//slice") do |node|
puts "#{x node, 'name'}\t#{x node, 'base-url'}"
end
end
|
#cmd_local_start ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/websolr_controller.rb', line 101
def cmd_local_start
check_local_solr_conditions
begin
n = Net::HTTP.new('127.0.0.1', @port)
n.request_head('/').value
rescue Net::HTTPServerException puts "Port #{@port} in use" and return
rescue Errno::ECONNREFUSED Dir.chdir(SOLR_PATH) do
pid = fork do
exec "java #{SOLR_JVM_OPTIONS} -Dsolr.data.dir=#{SOLR_DATA_PATH} -Djetty.logs=#{SOLR_LOGS_PATH} -Djetty.port=#{@port} -jar start.jar"
end
sleep(5)
File.open("#{SOLR_PIDS_PATH}/#{ENV['RAILS_ENV']}_pid", "w"){ |f| f << pid}
puts "#{ENV['RAILS_ENV']} Solr started successfully on #{SOLR_PORT}, pid: #{pid}."
end
end
end
|
#cmd_local_stop ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/websolr_controller.rb', line 122
def cmd_local_stop
ENV["RAILS_ENV"] = @options[:rails_env] || ENV["RAILS_ENV"] || "development"
fork do
file_path = "#{SOLR_PIDS_PATH}/#{ENV['RAILS_ENV']}_pid"
if File.exists?(file_path)
File.open(file_path, "r") do |f|
pid = f.readline
Process.kill('TERM', pid.to_i)
end
File.unlink(file_path)
Rake::Task["solr:destroy_index"].invoke if ENV['RAILS_ENV'] == 'test'
puts "Solr shutdown successfully."
else
puts "PID file not found at #{file_path}. Either Solr is not running or no PID file was written."
end
end
end
|
#die(s) ⇒ Object
35
36
37
38
|
# File 'lib/websolr_controller.rb', line 35
def die(s)
STDERR.puts s
exit(1)
end
|
#print_errors(doc) ⇒ Object
176
177
178
179
180
181
|
# File 'lib/websolr_controller.rb', line 176
def print_errors(doc)
REXML::XPath.each(doc, "//error") do |node|
STDERR.puts "Error: #{node.text}"
end
exit 1
end
|
#required_options(hash) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/websolr_controller.rb', line 40
def required_options(hash)
hash = hash.dup
if hash.delete(:auth) && (!@user || !@pass)
die <<-STR
You need to specify your username and password, either on the command
line with the -u and -p flags, or in the WEBSOLR_USER and WEBSOLR_PWD
environment variables.
STR
end
hash.inject(true) do |memo, (key, flag)|
unless @options[key]
STDERR.puts "Please use the #{flag} flag to specify the #{key}."
end
memo && @options[key]
end || exit(1)
end
|
#start ⇒ Object
220
221
222
223
224
225
226
227
|
# File 'lib/websolr_controller.rb', line 220
def start
if(COMMANDS.include?(@command))
send("cmd_#{@command.gsub(/\W+/, '_')}")
else
puts @parser
exit(1)
end
end
|
#url(url) ⇒ Object
59
60
61
|
# File 'lib/websolr_controller.rb', line 59
def url(url)
URI.join(@base, url).to_s
end
|
#x(doc, path) ⇒ Object
152
153
154
|
# File 'lib/websolr_controller.rb', line 152
def x(doc, path)
REXML::XPath.first(doc, path).text
end
|