How to Perform Rake Tasks in Rails
Afaq Shahid Khan
Posted on June 7, 2024
As a Rails developer, you often need to automate tasks such as database migrations, data seeding, or file management. This is where Rake (Ruby Make) tasks come in handy. In this article, I'll guide you through creating and scheduling Rake tasks in a Rails application.
What is a Rake Task?
Rake is a build automation tool written in Ruby. It allows you to define and run tasks using a simple Ruby DSL. In Rails, Rake tasks are commonly used for tasks like database migrations (rake db:migrate
), cleaning logs (rake log:clear
), and many other maintenance tasks.
Creating a Custom Rake Task
Let's start by creating a custom Rake task. Suppose you want to delete all PDF files in your public/pdfs
directory every night. Here's how you can do it.
Step 1: Define the Rake Task
First, create a new Rake task file in the lib/tasks
directory. We'll name it delete_pdfs.rake
.
mkdir -p lib/tasks
touch lib/tasks/delete_pdfs.rake
Open lib/tasks/delete_pdfs.rake
and add the following content:
# lib/tasks/delete_pdfs.rake
namespace :pdf do
desc "Delete all PDFs from public/pdfs and its subdirectories older than midnight"
task delete: :environment do
require 'fileutils'
pdf_directory = Rails.root.join('public', 'pdfs')
if Dir.exist?(pdf_directory)
Dir.glob(File.join(pdf_directory, '**', '*.pdf')).each do |file|
FileUtils.rm(file)
puts "Deleted #{file}"
end
else
puts "Directory #{pdf_directory} does not exist"
end
end
end
Step 2: Run the Rake Task
You can run your new Rake task from the command line:
bundle exec rake pdf:delete
This command will delete all PDF files in the public/pdfs
directory and its subdirectories.
Scheduling the Rake Task
To run the task automatically at midnight every day, you can use the whenever
gem, which provides a clear syntax for writing and deploying cron jobs.
Step 1: Add the whenever
Gem
Add whenever
to your Gemfile
:
gem 'whenever', require: false
Run bundle install
to install the gem.
Step 2: Configure whenever
Initialize whenever
in your project:
bundle exec wheneverize .
This command creates a config/schedule.rb
file. Open this file and add the following content:
# config/schedule.rb
# Set the environment
set :environment, "development" # or "production"
# Define the task schedule
every :day, at: '12:00 am' do
puts 'Delete pdf files'
rake "pdf:delete"
end
Step 3: Update Cron Jobs
Update your cron jobs by running:
bundle exec whenever --update-crontab
This command writes the schedule defined in config/schedule.rb
to your crontab, ensuring that the task runs at the specified time.
Step 4: Verify the Task
Manually run the task to ensure it works correctly:
bundle exec rake pdf:delete
Conclusion
Rake tasks are a powerful way to automate repetitive tasks in your Rails application. By combining Rake with whenever
, you can schedule these tasks to run automatically, making your workflow more efficient.
I hope this guide helps you get started with creating and scheduling Rake tasks in your Rails projects. If you have any questions or tips of your own, feel free to share them in the comments below!
Happy coding!
Posted on June 7, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.