onemorecloud-websolr-rails 1.3.3 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ 1.4.0
2
+ - Added CHANGELOG
3
+ - Added local development server
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.3
1
+ 1.4.0
data/bin/websolr CHANGED
@@ -2,6 +2,8 @@
2
2
  require File.dirname(__FILE__) + "/../lib/websolr_option_parser"
3
3
  require File.dirname(__FILE__) + "/../lib/websolr_controller"
4
4
 
5
+ PWD = File.expand_path(".")
6
+
5
7
  parser = WebsolrOptionParser.new
6
8
  parser.parse!
7
9
  controller = WebsolrController.new(parser)
@@ -15,6 +15,7 @@ module ActsAsSolr #:nodoc:
15
15
  if method.to_s =~ /^highlighted_(.*)$/ && a.length == 0
16
16
  original_field = $1
17
17
  @solr_data && @solr_data[:highlights] && @solr_data[:highlights][id] &&
18
+ @solr_data[:highlights][id][original_field] &&
18
19
  @solr_data[:highlights][id][original_field].join(" ") || send(original_field)
19
20
  else
20
21
  method_missing_without_solr_magic(method, *a, &b)
data/lib/tasks/solr.rake CHANGED
@@ -5,59 +5,16 @@ require 'active_record'
5
5
 
6
6
  namespace :solr do
7
7
 
8
- # desc 'Starts Solr. Options accepted: RAILS_ENV=your_env, PORT=XX. Defaults to development if none.'
9
- # task :start do
10
- # require "#{File.dirname(__FILE__)}/../../config/solr_environment.rb"
11
- # begin
12
- # n = Net::HTTP.new('127.0.0.1', SOLR_PORT)
13
- # n.request_head('/').value
14
- #
15
- # rescue Net::HTTPServerException #responding
16
- # puts "Port #{SOLR_PORT} in use" and return
17
- #
18
- # rescue Errno::ECONNREFUSED #not responding
19
- # Dir.chdir(SOLR_PATH) do
20
- # pid = fork do
21
- # #STDERR.close
22
- # exec "java #{SOLR_JVM_OPTIONS} -Dsolr.data.dir=#{SOLR_DATA_PATH} -Djetty.logs=#{SOLR_LOGS_PATH} -Djetty.port=#{SOLR_PORT} -jar start.jar"
23
- # end
24
- # sleep(5)
25
- # File.open("#{SOLR_PIDS_PATH}/#{ENV['RAILS_ENV']}_pid", "w"){ |f| f << pid}
26
- # puts "#{ENV['RAILS_ENV']} Solr started successfully on #{SOLR_PORT}, pid: #{pid}."
27
- # end
28
- # end
29
- # end
30
-
31
- # desc 'Stops Solr. Specify the environment by using: RAILS_ENV=your_env. Defaults to development if none.'
32
- # task :stop do
33
- # require "#{File.dirname(__FILE__)}/../../config/solr_environment.rb"
34
- # fork do
35
- # file_path = "#{SOLR_PIDS_PATH}/#{ENV['RAILS_ENV']}_pid"
36
- # if File.exists?(file_path)
37
- # File.open(file_path, "r") do |f|
38
- # pid = f.readline
39
- # Process.kill('TERM', pid.to_i)
40
- # end
41
- # File.unlink(file_path)
42
- # Rake::Task["solr:destroy_index"].invoke if ENV['RAILS_ENV'] == 'test'
43
- # puts "Solr shutdown successfully."
44
- # else
45
- # puts "PID file not found at #{file_path}. Either Solr is not running or no PID file was written."
46
- # end
47
- # end
48
- # end
49
-
50
- # desc 'Remove Solr index'
51
- # task :destroy_index do
52
- # require "#{File.dirname(__FILE__)}/../../config/solr_environment.rb"
53
- # raise "In production mode. I'm not going to delete the index, sorry." if ENV['RAILS_ENV'] == "production"
54
- # if File.exists?("#{SOLR_DATA_PATH}")
55
- # Dir["#{SOLR_DATA_PATH}/index/*"].each{|f| File.unlink(f)}
56
- # Dir.rmdir("#{SOLR_DATA_PATH}/index")
57
- # puts "Index files removed under " + ENV['RAILS_ENV'] + " environment"
58
- # end
59
- # end
8
+ desc 'Starts Solr. Options accepted: RAILS_ENV=your_env, PORT=XX. Defaults to development if none.'
9
+ task :start do
10
+ puts "Use: websolr start"
11
+ end
60
12
 
13
+ desc 'Stops Solr. Specify the environment by using: RAILS_ENV=your_env. Defaults to development if none.'
14
+ task :stop do
15
+ puts "Use: websolr stop"
16
+ end
17
+
61
18
  # this task is by Henrik Nyh
62
19
  # http://henrik.nyh.se/2007/06/rake-task-to-reindex-models-for-acts_as_solr
63
20
  desc %{Reindexes data for all acts_as_solr models. Clears index first to get rid of orphaned records and optimizes index afterwards. RAILS_ENV=your_env to set environment. ONLY=book,person,magazine to only reindex those models; EXCEPT=book,magazine to exclude those models. START_SERVER=true to solr:start before and solr:stop after. BATCH=123 to post/commit in batches of that size: default is 300. CLEAR=false to not clear the index first; OPTIMIZE=false to not optimize the index afterwards.}
@@ -3,9 +3,23 @@ require "rubygems"
3
3
  require "restclient"
4
4
  require 'rexml/document'
5
5
  require "fileutils"
6
+ require "net/http"
7
+ include FileUtils
8
+
9
+ SOLR_PATH = "#{File.dirname(File.expand_path(__FILE__))}/../solr" unless defined? SOLR_PATH
10
+ SOLR_LOGS_PATH = "#{ENV["PWD"]}/log" unless defined? SOLR_LOGS_PATH
11
+ SOLR_PIDS_PATH = "#{ENV["PWD"]}/tmp/pids" unless defined? SOLR_PIDS_PATH
12
+ SOLR_DATA_PATH = "#{ENV["PWD"]}/solr/#{ENV['RAILS_ENV']}" unless defined? SOLR_DATA_PATH
13
+ SOLR_JVM_OPTIONS = ENV["JAVA_OPTIONS"] || "-Xmx256M"
14
+
15
+ mkdir_p SOLR_PATH
16
+ mkdir_p SOLR_LOGS_PATH
17
+ mkdir_p SOLR_PIDS_PATH
18
+ mkdir_p SOLR_DATA_PATH
6
19
 
7
20
  class WebsolrController
8
- COMMANDS = %w[add list delete configure]
21
+ COMMANDS = %w[add list delete configure start stop]
22
+ SOLR_PORT = 8983
9
23
 
10
24
  def initialize(parser)
11
25
  @options = parser.options
@@ -16,6 +30,11 @@ class WebsolrController
16
30
  @base = "http://#{URI::escape @user}:#{URI::escape @pass}@websolr.com"
17
31
  end
18
32
 
33
+ def die(s)
34
+ STDERR.puts s
35
+ exit(1)
36
+ end
37
+
19
38
  def required_options(hash)
20
39
  hash.inject(true) do |memo, (key, flag)|
21
40
  unless @options[key]
@@ -29,6 +48,62 @@ class WebsolrController
29
48
  URI.join(@base, url).to_s
30
49
  end
31
50
 
51
+ def check_local_solr_conditions
52
+ ENV["RAILS_ENV"] = @options[:rails_env] || ENV["RAILS_ENV"] || "development"
53
+ begin
54
+ require "config/environment"
55
+ rescue LoadError
56
+ die("I can't find config/environment.rb. Are we in a rails app?")
57
+ end
58
+
59
+ unless url = ENV["WEBSOLR_URL"]
60
+ die("The WEBSOLR_URL environment variable is not set.\nHave you run websolr configure?")
61
+ end
62
+ uri = URI.parse(url)
63
+ @port = uri.port
64
+ rescue URI::InvalidURIError => e
65
+ die(e.message)
66
+ end
67
+
68
+ def cmd_start
69
+ check_local_solr_conditions
70
+ begin
71
+ n = Net::HTTP.new('127.0.0.1', @port)
72
+ n.request_head('/').value
73
+
74
+ rescue Net::HTTPServerException #responding
75
+ puts "Port #{@port} in use" and return
76
+
77
+ rescue Errno::ECONNREFUSED #not responding
78
+ Dir.chdir(SOLR_PATH) do
79
+ pid = fork do
80
+ exec "java #{SOLR_JVM_OPTIONS} -Dsolr.data.dir=#{SOLR_DATA_PATH} -Djetty.logs=#{SOLR_LOGS_PATH} -Djetty.port=#{@port} -jar start.jar"
81
+ end
82
+ sleep(5)
83
+ File.open("#{SOLR_PIDS_PATH}/#{ENV['RAILS_ENV']}_pid", "w"){ |f| f << pid}
84
+ puts "#{ENV['RAILS_ENV']} Solr started successfully on #{SOLR_PORT}, pid: #{pid}."
85
+ end
86
+ end
87
+ end
88
+
89
+ def cmd_stop
90
+ ENV["RAILS_ENV"] = @options[:rails_env] || ENV["RAILS_ENV"] || "development"
91
+ fork do
92
+ file_path = "#{SOLR_PIDS_PATH}/#{ENV['RAILS_ENV']}_pid"
93
+ if File.exists?(file_path)
94
+ File.open(file_path, "r") do |f|
95
+ pid = f.readline
96
+ Process.kill('TERM', pid.to_i)
97
+ end
98
+ File.unlink(file_path)
99
+ Rake::Task["solr:destroy_index"].invoke if ENV['RAILS_ENV'] == 'test'
100
+ puts "Solr shutdown successfully."
101
+ else
102
+ puts "PID file not found at #{file_path}. Either Solr is not running or no PID file was written."
103
+ end
104
+ end
105
+ end
106
+
32
107
  def cmd_add
33
108
  required_options :name => "-n"
34
109
  doc = post "/slices.xml", {:slice => {:name => name}}
@@ -72,20 +147,23 @@ class WebsolrController
72
147
  end
73
148
 
74
149
  def cmd_configure
75
- required_options :name => "-n", :rails_env => "-e"
150
+ required_options :name => "-n"
76
151
  doc = get "/slices.xml"
77
152
  found = false
78
153
  REXML::XPath.each(doc, "//slice") do |node|
79
154
  if x(node, 'name') == self.name
80
155
  found = true
81
156
  FileUtils.mkdir_p "config/initializers"
82
- path = "config/initializers/websolr_#{rails_env}.rb"
157
+ path = "config/initializers/websolr.rb"
83
158
  puts "Writing #{path}"
84
159
  File.open(path, "w") do |f|
85
160
  str = <<-STR
86
- if RAILS_ENV == '#{rails_env}'
87
- require 'websolr'
161
+ require 'websolr'
162
+ case RAILS_ENV
163
+ when 'production'
88
164
  ENV['WEBSOLR_URL'] ||= '#{x node, 'base-url'}'
165
+ else
166
+ ENV['WEBSOLR_URL'] ||= 'http://localhost:8983'
89
167
  end
90
168
  STR
91
169
  f.puts str
@@ -7,6 +7,9 @@ class WebsolrOptionParser < OptionParser
7
7
  "Usage: #{$0} COMMAND [INDEX_NAME] [options]
8
8
 
9
9
  COMMANDs:
10
+ start - starts the local development server
11
+ stop - stops the local development server
12
+
10
13
  add - creates a new index
11
14
  list - shows your indexes
12
15
  delete - deletes an index
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{websolr-rails}
8
- s.version = "1.3.3"
8
+ s.version = "1.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kyle Maxwell"]
12
- s.date = %q{2009-08-26}
12
+ s.date = %q{2009-09-15}
13
13
  s.default_executable = %q{websolr}
14
14
  s.description = %q{acts_as_solr compatible gem for websolr}
15
15
  s.email = %q{kyle@kylemaxwell.com}
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
21
21
  s.files = [
22
22
  ".document",
23
23
  ".gitignore",
24
+ "CHANGELOG",
24
25
  "LICENSE",
25
26
  "README.rdoc",
26
27
  "Rakefile",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: onemorecloud-websolr-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.3
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Maxwell
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-26 00:00:00 -07:00
12
+ date: 2009-09-15 00:00:00 -07:00
13
13
  default_executable: websolr
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -34,6 +34,7 @@ extra_rdoc_files:
34
34
  files:
35
35
  - .document
36
36
  - .gitignore
37
+ - CHANGELOG
37
38
  - LICENSE
38
39
  - README.rdoc
39
40
  - Rakefile