You created your hobby application in Ruby on Rails and wanted to make it accessible for public. You probably chose Heroku, as it is really easy and fast to deploy your application without any complicated server configuration. You also don’t need to spend a penny if you select a free dyno.
Everything was working fine after the first deployment, but the next day you noticed that it took about 20 seconds for your website to load. Unfortunately, this is one of the drawbacks of using free dynos on Heroku – if there is no web traffic in a 30-minute period, your dyno will go to sleep. So every time after it happens it takes about 20 seconds for the dyno to wake up and start working again.
Personal accounts are given a base of 550 free dyno hours each month. In addition to these base hours, accounts which verify with a credit card will receive an additional 450 hours added to the monthly free dyno quota. This means you can receive a total of 1000 free dyno hours per month, if you verify your account with a credit card.
It means that you just need to verify your account with a credit card to be able to run your web application 24/7 for free. But there is one caveat though – your app needs to receive web traffic in at least every 30 minutes. You can now refresh your page periodically (which is not a wise idea) or you can hope that people will browse your website often enough, that it will never go to sleep. As it’s too optimistic for a hobby project, we need to find another solution.
Let’s create a task that will run every ten minutes and pretend to be browsing the website.
First, create a special endpoint that will be periodically requested. You can skip this and visit any existing application path like root, but I like to have a separate endpoint for it.
class PagesController < ApplicationController
def awake
render plain: "I'm awake"
end
end
Also, add a route for it.
Rails.application.routes.draw do
get '/awake', to: 'pages#awake'
end
In a Rakefile create a task that will be responsible for visiting the endpoint defined above.
desc 'This task is called by the Heroku scheduler add-on'
task keep_awake: :environment do
uri = 'http://your_application.herokuapp.com/awake'
Net::HTTP.get(URI.parse(uri))
end
The code in your application is ready. Now let’s setup Heroku to run the above task periodically. In order to do so, go to your Heroku account, find the application and select Resources.
Under Add-ons look for heroku scheduler and select the only result.
Nothing to do here, just hit Provision.
After you are notified that the add-on was successfully installed, find it on add-ons list and hit it.
If you are configuring Heroku Scheduler for the first time, the jobs list is empty – hit Create job.
Now the crucial part – select the job to be run every 10 minutes, enter rake keep_awake as a command and hit a Save job button.
Done! The job now appears on the list and your application will stay awake forever (unless Heroku decides to restrain such workarounds in the future).
Thanks to Heroku you are able to make your application public really easily with no commitment and don’t have to worry about any server configurations. If you are using your hobby application regularly, keeping it awake all the time will save you plenty of time on waking up the dyno.