cron with whenever gem not running - ruby-on-rails-3

I am using thinking sphinx for search.It searches properlt but the problem was whenever i add new records i have to do rake thinking_sphinx:index manually.So to run it automatically i am using whenever gem with cron but still its not happening automatically,Either i have to do
rake thinking_sphinx:index
or
whenever -w to create index automatically
Following is the code of config/schedule.rb :
every 10.minutes do
rake "thinking_sphinx:index", :environment => :development
end
every :reboot do
rake "thinking_sphinx:start",:environment => :development
end
when i do crontab -l i get following :
# Begin Whenever generated tasks for: store
#reboot /bin/bash -l -c 'cd /home/user/newsvn/alumnicell && RAILS_ENV=development bundle exec rake thinking_sphinx:start --silent'
0,10,20,30,40,50 * * * * /bin/bash -l -c 'cd /home/user/newsvn/alumnicell && RAILS_ENV=development bundle exec rake thinking_sphinx:index --silent'
# End Whenever generated tasks for: store
What and where is the problem that this not working?
Also i changed environment to development.by default it was production.what is the difference in these 2?

You can assign log file to track errors, then post it here.
Also I advise to set :environment globally.
Try this code:
set :environment, :development
set :output, 'tmp/whenever.log'
every 10.minutes do
rake "thinking_sphinx:index"
end
every :reboot do
rake "thinking_sphinx:start"
end

I will advise you to use thinking sphinx's delta indexes instead of using whenever cron jobs to rebuild thinking sphinx every 10 minutes.
As it's described here you should just add a boolean column delta to your model, set set_property :delta => true in your define_index block and run rake ts:rebuild
That's it.

Related

rake db tasks disappear in deployment

I have a Rails 3.2.8 app which is running nicely in development on my machine, with Postgres as the db backend for ActiveRecord. However, when I deploy my code to an EC2 server, either via capistrano or by rsync'ing my project to the EC2 instance, rake no longer lists any of the db: tasks:
$ rake -T
rake about # List versions of all Rails frameworks and the environment
rake assets:clean # Remove compiled assets
rake assets:precompile # Compile all the assets named in config.assets.precompile
rake doc:app # Generate docs for the app -- also available doc:rails, doc:guides, doc:plugins (options: TEMPLATE=/rdoc-t...
rake load_gml # Load GML source files into the environment database
rake log:clear # Truncates all *.log files in log/ to zero bytes
rake middleware # Prints out your Rack middleware stack
rake notes # Enumerate all annotations (use notes:optimize, :fixme, :todo for focus)
rake notes:custom # Enumerate a custom annotation, specify with ANNOTATION=CUSTOM
rake rails:template # Applies the template supplied by LOCATION=(/path/to/template) or URL
rake rails:update # Update configs and some other initially generated files (or use just update:configs, update:scripts, or u...
rake routes # Print out all defined routes in match order, with names.
rake secret # Generate a cryptographically secure secret key (this is typically used to generate a secret for cookie se...
rake stats # Report code statistics (KLOCs, etc) from the application
rake test # Runs test:units, test:functionals, test:integration together (also available: test:benchmark, test:profil...
rake test:recent # Run tests for {:recent=>"test:prepare"} / Test recent changes
rake test:single # Run tests for {:single=>"test:prepare"}
rake test:uncommitted # Run tests for {:uncommitted=>"test:prepare"} / Test changes since last checkin (only Subversion and Git)
rake time:zones:all # Displays all time zones, also available: time:zones:us, time:zones:local -- filter with OFFSET parameter,...
rake tmp:clear # Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions:clear, tmp:cache:clear, tmp:sock...
rake tmp:create # Creates tmp directories for sessions, cache, sockets, and pids
rake -P also reports no db: tasks.
I'm guessing it's some sort of dependency problem, but I can't see what that would be since I'm declaring all my gem dependencies in a Bundler Gemfile. Postgres 9.2.1 is installed and working, but even if it wasn't I don't get why the rake task would vanish.
Any clues as to what's going on gratefully received!
And it turns out to be stupid-simple: I had copied the deploy.rb file from a previous project which didn't use ActiveRecord, so I hadn't set the role db:. I'm still not sure why that meant that the rsync'ed copy of my project also didn't have any rake db: tasks, but I'm not going to spend any more cycles worrying about that.

restart sunspot solr when make a deploy with capistrano in rails 3.1

I have a project rails 3.1 in production environment.
This is my deploy.rb now:
$:.unshift(File.expand_path('./lib', ENV['rvm_path'])) # Add RVM's lib directory to the load pathe
require "rvm/capistrano" # Load RVM's capistrano plugin.
require "bundler/capistrano"
set :rvm_ruby_string, 'ruby-1.9.2-p318#global'
set :rvm_type, :user
set :application, "domain.com"
set :user, 'user'
#set :repository, "#{user}#ip.ip.ip.ip:~/app"
set :repository, "ssh://git#bitbucket.org/user/app.git"
set :keep_releases, 3
set :scm, :git
set :use_sudo, false
set :deploy_to, "~/#{application}"
#set :deploy_via, :copy
set :unicorn_conf, "#{deploy_to}/current/config/unicorn.rb"
set :unicorn_pid, "#{deploy_to}/shared/pids/unicorn.pid"
set :deploy_via, :remote_cache
ssh_options[:forward_agent] = true
default_run_options[:pty] = true
role :web, "ip.ip.ip.ip" # Your HTTP server, Apache/etc
role :app, "ip.ip.ip.ip" # This may be the same as your `Web` server
role :db, "ip.ip.ip.ip", :primary => true # This is where Rails migrations will run
namespace :deploy do
task :restart do
run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -USR2 `cat #{unicorn_pid}`; else cd #{deploy_to}/current && bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D; fi"
end
task :start do
run "bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D"
end
task :stop do
run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -QUIT ` cat #{unicorn_pid}`; fi"
end
end
load 'deploy/assets'
after "deploy:restart", "deploy:cleanup"
I want make these tasks in capistrano. Now I perform these tasks manually:
1º I kill sunspot solr pid with:
a) Find the pid with ps aux | grep 'solr'
b) Kill pid with kill pid_number
2º Remove index solr in production environment if exist with:
a) rm -r solr/data/production/index
3º turn on sunspot solr with:
a) RAILS_ENV=production rake sunspot:solr:start
4º Reindex models with:
a) RAILS_ENV=production rake sunspot:mongo:reindex
My question is:
How can I add these tasks to my deploy.rb?
Thank you!
This might be a good starting point:
namespace :solr do
task :reindex do
run "cd #{current_path} && #{rake} RAILS_ENV=#{rails_env} sunspot:solr:reindex"
end
end
You can just call the rake sunspot:solr:stop instead of kill? I'm not sure you need to remove the index if you are going to do a reindex...
task :stop_solr do
begin
run("cd #{deploy_to}/current && /usr/bin/env rake sunspot:solr:stop RAILS_ENV=#{rails_env}")
rescue Exception => error
puts "***Unable to stop Solr with error: #{error}"
puts "***Solr may have not been started. Continuing anyway.***"
end
end
#restart and reindex any newly added full search fields:
task :restart_solr do
begin
run("cd #{release_path} && /usr/bin/env rake sunspot:solr:start RAILS_ENV=#{rails_env}")
rescue Exception => error
puts "***Unable to start Solr with error: #{error}."
puts "***Continuing anyway.***"
end
end
task :reindex_solr do
begin
run("cd #{release_path} && /usr/bin/env rake sunspot:reindex RAILS_ENV=#{rails_env}")
rescue Exception => error
puts "***Unable to reindex Solr with error: #{error}"
puts "***Continuing anyway.***"
end
end
Also, as I mentioned in my comment to Kris's answer, you'll have issues if you clean up old Capistrano directories, unless you kill the SOLR process and force it to point to new index files. One way to avoid this scenario is to set up SOLR in a shared directory and reset the symlink during deployment.

Rails 3 / whenever cron not firing

I am using whenever to fire a rake task every 5 minutes for my app.
schedule.rb:
every 5.minutes do
rake "audit",
:environment => 'development'
end
"whenever" in console:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /Users/john/Sites/rambler && RAILS_ENV=development bundle exec rake audit --silent'
"rake audit" in console works properly.
So all looks good .... except it doesn't work. Nothing happens every five minutes.
Is this because I am trying to run it in development / local?
Thanks!
You need to update your cron file every time you change it.
After you have addded your cron job do this:
whenever --update-crontab 'project_name'
Also I only found whenever working fine in production mode only.
UPDATE:
I have found that we can use whenever in development mode also. Just add
set :environment, "development"
set :output, {:error => "log/error.log", :standard => "log/cron.log"}
to your scehdule.rb file. ( The log one is optional but still you can use that for testing purpose)
Finally I have solved how to run the gem Whenever. It's working good on production, but not in development mode (I think that to working good in dev mode you must do some tricks).
see this answer for working in dev mode: Cron not working in Whenever gem
Then, these are the processes to do:
install the gem
write your scheduler.rb file
push to the remote server
login to the remote server (for example with ssh)
see if whenever is good uploaded by running in terminal: whenever
update whenever crontab by running: whenever --update-crontab
restart the server crontab (for example in ubuntu): sudo service cron restart
check if crontab are good implemented on the server: crontab -l
That is!

Rails 3 - Whenever gem error: /usr/bin/env: ruby: No such file or directory

When using the 'whenever gem', I get an error in the log:
/usr/bin/env: ruby: No such file or directory
It works when I run the job manually. I've installed everything with RVM.
I've used the which command to find where my Ruby installation is, and I get:
kevin#lovely:/opt/personal$ which ruby
/home/kevin/.rvm/rubies/ruby-1.9.2-p290/bin/ruby
and I've checked my $PATH variable, where it returns:
kevin#lovely:/opt/personal$ echo $PATH
/home/kevin/.rvm/gems/ruby-1.9.2-p290/bin:/home/kevin/.rvm/gems/ruby-1.9.2-p290#global/bin:/home/kevin/.rvm/rubies/ruby-1.9.2-p290/bin:/home/kevin/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
I believe this should be set up right, but I'm probably wrong since it doesn't work. Can anyone point me in the right direction?
If you're interested, this is what my whenever crontab output is:
# Begin Whenever generated tasks for: rss
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /opt/personal && script/rails runner -e development '\''FeedEntry.update_from_feed("http://lovely/blog/feed/")'\'' >> /opt/personal/log/feedzirra.log 2>&1'
You're probably long past this issue but for future reference:
I had a similar problem only I was getting
/usr/bin/env: ruby: No such file or directory
It turned out the first line of the file script\rails was #!/usr/bin/env ruby1.9.1, which tells the system to invoke it with ruby1.9.1 as explained here. But it should have been #!/usr/bin/env ruby1.9.3 since that was the version I had installed.
Hope this helps someone in the future :)
My issue was that ruby is in /usr/local/bin which is not in the path of a headless bash. So I just made my rake task line in schedule.rb:
job_type :rake, "cd :path && PATH=/usr/local/bin:$PATH RAILS_ENV=:environment bundle exec rake :task :output"
I am successfully using whenever with RVM and bundler in production. Here are the relevant pieces of my capistrano setup that may help you:
# rvm and bundler integration
require 'rvm/capistrano'
require 'bundler/capistrano'
# RVM environment
set :rvm_ruby_string, "ruby-1.9.2#mygemset"
# crontab
set :whenever_roles, :cron
set :whenever_command, "bundle exec whenever"
set :whenever_environment, defer { stage }
require 'whenever/capistrano'
The :whenever_environment setting is because I am using a multi-stage deployment setup. You can ignore that or set it to a string that matches your setup if needed.
Most of this information can be found at the whenever github page under the "Capistrano integration" and "RVM Integration" section headers in the README.
I hope that helps.
I solved the problem about the same as Duke. Except I figure out that $PATH variable is not working for me.
sys_path = '/home/[user]/.rbenv/versions/[ruby_version]/bin'
job_type :runner, "cd :path && PATH=#{sys_path} bin/rails runner -e :environment ':task' :output"
job_type :rake, "cd :path && PATH=#{sys_path} :environment_variable=:environment bin/bundle exec rake :task --silent :output"
If none of these worked for you, try:
gem install rails
This did the job for me, hope it helps!

rake aborted! no such file to load -- bundler/setup

I run a rake task every night via cron (as root), when it runs it gives the error:
rake aborted!
no such file to load -- bundler/setup
which I get in an email
When I run it manually (as root), it runs just fine.
I am running rvm if that helps.
I am not really sure what to add to help, but here are a few things.
# ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [i686-linux]
# rails -v
Rails 3.0.9
# gem -v
1.8.5
try to set up your cron task like this
* * * * * /bin/bash -l -c 'rake blah:blah'