[RUBY] How to make batch processing with Rails + Heroku configuration

Purpose

It is a memorandum when I investigated and implemented it because it is necessary to create a batch process that delivers mail once a day with a Rails application developed by myself.

Mail delivery is asynchronously processed by sidekiq, and in the development environment, it was started by sidekiq-schedular every 24 hours. However, Heroku (free tier) used in the production environment did not work well because the web server became Idle in 30 minutes. For this reason, in the production environment, it is configured using the Heroku add-on (Heroku-schedular).

I think that there are many people who choose Rails + Heroku configuration for small start etc., so I hope it will be helpful.

Development environment

procedure

The procedure is simple. 1 is Rails, 2 and 3 are done on Heroku side. I will explain step by step.

  1. Write the task you want to perform in the rake file
  2. Add on Heroku-schedular
  3. Set up a scheduler on Heroku-schedular

1. Write the task you want to perform in the rake file

Create a rake file under/lib/tasks to describe your tasks. For more information on rake tasks, click here (https://railsguides.jp/command_line.html#%E3%82%AB%E3%82%B9%E3%82%BF%E3%83%A0rake%E3%82%BF%E3%82%B9%E3%82%AF)

sample.rake


desc "This task is called by the Heroku scheduler add-on"
task :task_name ,[:arg_1] => :environment do
  #Write the process
end

: task_name is the name of the task. Used when executing a task. : arg_1 is described as an argument if necessary. Also include `` `=>: environment``` when querying the DB within the task.

After creating the rake file, check the operation with the rake command from the console.

rake task_name

2. Add on Heroku-schedular

From the Resources tab of the Heroku console, search for Heroku Schedular in the Add-ons menu. If you select Heroku Schedular, the plan selection screen will pop up. I think the Plan name is "standard-free", but if you press Submit order form without changing it, it will be added.

3. Set up a scheduler on Heroku-schedular

When the add-on is complete, Heroku Schedular has been added to the Installed add-ons in the Overview of the Heroku console, so select it to move to the scheduler settings screen. You can set up new job scheduling with Add job. The command to be executed periodically (rake task_name) is described in Run Command. Please note that the execution time is UTC.

that's all

Recommended Posts

How to make batch processing with Rails + Heroku configuration
[Rails] How to make seed
How to make an almost static page with rails
How to get along with Rails
[Rails] I tried to implement batch processing with Rake task
[Rails] How to use rails console with docker
Deploy to heroku with Docker (Rails 6, MySQL)
How to build Rails 6 environment with Docker
[Rails MySQL] How to reset DB on heroku
[Rails] How to easily implement numbers with pull-down
How to build API with GraphQL and Rails
Error deploying rails5 + Mysql to heroku with Docker-compose
[Rails] How to build an environment with Docker
How to make a follow function in Rails
How to write Rails
How to uninstall Rails
How to make shaded-jar
How to make an image partially transparent in Processing
How to make a factory with a model with polymorphic association
How to implement login request processing (Rails / for beginners)
[Rails] How to search by multiple values ​​with LIKE
How to push an app developed with Rails to Github
How to make LINE messaging function made with Ruby
How to delete a new_record object built with Rails
How to manually generate a JWT with Rails Knock
[Ruby On Rails] How to reset DB in Heroku
[How to insert a video in haml with Rails]
[Rails] How to deal with URL changes after render
How to make Laravel faster with Docker for Mac
How to query Array in jsonb with Rails + postgres
How to get started with creating a Rails app
[Docker + Rails] How to deal with Rails server startup failure
Java --How to make JTable
[Rails] How to use enum
[Rails] How to install devise
How to deploy on heroku
[Rails] How to use enum
How to read rails routes
How to use rails join
How to number (number) with html.erb
How to update with activerecord-import
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
How to automatically generate ER diagram when migrating with Rails6
How to set environment variables when using Payjp with Rails
Let's write how to make API with SpringBoot + Docker from 0
[Rails] How to define macros in Rspec and standardize processing
Learning Ruby with AtCoder 13 How to make a two-dimensional array
[With back tricks] How to introduce React to the simplest Rails
How to specify db when creating an app with rails
How to make an app using Tensorflow with Android Studio