Introduction to "Rake tasks"

Introduction to "Rake tasks"

Hello, today I offer you a short introduction to rake tasks ( rake tasks ).

Rake tasks are a way to define reusable and automated tasks in a Ruby application.

They can be used to perform a wide variety of tasks, such as database migrations, report generation, data synchronization, etc.

Chances are you've already encountered "rake tasks" if you've used a framework like Ruby on Rails at least once in your life.

How to use rake tasks ?

To use rake tasks in a Ruby project, you must first install the Rake gem by adding the line gem 'rake' to your application's Gemfile, then running bundle install.

Once Rake is installed, you can create tasks by creating a .rake file in the lib/tasks directory of your application. In that file, you can use Rake's task method to define your task as follows:

namespace :my_tasks do
  desc "Description of my task"
  task :task_name => :environment do
    # code to execute the task goes here
  end
end

Brief explanation: the task method takes the task name as its first argument (here, :task_name) and a list of dependencies as its second argument (here, :environment). You can set a description for the task with the desc method.

To run your newly created task, you can use the rake task_name command in your terminal.

If you want to list the tasks you have defined in your .rake file, you can use the rake -T command which will display a list of all available tasks, along with their descriptions.

There are many other options and methods available to customize and extend the rake tasks of your application; we will expand on this in the following sections.

Going further by using arguments

We can enrich the capabilities of rake tasks by allowing them to accept arguments for their execution. Here is an example :

namespace :my_tasks do
  desc "Print a message"
  task :print_message, [:message] => :environment do |t, args|
    puts args[:message]
  end
end

To run this task and pass it an argument, use the following command:

rake print_message[hello]

This task, as you might expect, will print the message "hello" to the terminal.

Here is another example of a Rake task that takes multiple arguments:

namespace :my_tasks do
  desc "Print a message multiple times"
  task :print_message_multiple, [:message, :count] => :environment do |t, args|
    args[:count].to_i.times do
      puts args[:message]
    end
  end
end

To run this task and pass it two arguments, use the following command:

rake print_message_multiple[hello,5]

This task will print the message "hello" five times to the terminal.

And Ruby on Rails in all this ?

As mentioned earlier in the article, Ruby on Rails uses rake to provide a whole list of handy utility commands to help with development. Commands range from generating controllers and models to commands that list information, for example the list of routes available within our application.

You can also create your own tasks to enrich the capabilities of your Rails application.

Here is an example :

namespace :db do
  desc "Fill the database with sample data"
  task :populate => :environment do
    # Create some sample data here
    User.create(name: "John", email: "john@example.com")
    User.create(name: "Jane", email: "jane@example.com")
    # ...
  end
end

This task creates sample data records in the application's users database table.

This task can be useful to populate the database with test data or to create a demonstration dataset for an application.

To run this task:

rake db:populate

Here is another example of a Rake task that can be useful in a Ruby on Rails application:

namespace :emails do
  desc "Send a reminder email to all users who have not logged in for 30 days"
  task :send_reminders => :environment do
    users = User.where("last_login < ?", 30.days.ago)
    users.each do |user|
      UserMailer.login_reminder(user).deliver_now
    end
  end
end

This task sends a reminder email to all users who have not logged in for 30 days.

To run this task:

rake emails:send_reminders

Consolidate your knowledge

To go further, I recommend consulting the following resources :

I hope this article gives you an idea of what you can do with Rake tasks in your future Ruby projects!

Tags

  • ruby

  • api

This article was posted on

Comments

Loading...

Introduction to "Rake tasks" | DEMILY Clément