Erase records every 60 days - ruby-on-rails

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

Related

How to trigger a rake task using whenever in Rails

I am writing a Rake task. I want to trigger it every last sunday of every month at 11 pm.
How can I schedule this using the Whenever gem in Ruby on Rails?
I have my rake task in the following location: app/lib/tasks/my_task.rake
task :create_entries => :environment do
puts "Hello"
end
I don't think there's a rule for "last x day of the month" but you can always put an extra if test inside the block:
every :sunday, :at => '11pm' do
#if the month is different a week from now, we must be in the last
#sunday of the month
if Time.now.month != 1.week.from_now.month
rake "my:rake:task"
end
end
So, this scheduled code will run every sunday, but it will only go on to call the rake task if the time meets the further conditions.

how can i trigger a rake task on every 4th sunday of every month in rails?

i am having a rake task. i want that task to be scheduled to run on every 4th Sunday of every month. i am using whenever gem. how can i define logic in config/schedule.rb so that it will run on every 4th Sunday of every month in Rails? please help me.
this is my rake task app/lib/tasks/my_task.rake.
task :do_something => :environment do
end
this is my code in config/schedule.rb
every :sunday, :at => '12pm' do
runner "Employee.make_checkin_out"
end
how can i change the above logic?
whenever gem gives a facility to generate a cron job. This is a scheduler for rake tasks. You can define like
every 1.month, :at => '4:30 am' do
runner "MyModel.task_to_run_at_four_thirty_morning_every_month"
end
If you want something like 1st sunday every month use cron editor like
0 12 1-7 * * [ "$(date '+\%a')" = "Sun" ]

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'

RoR: Using CRON and Actionmailer to send daily email updates to all users

What exactly would be the best way to go about using a cron task to send daily e-mails of updates to all users on my network? The e-mail would be made up of different information from multiple models.
I want to do something like "1 new friend requests : name ..." from the request model and user model and "There are 3 upcoming events from your friends: event name hosted by name..." from the event and user model.
I realize this is a common task but I didn't see much information on it, so any general tips about doing something like this would be greatly appreciated!
Side note: I will be using the Heroku daily cron plug-in to accomplish this if that matters (although I don't think it should).
I usually just write a rake task and add it to CRON.
The rake task will look like this:
namespace :notifications do
desc "Sends notifications"
task :send => :environment do
MyModel.all_users_to_notify.each do |u|
MyMailer.notification(u).deliver
end
end
end
And your crontab should look like this:
RAILS_ENV=production
HOME=/path/to/your/rails/app
PATH=/path/to/ruby/binaries
30 17 * * * rake notifications:send
If anyone else is looking for this answer, I recommend using the whenever gem for cron tasks. This will keep you from having to write a rake task, as well as giving you a nicer cron syntax:
app/mailers/my_mailer.rb:
class MyMailer < ActionMailer::Base
def my_email
...
end
end
config/schedule.rb
job_type :runner, "cd :path && rvm 2.0.0 do bundle exec script/rails runner -e :environment ':task' :output"
every 1.days, at: "7:00 am", roles: [:app] do
runner "MyMailer.my_email.deliver"
end

Ruby on Rails - Using Timezones in Rake Tasks set for Cron Job

My web application requires 6 different cron jobs to run to update timezone sensitive data within tables at 00:01:00 of each timezone. I want to create rake tasks for each time zone. Can I configure rake tasks to be timezone aware? For e.g., can I do that following in a rake task:
namespace :db do
task :update_EST_records => :environment do
Time.zone = "Eastern Time (US & Canada)"
sql = "UPDATE QUERY GOES HERE WITH CREATED_AT BETWEEN ? AND ?"
ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection.execute(sql,
Time.zone.now.beginning_of_day.utc,
Time.zone.now.end_of_day.utc)
end
end
Yes, you can. Because :update_EST_records depends on :environment task, then you have access to all your Rails environment.
You don't even have to call ActiveRecord::Base.establish_connection as long as you run the update query from your model class instead of raw SQL.
Model.update_all("...", ["CREATED_AT BETWEEN ? AND ?", value, value])

Resources