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'
Related
I want to use 'whenever' this gem to check my all projects are still not out of deadline. I wrote this code but it didn't work and change status in the database. Can somebody give me some advises. Thank you for helping!
config/schedule.rb
set :environment, :development
every 1.day, at: '11:3 am' do
rake 'project:close_project'
end
app/models/project.rb
def self.close_project(dt)
# 締切日が過ぎているプロジェクトを抽出
Project.where(deadline > dt).each do |project|
# 対象プロジェクトを終了状態に
project.update!(status: 'closed')
end
end
product.rake
namespace :product do
task :close_project => :environment do
Project.close_project(Date.today)
end
end
Whenever creates jobs based on CronJob format. So to run your jobs periodically, you should run whenever command and copy and pasting the results to crontab by running crontab -e or do this task automatically just by running whenever -w.
i'm trying to understand how capistrano 3.1 is working, but because of its lack of documentation (its capistrano, so...), im running below my understanding.
Let me explain.
Here's a snippet taken from capistrano/rails gem
namespace :deploy do
desc 'Runs rake db:migrate if migrations are set'
task :migrate => [:set_rails_env] do
on primary fetch(:migration_role) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :rake, "db:migrate"
end
end
end
end
#[...]
end
when execute cap integration deploy:migrate, it sends the following command:
cd /srv/app/releases/20131106101722 && ( RAILS_ENV=integration /tmp/app/rvm-auto.sh . rake assets:precompile )
I changed a little bit the (non-working) code provided for delayed_job into that
namespace :delayed_job do
def args
fetch(:delayed_job_args, '')
end
def delayed_job_roles
fetch(:delayed_job_server_role, :app)
end
def delayed_job_bin
fetch(:delayed_job_bin, :'bin/delayed_job')
end
desc 'Restart the delayed_job process'
task :restart do
on roles(delayed_job_roles) do
within release_path do
with rails_env: fetch(:rails_env) do
execute delayed_job_bin, 'restart', args
end
end
end
end
end
And i get the following command cd /srv/winddle/current && ( RAILS_ENV=integration bin/delayed_job restart )
Obviously, it misses the bundle exec command.
I dive deeply into capistrano/bundler and capistrano/rails to look for some kind of hook that would add bundle exec automatically to any of these commands (or force the register of ssh kits commands) but couldnt find any.
The only solution i found is to use
execute :bundle, :exec, delayed_job_bin, :start, args which is not acceptable of course.
Anyone proper solution / explanation is welcomed.
Regards
Add the following line in deploy.rb, then use the code provided by delayed_job other than changing script to bin, which I see you already did:
set :bundle_bins, fetch(:bundle_bins, []).push('bin/delayed_job')
For users of RVM, add this instead:
set :rvm_map_bins, fetch(:rvm_map_bins, []).push('bin/delayed_job')
Source: https://github.com/capistrano/bundler#usage.
I'm literally just starting out with Capistrano and also struggling against a lack of documentation, so sorry if this post misses the mark.
v3 relies a lot on sshkit, so reading the documentation for that should be a big help. The readme gives an example that may solve your problem.
SSHKit.config.command_map.prefix[:rake].push("bundle exec")
puts SSHKit.config.command_map[:rake]
# => bundle exec rake
I also found an alternative solution in a Semaphore blog post.
SSHKit.config.command_map[:rake] = "bundle exec rake"
SSHKit.config.command_map[:rails] = "bundle exec rails"
what's up?
My friend created a rake task to update our data in the database (because we have db changes). Following is the task:
namespace :db do
task :update_database => :environment do
puts "Update do banco"
posts = Post.where("source_id is null").order("id")
done = Array.new
posts.each do |post|
if post.source_id.nil? and !done.include?(post)
posts2 = Post.where("content LIKE ? AND id != ?", post.content, post.id)
done.concat(posts2)
posts2.each do |post2|
post2.source_id = post.id
post2.save
end
end
end
end
end
I already executed this rake task in my localhost, but I deploy my project to heroku and now my project won't open online. I don't remember what's the command to execute rake tasks and I can't find it in no place.
My questions is:
What's the command to execute rake tasks?
What's the command to execute rake tasks on heroku? Just "heroku run "?
Thanks!
heroku run bundle exec rake db:update_database
should do.
bundle exec ensures that the script is run in the context of current bundle.
I need to erase records in my offers models if the record has more that 60 days from the created_at date.
I only found information about how to populate my model with a rake task, but I couldn't find information about how to make a rake task to delete records. So I just wonder if I have to do this with a task or if rails has something else to do this.
Create a file for the task:
# lib/tasks/delete_old_records.rake
namespace :delete do
desc 'Delete records older than 60 days'
task :old_records => :environment do
Model.where('created_at < ?', 60.days.ago).each do |model|
model.destroy
end
# or Model.delete_all('created_at < ?', 60.days.ago) if you don't need callbacks
end
end
Run with:
RAILS_ENV=production rake delete:old_records
Schedule it to run with cron (every day at 8am in this example):
0 8 * * * /bin/bash -l -c 'cd /my/project/releases/current && RAILS_ENV=production rake delete:old_records 2>&1'
You can also use the whenever gem to create and manage your crontab on deploys:
every 1.day, :at => '8:00 am' do
rake "delete:old_records"
end
Learn more about the gem on Github.
60.days.ago generates a timestamp like
Fri, 08 Aug 2014 15:57:18 UTC +00:00
So you'd need to use < to look for records older(less) than the timestamp.
Online.delete_all("created_at < '#{60.days.ago}'")
It's just a slight oversight by Unixmonkey.
I'd also use the delete_all instead of looping each iterating in a block.
You can create a rake task to delete expired offers , or create class method for your offers model and call it using, for example, whenever gem.
You can delete all records older than 60 days in a single query like so:
Model.where("created_at < '#{60.days.ago}'").delete_all
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