Solr Sunspot Production environment setup issue - ruby-on-rails

I am trying to setup solr sunspot search in production mode.
I have followed the below tutorial and got success in setting up tomcat-solr server running.
http://www.arborisoft.com/how-to-install-apache-solr-4-4-on-ubuntu-12-04/
I ran
rails generate sunspot_rails:install
and configured the config/sunspot.yml file.
For development mode I used 'sunspot_solr' gem(which is not advisable for production mode) and got this command working.
bundle exec rake sunspot:solr:start
When I removed this gem
it's giving me following error
Note: This task has been moved to the sunspot_solr gem. To install, start and
stop a local Solr instance, please add sunspot_solr to your Gemfile:
group :development do
gem 'sunspot_solr'
end
I have tried adding this to Rakefile:
require 'sunspot/solr/tasks'
when I run the following command
bundle exec rake app:sunspot:solr:start
I am getting the following error
rake aborted!
cannot load such file -- sunspot/solr/tasks
I am using rails 4.
I don't know what I've missed. I have searched for the solution in and out of stackoverflow. But I couldn't get any reference. Even if I find any since I am a rails noob, I couldn't understand what they are talking about. any solution will be really helpful.

You can also change the path to solr/default in config/sunspot.yml file for production mode or any other defined modes.
These are some terminal commands which could follow to solve your problems:
ps aux | grep solr to get solr process ID
through this you can check which server id is running and which one is in your sunspot pid file.
sudo kill <ID>, <ID> is the ID you found from 1
rm -r <path/to/solr>, remove the solr directory inside your project to remove all of previous indexes
RAILS_ENV=production bundle exec rake sunspot:solr:start
Change the path to /solr/default inside config/sunspot.yml
RAILS_ENV=production bundle exec rake sunspot:solr:reindex
this link can also help in understanding problem and issues:https://github.com/sunspot/sunspot/issues/492

Related

Delayed Job in Rails 4 with Capistrano

I cant figure how to start Delayed Jobs on a dedicated Ubuntu server.
It works fine on my localhost but when I run on my server
sudo RAILS_ENV=production bin/delayed_job restart
I get
sudo: bin/delayed_job: command not found
On top of that, if I run the "rake jobs:work RAILS_ENV=production" command Im getting the following error:
PG::FeatureNotSupported: ERROR: SELECT FOR UPDATE/SHARE is not allowed in subqueries
Apparently theres an issue with my psql version.
Is there any way I can get the script to work? Any effective Capistrano recipes available? All ive found on the web are old recipes for Rails 3 and older versions of capistrano.
Thanks in advance.
EDIT:
I have already bundled install the daemons gem and generated "delayed_job:active_record" on my local machine, then proceded to cap deploy which bundle installed and migrated in the production server.
The bin/delayed_job file exists in the server yet it fails with command not found.
And add this to config/environment.rb:
ENV['RAILS_ENV'] ||= 'production'
Then at your production server:
RAILS_ENV=production rake db:migrate
RAILS_ENV=test production generate delayed_job:active_record && RAILS_ENV=production rake db:migrate
Now after you do that:
RAILS_ENV=production script/delayed_job start
As for Capistrano error you are facing, please try to add the command like:
run "cd #{current_path}; #{sudo} RACK_ENV=production bundle exec #{current_path}/bin/delayed_job start"
You must run this on target server:
bundle exec rails generate delayed_job

Rails Production server stylesheet not loading

I am using the digital ocean ruby on rails image which has nginx and unicorn. I bundled my project bundle install and then did service unicorn restart but it doesnt seem to be loading. I had this issue a while ago and solved it using bundle exec ... but I cant remember the full command. I saw a few issue similar to this and tried bundle exec rake assets:precompile as well as editing the production.rb (Why "rails server -e production" makes it "No route matches "/" and stylesheet not loading?) file but it does not work. I am looking for the solution to do this through the command and not through code.
EDIT: bundle exec rake assets:precompile did somewhat work! The problem is that I created an extra css file called main.css which isnt importing, but everything else works!
Use RAILS_ENV=production rake assets:precompile. RAILS_ENV=production specifies the environment in which you run your rake tasks.

Piping email from Postfix to Ruby task error

I'm trying to pipe mail from postfix to a rails tasks. I've got this working successfully when just piping to a vanilla ruby script, but when switching to a rails task I get the following error in the mail.log from Postfix:
(Command died with status 1: "/usr/local/bin/rake". Command output: /usr/lib/ruby/1.9.1/rubygems/dependency.rb:247:in to_specs': Could not find rake (>= 0) amongst [] (Gem::LoadError) from /usr/lib/ruby/1.9.1/rubygems/dependency.rb:256:into_spec' from /usr/lib/ruby/1.9.1/rubygems.rb:1231:in gem' from /usr/local/bin/rake:22:in' )
Here is the piping line from master.cf
email-task unix - n n - 2 pipe
flags=Xhq user=appuser directory=/home/myapp/application/ argv=/usr/local/bin/rake RAILS_ENV=production myapp:process_email
Running the command which rake returns /usr/local/bin/rake so I have verified I have the correct rake path.
I've tried setting ENV['PATH'] inside of the .rake file to /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games and I've also tried cd'ing to the rails app directory in the pipe command but still causes the same error. I've done bundle install and update etc.
Any help is greatly appreciated...thank you!
UPDATE
I tried using bundle exec rake etc as suggested but am now getting this error:
Command died with status 127: "/usr/local/bin/bundle". Command output: bundler: command not found: rake Install missing gem executables with bundle install
I've tried adding gem 'rake' to the Gemfile as well. I'm assuming bundler can't find the Gemfile?
UPDATE (FIXED)
So the command that finally worked was the following:
email-task unix - n n - 2 pipe
flags=Xhq user=appuser directory=/home/myapp/application/ argv=/usr/local/bin/bundle exec /usr/local/bin/rake RAILS_ENV=production mytask
It was a combination of errors on my part.
Full paths need to be used for bundle and rake
app name is not needed in the task param for rake since the directory is being selected before executing
A side issue: an old version of ruby was being used which caused an unrelated gem error after executing the postfix pipe
Appreciate the help everyone.
I think your on the wrong track, it's not the system that can't find rake it's your rubygems. Instead of running rake as your command, try running bundle passing whatever you need to in order for it to basically be bundle exec rake...etc
Update
Based on your new information, it would seem that the rake gem is not installed in your app. Run bundle show rake
Assuming it's not there, I'm wondering why. Did you ship your app with packaged gems or something? Basically, did you not run bundle install in your deployed app?

Sunspot Solr error on rake sunspot:index

I am trying to use Sunspot gem for Solr on a Rails 3 app. I have followed RailsCast #278, since I am using Windows I have to do rake sunspot:solr:run and that seems to start with no error. However when I try to run rake sunspot:reindex I get the error:
No connection could be made because the target machine actively refused it. - connect(2)
I am using mysql2 gem for mysql. Please advise.
I have to do rake sunspot:solr:run and that seems to start with no error
It sounds like the connection is not being accepted at all to Solr, so this part is my first suspicion — you should get a lot of fairly verbose output from Solr when starting it. Can you share some output of that?
Also, what version of java are you running? (java -version)
I had the same problem running "bundle exec rake sunspot:solr:reindex" on Windows. The solution is to add the correct RAILS_ENV option, for me it was test, e.g.
bundle exec rake sunspot:solr:reindex RAILS_ENV=test
Yes, you'll have to have solr server running beforehand:
bundle exec rake sunspot:solr:run RAILS_ENV=test

Use bundle exec rake or just rake?

I learned Rails using just the rake command like rake db:migrate; however, I read that I should be using the bundle exec rake ... instead of just plain rake. Now I'm confused about which to use.
Should I be using bundle exec rake instead of just plain rake or is it just a preference thing? Any insight would be much appreciated! Thanks!
bundle exec executes a command in the context of your bundle.
That means it uses the gems specified in your Gemfile. Much of the time, running bundle exec rake foo has the same results as if you just ran rake foo, especially if you have the same gems installed systemwide as in your Gemfile. However, some applications may specify different versions of gems than the ones you have installed systemwide, and may want those exact gems and versions to be able to run correctly. If you just run without bundle exec, you may get some weird errors.
Using bundle exec guarantees that the program is run with the environment specified in the gemfile, which hopefully means it is the environment that the creators of the program want it to be run in, which hopefully means it should run correctly no matter what weird setup you have on your computer.
It basically standardizes the environment under which the program is run. This helps avoid version hell and makes life much easier.
See http://bundler.io/v1.3/man/bundle-exec.1.html for more info.
$ bundle exec rake db:migrate
Uses the version of rake specified in the Gemfile to execute the rake task db:migrate.
But there is no rake gem specified in the Gemfile!
Yes, but a rake gem was installed as a dependency of some other gem--look in Gemfile.lock. So the rule must be: Uses the version of rake specified in Gemfile.lock.
But Gemfile.lock doesn't specify a specific version--it specifies a version greater than x.y!
Then the rule must be: Uses the version of rake that was installed in the current gemset.
$ rake db:migrate
Normally, when you issue a command on the command line, e.g. rake, your system searches for the command in the list of directories specified in your PATH environment variable. The first directory that contains the command is the version of the command that is used. To see which directory that is, you can do:
$ which rake
So if you execute,
$ rake db:migrate
that may use a different rake gem than the one you installed with bundle install. But, even if your system finds the same rake version as bundle exec, any gems required by the rake source code will be searched for in places outside your project's gemset. Therefore, there are many ways that just:
$ rake db:migrate
can screw things up.
According to the Ruby on Rails Tutorial Book(free online), section 3.6, if you are using rvm 1.11.x+ then you do not need to preface commands with bundle exec.
running any exacutable without bundle exec will have Rubygems fetching the latest version of the gem installed in your system.
By adding the bundle exec prefix instead will have the executable running in the context of your Gemfile.lock, which means that will be run using the version defined in the gem file.

Resources