run rake task from crontab - ruby-on-rails

I use Ubuntu, I try run my rake task from cron
My rake task:
namespase :import do
task :import_twitter => :environment do
puts "Twitter importing...."
end
end
schedule.rb file
every 1.minutes do
rake "import:import_twitter", :output => {:error => 'error.log', :standard => 'cron.log'}
end
with whenever I generate crontab task
# Begin Whenever generated tasks for: /home/administrator/www/my_application/config/schedule.rb
PATH=/usr/local/rvm/gems/ruby-1.9.2-p0/bin:/bin:/usr/local/rvm/rubies/ruby-1.9.2-p0/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
* * * * * cd /home/administrator/www/my_application && RAILS_ENV=production /usr/bin/env rake import:import_twitter >> cron.log 2>> error.log
# End Whenever generated tasks for: /home/administrator/www/my_application/config/schedule.rb
In crontab error.log I have
/usr/local/rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/rubygems.rb:340:in `bin_path': can't find executable rake for rake-0.8.7 (Gem::Exception)
from /usr/local/rvm/gems/ruby-1.9.2-p0/bin/rake:19:in `<main>'
But when I run this task from console it work
path_to_my_project >rake import:import_twitter
What's wrong?

I have the same issue with rvm and whenever. If I want it's works I define the HOME VARIABLE and comment the PATH variable and it's works.
I thinks it's a rvm issue with loading of your environment.
# Begin Whenever generated tasks for: supermarmite
SHELL=/bin/bash
HOME=/var/rails/supermarmite
#PATH=/var/rails/supermarmite/.rvm/gems/ruby-1.9.2-p0/bin:/var/rails/supermarmite/.rvm/gems/ruby-1.9.2-p0#global/bin:/var/rails/supermarmite/.rvm/rubies/ruby-1.9.2-p0/bin:/var/rails/supermarmite/.rvm/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/local/mongodb/bin
0 * * * * source ~/.bashrc && cd /var/rails/supermarmite/site/releases/20100930044915 && RAILS_ENV=production rake -s evaluate_notify
# End Whenever generated tasks for: supermarmite

Related

No such file or directory during cap deploy:update

I noticed in the output from cap deploy:update, that it is triggering "deploy:assets:precompile" as an after callback for deploy:update_code:
triggering after callbacks for `deploy:update_code'
* 2013-05-15 11:32:16 executing `deploy:assets:precompile'
triggering before callbacks for `deploy:assets:precompile'
* 2013-05-15 11:32:16 executing `deploy:assets:update_asset_mtimes'
* executing "[ -e /home/johnmerlino/public_html/store.johnmerlino.com/shared/assets/manifest* ] && cat /home/johnmerlino/public_html/store.johnmerlino.com/shared/assets/manifest* || echo"
servers: ["xxx.xx.xx.xxx"]
[xxx.xx.xx.xxx] executing command
command finished in 314ms
* executing "cd -- /home/johnmerlino/public_html/store.johnmerlino.com/releases/20130515153214 && bundle exec rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile"
servers: ["xxx.xx.xx.xxx"]
[xxx.xx.xx.xxx] executing command
** [out :: xxx.xx.xx.xxx] rake aborted!
** [out :: xxx.xx.xx.xxx] No such file or directory - /home/johnmerlino/public_html/store.johnmerlino.com/releases/20130515153214/config/config.yml
** [out :: xxx.xx.xx.xxx
Now the problem there is it says that the latest release doesn't have a "config.yml" file.
Actually in my capistrano script, that file is created after "deploy:update_code":
after "deploy:update_code", "deploy:symlink_shared_configs"
namespace :deploy do
desc "Symlink configuration files"
task :symlink_shared_configs, :roles => [:db,:app] do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
run "ln -nfs #{shared_path}/config/config.yml #{release_path}/config/config.yml"
end
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
end
So shouldn't config.yml be created by this time?
If both are being triggered in the list of callbacks for after deploy:update_code then they're essentially part of the same callback group, but the order in which callbacks in the same group are called depends on the implementation of the callback registration and execution.
If you need them to run in a given order then you can explicitly change when assets:precompile is run by moving it later, or moving the config.yml file earlier in order to guarantee that one comes before the other.
As it stands now, since they both run in the after deploy:update_code the order in which they get executed could be either:
...before...
deploy:update_code
...after... | after group
deploy:symlink_shared_configs | after group
deploy:assets:precompile | after group
...OR...
...before...
deploy:update_code
...after... | after group
deploy:assets:precompile | after group
deploy:symlink_shared_configs | after group
...and based on the fact that you've posted this question, it sounds like the latter instance is happening for you.

rails whenever delete records sql

I tried to a records cleanup after certain period of time (6 month) using gem 'whenever'.
In my whenever scheduler :
every 1.month, at: '1am' do
rake 'lib/tasks/cleanup_user.rake'
end
In the lib/tasks/cleanup_user.rake
#user = User.all.where(:created_at > 'Time.6.month.ago').delete
It seems about right. However, I got error 'uninitialized constant User'. I am relatively new in rails. Please assist me.
EDIT : I changed the game by run clean one line command :
set :output, "log/cron.log"
every 1.minutes, :environment => :development do
command 'User.where("confirmed = 0 AND created_at <= ?", 6.months.ago).delete'
end
I set the specific environment,and run this in command :
whenever --set environment=development --update-crontab userscleaning
Checking at crontab, its there but still not work. Any thought?
Try adding the environment dependency to your task.
task :cleanup_users => :environment do
User.where(:created_at > 'Time.6.month.ago').delete_all
end
If you want the callbacks to trigger, use destroy_all
task :cleanup_users => :environment do
User.where(:created_at > 'Time.6.month.ago').destroy_all
end
Here is a relevant Railscasts.
According to the answers here:
# lib/tasks/delete_old_records.rake
namespace :delete do
desc 'Delete records older than 6 months'
task old_records: :environment do
User.where('created_at > ?', 6.month.ago).destroy_all
end
end
Run with:
RAILS_ENV=production rake delete:old_records
In whenever:
every 1.minutes do
rake "delete:old_records"
end
Or in cron:
0 8 * * * /bin/bash -l -c 'cd /my/project/releases/current && RAILS_ENV=production rake delete:old_records 2>&1'

Load ActiveRecord's data to Capistrano's deploy.rb

I'am using Capistrano for deployment and Sidekiq for queues. I need to load user ids to set :sidekiq_queues variable in deploy.rb file to perform sidekiq queues. Now I use following code
set :sidekiq_queues, ::User.all.map {|u| "-q parsing_user_#{u.id}"}.join(" ") + " -q parsing_user_0"
but it throws following error
./config/deploy.rb:29:in `load': uninitialized constant User (NameError)
I tried to require 'rubygems' and 'active_record' into deploy.rb, but it didn't help.
In result I should have
sidekiq_queues == "-q parsing_user_1 -q parsing_user_2 -q parsing_user_3 -q parsing_user_4 -q parsing_user_5 -q parsing_user_0".
Hardcoding queue names isn't a solution.
Capistrano executes deploy.rb locally, and it doesn't load your Rails environment in deploy.rb.
It might be more trouble than it is worth. Especially if you want to execute this on your remote server, you might consider doing a Rake task instead. The => :environment in the rake task ensures that your Rails environment is loaded.
# in deploy.rb
namespace :sidekiq do
desc "Do something with queues"
task :queues, :roles => :web do
run "cd #{current_path} ; RAILS_ENV=#{rails_env} bundle exec rake sidekiq:queues"
end
end
# you'll need to decide when to execute this in your deployment process,
# something like this:
after "deploy:update_code", "sidekiq:queues"
# in lib/tasks/sidekiq.rake
namespace :sidekiq do
desc "Do something with queues"
task :queues => :environment do
queues = (User.scoped.pluck(:id) + [0]).map{|id| "-q parsing_user_#{id}"}.join(" ")
# do what you need to do
end
end

cron job is not working in ruby on rails in production mode

Cron job is not working in rails 3. I am using whenever gem Here is the code that I am using.
schedule.rb file
every 1.hours do
command "cd /home/me/my_app/current"
rake "thinking_sphinx:index RAILS_ENV=production"
end
every 1.day, :at => '12.01 am' do
command "cd /home/me/my_app/current"
rake "messages_counter_for_user_and_group RAILS_ENV=production"
command "cd /home/me/my_app/current"
runner "RAILS_ENV=production User.update_all(:daily_message_count => 0)"
end
From capistrano deploy script:-
task :long do
transaction do
cron_tab_activate
end
task:cron_tab_activate, :roles=>:app do
send(run_method, "cd #{current_path} && RAILS_ENV=#{stage} whenever --update-crontab test ")
end
I am using rvm. Ruby 1.9.2, Rails 3.0.10
Instead of using custom capistrano tasks I would suggest to load capistrano tasks supplied by whenever gem.
Add to deploy.rb:
require 'whenever/capistrano'

Rake task is not called in production mode for ruby on rails 3

From a controller:-
call_rake :parse_venue, :venue_list_id => venue_list.id
def call_rake(task, options = {})
options[:rails_env] ||= Rails.env
args = options.map { |n, v| "#{n.to_s.upcase}='#{v}'" }
system "rake #{task} #{args.join(' ')} --trace 2>&1 >> #{Rails.root}/log/rake.log &"
end
In development mode this rake task is triggered.. But in case of production mode its not triggered.
PS:- Inspired from rake in background, railscast. There is nothing in rake.log file in production mode
Try the following system call:
system "#{args.join(' ')} rake #{task} --trace 2>&1 >> #{Rails.root}/log/rake.log &"
Verify that "rake" is in the $PATH of the user running your web server / app server

Resources