Facing an error with django channels when deploying to elastic beanstalk - python

I am facing an issue with websockets. When I run the program locally it works fine but when I deploy it to aws elastic beanstalk I face the following issue. I have a simple code as mentioned below.
django.config
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: playzone.wsgi:application
aws:elasticbeanstalk:environment:process:http:
Port: '80'
Protocol: HTTP
aws:elasticbeanstalk:environment:process:websocket:
Port: '5000'
Protocol: HTTP
aws:elasticbeanstalk:environment:proxy:staticfiles:
/static: static
Procfile
web: gunicorn my_app.wsgi
websocket: daphne -b :: -p 5000 my_app.asgi:application
asgi.py
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter,URLRouter
from . import routing
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_app.settings')
application = get_asgi_application()
application = ProtocolTypeRouter({
'http': get_asgi_application(),
'websocket': URLRouter(
routing.websocket_urlpatterns
)
})
And there are 2 more simple files routing.py and consumers.py. I've even configured the load balancers on the environment's settings (80 - HTTP ; 5000 - HTTP). So once deployed, when I try routing to the webpage which has a websocket connection, I get an error saying WebSocket connection to 'ws://yyy.com/ws/' failed:. Please help me out on how I can fix it.
Also am not using redis or any channel layer. Please help me out on how I can fix it.

Related

How to resolve “Error during WebSocket handshake: Unexpected response code: 503” error in a Django Channels Heroku app?

The error in the console :
app.e05f6d1a6569.js:105 WebSocket connection to 'wss://domecode.com/wss?session_key=${sessionKey}' failed: Error during WebSocket handshake: Unexpected response code: 503
(anonymous) # app.e05f6d1a6569.js:105
mightThrow # jquery.js:3762
process # jquery.js:3830
I'm running a Django Channels app ( Messaging app ) on Heroku using Daphne. All the files that correspond to this problem are included in this question.
routing.py - messaging app
from messaging import consumers
from django.urls import re_path
websocket_urlpatterns = [
re_path(r'^ws$', consumers.ChatConsumer),
]
app.js - snipped of the websocket part of the app.js file
var ws_scheme = window.location.protocol == 'https:' ? 'wss' : 'ws';
var socket = new WebSocket(ws_scheme + '://' + window.location.host + '/' + ws_scheme + '?session_key=${sessionKey}');
asgi.py - project's asgi
import os
import django
# from django.core.asgi import get_asgi_application
from channels.routing import get_default_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'domecode.settings')
django.setup() # to deploy with asgi when using channels
application = get_default_application()
Procfile
release: python3 manage.py makemigrations && python3 manage.py migrate
web: daphne domecode.asgi:application --port $PORT --bind 0.0.0.0 -v2
worker: python3 manage.py runworker channel_layer -v2
Django Settings snippet for CHANNEL_LAYERS - REDIS_URL is an env variable in Heroku.
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [config('REDIS_URL')],
},
},
}
Any information on what went wrong here?
Sidenote: It works perfectly fine in the development server however it gives the error mentioned at the top in the production server at https://domecode.com/chat/.
When I send a message on the production server, it gives an error in the console and I don't see my message that was sent unless I reload the page. I've tried looking for similar questions everywhere but none of them were answered in a way that would work for me.
Changes in the Procfile, routing.py of the messaging app and some minor changes such as activating worker dyno on Heroku worked for me.
Procfile should be changed to :
release: python3 manage.py makemigrations && python3 manage.py migrate
web: daphne domecode.asgi:application --port $PORT --bind 0.0.0.0 -v2
worker: python3 manage.py runworker channels --settings=domecode.settings -v2
and Routing.py to :
from messaging import consumers
from django.urls import re_path
websocket_urlpatterns = [
re_path(r'^ws$', consumers.ChatConsumer),
]

Django ModuleNotFoundError: No module named 'mysite.settings' when trying to host application with wsgi server

So I wanted to deploy my first django application on a cherryPy webserver using wsgi. And I have issues with os.environ['DJANGO_SETTINGS_MODULE']. When trying to run application callable it throws error, that module is not found. Project structure:
ResourceManager
ResourceManager
ResourceManager
__init__.py
cherryserver.py
settings.py
urls.py
wsgi.py
SimpleResourceManager
migrations
__init__.py
admin.py
apps.py
models.py
serializers.py
tests.py
urls.py
views.py
manage.py
wsgi.py file:
import os
import sys
from django.core.wsgi import get_wsgi_application
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,BASE_DIR)
os.environ['DJANGO_SETTINGS_MODULE'] = 'ResourceManager.settings'
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ResourceManager.settings')
application = get_wsgi_application()
cherryserver.py:
import cherrypy
from ResourceManager.ResourceManager.wsgi import application
if __name__ == '__main__':
# Mount the application
cherrypy.tree.graft(application, "/")
# Unsubscribe the default server
cherrypy.server.unsubscribe()
# Instantiate a new server object
server = cherrypy._cpserver.Server()
# Configure the server object
server.socket_host = "0.0.0.0"
server.socket_port = 8080
server.thread_pool = 30
# Subscribe this server
server.subscribe()
cherrypy.engine.start()
cherrypy.engine.block()
Application works fine when using command runserver 8080, but when i tried to run it on different server. It says ModuleNotFoundError: No module named "ResourceManager.settings".
So i tried: Change where cherryserver.py is located in directory, I have added additional lines of code to wsgy.py file and I'm running out of ideas what is wrong when I'm deploying my app on different server. Why I'm using cherryPy, well I have to test 5 web servers that are based on python.
Try changing from:
from ResourceManager.ResourceManager.wsgi import application
To:
from wsgi.py import application
This is because they are located in the same directory, if this does not work just mess around with the from as it seems that your path to the wsgi file is wrong.

How to setup the startup command for a flask azure web app

I've created a simple flask web app and managed to publish it with CI/CD from azure devops to an azure web app. The pipelines are working except the startup of my app.
If i look at the log files in the webapp this comes up -
logfile default_docker.log:
2020-01-08T13:04:17.017575225Z Documentation: http://aka.ms/webapp-linux
2020-01-08T13:04:17.017579025Z Python 3.7.5
2020-01-08T13:04:17.017582725Z Note: Any data outside '/home' is not persisted
2020-01-08T13:04:17.093756525Z Starting OpenBSD Secure Shell server: sshd.
2020-01-08T13:04:17.109540649Z Site's appCommandLine: gunicorn --bind = 0.0.0.0 --timeout 600 app: application
2020-01-08T13:04:17.110379356Z Launching oryx with: -appPath /home/site/wwwroot -output /opt/startup/startup.sh -virtualEnvName antenv -defaultApp /opt/defaultsite -bindPort 8000 -userStartupCommand 'gunicorn --bind = 0.0.0.0 --timeout 600 app: application'
2020-01-08T13:04:17.114336587Z Oryx Version: 0.2.20191105.2, Commit: 67e159d71419415435cb5d10c05a0f0758ee8809, ReleaseTagName: 20191105.2
2020-01-08T13:04:17.116548105Z Found build manifest file at '/home/site/wwwroot/oryx-manifest.toml'. Deserializing it...
2020-01-08T13:04:17.118951024Z Build Operation ID: |DGaRVt5jG5c=.2a144509_
2020-01-08T13:04:17.554659456Z Writing output script to '/opt/startup/startup.sh'
2020-01-08T13:04:17.784203265Z Found virtual environment .tar.gz archive.
2020-01-08T13:04:17.784884970Z Removing existing virtual environment directory /antenv...
2020-01-08T13:04:17.788272497Z Extracting to directory /antenv...
2020-01-08T13:04:32.810295030Z Using packages from virtual environment antenv located at /antenv.
2020-01-08T13:04:32.817794689Z Updated PYTHONPATH to ':/antenv/lib/python3.7/site-packages'
2020-01-08T13:04:36.780635398Z usage: gunicorn [OPTIONS] [APP_MODULE]
2020-01-08T13:04:36.780670499Z gunicorn: error: unrecognized arguments: app: application
my simplefied app treeview looks like this -
test_app/
venv/
application/
templates/
__init__.py
routes.py
errors.py
models.py
forms.py
app.py
i've tried different startup commands in azure portal "general setting" but to no solution
gunicorn --bind=0.0.0.0 --timeout 600 app:application
EDIT : added app.py and init.py
app.py:
from application import app, db
from application.models import User, Post
#app.shell_context_processor
def make_shell_context():
return {'db': db, 'User': User, 'Post': Post, 'Classrooms' : Classrooms, 'ClassSession' : ClassSession, 'Teacher' : Teacher}
init.py
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from logging.handlers import RotatingFileHandler
import os
from flask_bootstrap import Bootstrap
import logging
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
login = LoginManager(app)
login.login_view = 'login'
bootstrap = Bootstrap(app)
from application import routes, models, errors
if __name__ == '__main__':
# * --- DEBUG MODE: --- *
app.run(host='127.0.0.1', port=5000, debug=True)
can anybody point me a direction to where i can solve this stupid problem.
thx a lot!!
The gunicorn command isn't actually pointing to the WSGI app object. Try this instead:
gunicorn --bind=0.0.0.0 --timeout 600 application:app

django channels ImproperlyConfigured: Cannot find 'app' in ASGI_APPLICATION module

I am setting up channels asgi with Django. I have tried upgrading Django and Channels.
"Cannot find %r in ASGI_APPLICATION module %s" % (name, path)
django.core.exceptions.ImproperlyConfigured: Cannot find 'app' in ASGI_APPLICATION module <MyApp>.routing
my routing config is as per the tutorial in mysite/routing
application = ProtocolTypeRouter({
# (http->django views is added by default)
'websocket': AuthMiddlewareStack(
URLRouter(
chat.routing.websocket_urlpatterns
)
),
})
and the import statement that is supposed to just be simply
import chat.routing
my directory structure is exactly per the tutorial as well
with setting config
INSTALLED_APPS = [
'channels',
'chat',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
and
ASGI_APPLICATION = 'chat.routing.application'
Thanks
I got this kind of error when running my Django Channels's routing.py using daphne server.
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
This is what documentation explain about daphne servers,
Daphne is a HTTP, HTTP2 and WebSocket protocol server for ASGI and ASGI-HTTP, developed to power Django Channels.
It supports automatic negotiation of protocols; there’s no need for URL prefixing to determine WebSocket endpoints versus HTTP endpoints.
Note: Daphne 2 is not compatible with Channels 1.x applications, only with Channels 2.x and other ASGI applications. Install a 1.x version of Daphne for Channels 1.x support.
As you can see, we can use both HTTP and WSprotocol through daphne server without using Gunicorn server. What you can do is just add below line to top of your routing.py file.
from .wsgi import *
So now your routing.py file should be like this,
# DockerDjangoNginx is my project name
# your routing.py file should be in this location where the wsgi.py file is placed
# DockerDjangoNginx/DockerDjangoNginx/routing.py
from .wsgi import * # add this line to top of your code
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import comapp.routing as routing
application = ProtocolTypeRouter({
# (http->django views is added by default)
'websocket': AuthMiddlewareStack(
URLRouter(
routing.websocket_urlpatterns
)
),
})
Now you can run your daphne server.
(venv) [root#t2mdocker]#daphne -b 0.0.0.0 -p 8000 DockerDjangoNginx.routing:application
2019-05-30 03:33:06,390 INFO Starting server at tcp:port=8000:interface=0.0.0.0
2019-05-30 03:33:06,391 INFO HTTP/2 support enabled
2019-05-30 03:33:06,391 INFO Configuring endpoint tcp:port=8000:interface=0.0.0.0
2019-05-30 03:33:06,392 INFO Listening on TCP address 0.0.0.0:8000
If you see something like this HTTP/2 support not enabled (install the http2 and tls Twisted extras) when running daphne server, you can run pip install -U Twisted[tls,http2] to correct those errors.
Pretty sure this was the problem. Needed to add this asgi.py file next to wsgi.py
import os
import django
from channels.routing import get_default_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<myproj>.settings")
django.setup()
application = get_default_application()
and start server with
(vEnv)$daphne <myproj>.asgi:application --port 8888
Change:
ASGI_APPLICATION = 'myproject.routing.application'
To
ASGI_APPLICATION = "myproject.asgi.application"
and issue will hopefully be solved, check the official channels site for more details (https://channels.readthedocs.io/en/stable/installation.html)
Try the following, it worked for me:
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from chat import routing # This change
application = ProtocolTypeRouter({
# (http->django views is added by default)
'websocket': AuthMiddlewareStack(
URLRouter(
routing.websocket_urlpatterns #This change
)
),
})
Check your settings.py, you stated it included:
ASGI_APPLICATION = 'chat.routing.application'
But chat is your added App, it should be the name of the django project itself, so in the tutorial you said you followed it should be something like:
ASGI_APPLICATION = 'mysite.routing.application'
# or
ASGI_APPLICATION = 'core.routing.application'

Celery not work: Cannot connect to amqp://guest:**#127.0.0.1:5672//

I can not run the celery worker + docker + django. I download image rabbit and linked worker, and at run I get error: Cannot connect to amqp://guest:**#127.0.0.1:5672//: [Errno 111] Connection refused.
worker_1. Django: 1.11, calary: 4.1.0. What doing wrong?
docker-compose
rabbit:
image: rabbitmq:latest
ports:
- "5672:5672"
worker:
build: ./project
volumes:
- ./main:/src/app
depends_on:
- rabbit
links:
- web #django project
entrypoint: /src/app/calery.sh
calery
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
app = Celery('app')
app.config_from_object('django.conf:settings', namespace='APP')
app.autodiscover_tasks()
#app.task(bind=True)
def add():
print('Task')
celery.sh
#!/bin/bash
cd app
celery -A app worker -l info
The error is caused by invalid host for CELERY_BROKER_URL. Based on the error you provided, it seems that the host in your broker url is 127.0.0.1, since you are using docker, this will not work unless you provide the public IP of your host. You need to update the host in your CELERY_BROKER_URL to use the service name in you compose file. In your case it is rabbit. Something like below should work:
CELERY_BROKER_URL = 'amqp://guest:guest#rabbit:5672/%2F'
Change the user and password and other details.
If you can't access with the guest:guest, add your own user to the system. This doc can help you setup your own user, password and Virtual Host inside your RabbitMQ server.
http://docs.celeryproject.org/en/latest/getting-started/brokers/rabbitmq.html#broker-rabbitmq

Categories