DEV Community

Afaq Shahid Khan
Afaq Shahid Khan

Posted on

How to Perform Rake Tasks in Rails

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Step 2: Run the Rake Task

You can run your new Rake task from the command line:

bundle exec rake pdf:delete
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Run bundle install to install the gem.

Step 2: Configure whenever

Initialize whenever in your project:

bundle exec wheneverize .
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Step 3: Update Cron Jobs

Update your cron jobs by running:

bundle exec whenever --update-crontab
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (0)