4

I run my django project on local which import models on consumers.py that work but when I deploy on heroku error occurred

2021-11-06T10:22:14.039525+00:00 app[web.1]: Traceback (most recent call last):
2021-11-06T10:22:14.039542+00:00 app[web.1]:   File "/app/.heroku/python/bin/daphne", line 8, in <module>
2021-11-06T10:22:14.039607+00:00 app[web.1]:     sys.exit(CommandLineInterface.entrypoint())
2021-11-06T10:22:14.039607+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/daphne/cli.py", line 170, in entrypoint
2021-11-06T10:22:14.039690+00:00 app[web.1]:     cls().run(sys.argv[1:])
2021-11-06T10:22:14.039697+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/daphne/cli.py", line 232, in run
2021-11-06T10:22:14.039772+00:00 app[web.1]:     application = import_by_path(args.application)
2021-11-06T10:22:14.039778+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/daphne/utils.py", line 12, in import_by_path
2021-11-06T10:22:14.039824+00:00 app[web.1]:     target = importlib.import_module(module_path)
2021-11-06T10:22:14.039830+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/importlib/__init__.py", line 127, in import_module
2021-11-06T10:22:14.039892+00:00 app[web.1]:     return _bootstrap._gcd_import(name[level:], package, level)
2021-11-06T10:22:14.039898+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
2021-11-06T10:22:14.039963+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
2021-11-06T10:22:14.040002+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
2021-11-06T10:22:14.040040+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
2021-11-06T10:22:14.040078+00:00 app[web.1]:   File "<frozen importlib._bootstrap_external>", line 850, in exec_module
2021-11-06T10:22:14.040124+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
2021-11-06T10:22:14.040163+00:00 app[web.1]:   File "/app/./spotgame/asgi.py", line 16, in <module>
2021-11-06T10:22:14.040212+00:00 app[web.1]:     import spotifymusicgame.routing
2021-11-06T10:22:14.040218+00:00 app[web.1]:   File "/app/./spotifymusicgame/routing.py", line 3, in <module>
2021-11-06T10:22:14.040261+00:00 app[web.1]:     from . import consumers
2021-11-06T10:22:14.040268+00:00 app[web.1]:   File "/app/./spotifymusicgame/consumers.py", line 59, in <module>
2021-11-06T10:22:14.040324+00:00 app[web.1]:     from .models import roomInfo
2021-11-06T10:22:14.040330+00:00 app[web.1]:   File "/app/./spotifymusicgame/models.py", line 10, in <module>
2021-11-06T10:22:14.040373+00:00 app[web.1]:     class songModel(models.Model):
2021-11-06T10:22:14.040379+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 108, in __new__
2021-11-06T10:22:14.040441+00:00 app[web.1]:     app_config = apps.get_containing_app_config(module)
2021-11-06T10:22:14.040448+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
2021-11-06T10:22:14.040534+00:00 app[web.1]:     self.check_apps_ready()
2021-11-06T10:22:14.040540+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/apps/registry.py", line 136, in check_apps_ready
2021-11-06T10:22:14.040599+00:00 app[web.1]:     raise AppRegistryNotReady("Apps aren't loaded yet.")
2021-11-06T10:22:14.040617+00:00 app[web.1]: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

This is my consumers.py

import json
from channels.generic.websocket import AsyncWebsocketConsumer
from channels.db import database_sync_to_async
from .models import roomInfo
class ChatConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name

        # Join room group
        await self.channel_layer.group_add(
            self.room_group_name,
            self.channel_name
        )

        await self.accept()

    async def disconnect(self, close_code):
        # Leave room group
        await self.channel_layer.group_discard(
            self.room_group_name,
            self.channel_name
        )

    # Receive message from WebSocket
    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']
        username = text_data_json['username']
        print(username)

        # Send message to room group
        await self.channel_layer.group_send(
            self.room_group_name,
            {
                'type': 'chat_message',
                'message': message,
                'username': username,
            }
        )

    # Receive message from room group
    async def chat_message(self, event):
        message = event['message']
        username = event['username']
        max_user = await self.max_users()
        print(max_user)

        # Send message to WebSocket
        await self.send(text_data=json.dumps({
            'message': message,
            'username': username,
        }))
    
    @database_sync_to_async
    def max_users(self):
        max_user = roomInfo.objects.get(id=1).max_player
        return max_user

asgi.py

import os
import django

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import spotifymusicgame.routing

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'spotgame.settings')
django.setup()
application = ProtocolTypeRouter({
  "http": get_asgi_application(),
  "websocket": AuthMiddlewareStack(
        URLRouter(
            spotifymusicgame.routing.websocket_urlpatterns
        )
    ),
})

wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'spotgame.settings')

application = get_wsgi_application()

settings.py


ASGI_APPLICATION = "spotgame.routing.application"

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
        },
    },
}

Procfile

release: python manage.py migrate
web: daphne spotgame.asgi:application --port $PORT --bind 0.0.0.0 -v2
worker: python manage.py runworker channels --settings=spotgame.settings -v2

I assume heroku can't load models in time. Please let me know if it is unclear what I am asking or if I need to show any other files.

Beso
  • 1,176
  • 5
  • 12
  • 26
Aleck
  • 111
  • 1
  • 1
  • 5

1 Answers1

7

i found problem now on asgi.py daphne can't find Django INSTALLED_APPS so i setup DJANGO_SETTINGS_MODULE that work


import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'spotgame.settings')
django.setup()

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import spotifymusicgame.routing

application = ProtocolTypeRouter({
  "http": get_asgi_application(),
  "websocket": AuthMiddlewareStack(
        URLRouter(
            spotifymusicgame.routing.websocket_urlpatterns
        )
    ),
})

This my INSTALLED_APPS in settings.py


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'corsheaders',
    'django_extensions',
    'channels',
    'spotifymusicgame',
    'users',

]

Aleck
  • 111
  • 1
  • 1
  • 5