Keep Heroku App from Sleeping

If you liked this post, say thanks by sharing it:

1. Add Scheduler App to Heroku (heroku addons:add scheduler)

2. Create a worker file that pings your app URL


$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (200==$retcode) {
echo “I am working”;}

else {echo “I am sleeping”;}

3. Create a scheduler job that runs every 10 minutes to request the main page of the app. (bin/php /app/www/scheduler.php)

Now every 10 minutes, heroku scheduler will ping your site so it doesn’t go to sleep, and you can still use the single web dyno to run / host static files.

Log Example:

2014-04-08T19:35:15.617772+00:00 heroku[router]: at=info method=HEAD path=/ request_id=f63916ac-0e4c-4b72-b30a-55088bd610b6 fwd="" dyno=web.1 connect=1ms service=8ms status=200 bytes=167
2014-04-08T19:35:15.695511+00:00 app[scheduler.6695]: I am working
2014-04-08T19:35:15.375210+00:00 heroku[scheduler.6695]: State changed from starting to up
2014-04-08T19:35:16.306243+00:00 app[web.1]: - - [08/Apr/2014:19:35:15 +0000] "HEAD / HTTP/1.1" 200 -
2014-04-08T19:35:16.948716+00:00 heroku[scheduler.6695]: Process exited with status 0
2014-04-08T19:35:16.960716+00:00 heroku[scheduler.6695]: State changed from up to complete
If you liked this post, say thanks by sharing it:
Posted in Heroku

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>