Heroku無停止(スリープさせない・阻止)処理

無料枠でHeroku使っていると30分で停止するので、それを阻止する方法

不要なものもあるのでdef awake():以下を追加でimportは適当なものを

wsgi.py

import os

from botocore.exceptions import ClientError
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

import threading
import requests
import time

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings.local")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)


def awake():
    while True:
        try:
            requests.get("https://appname.herokuapp.com/")
        except ClientError as e:
            print(e)
        time.sleep(300)


t = threading.Thread(target=awake)
t.start()

これで5分ごとアクセスするようになる。

5分って感覚狭すぎなので、10分とか20分でいいと思う。