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.
The procedure is simple. 1 is Rails, 2 and 3 are done on Heroku side. I will explain step by step.
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
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.
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