0

i have a really big problem with channels. when I try to run asgi server in production the problems come up but there is no problem when running in terminal.

first let me show you a little code

class LogConsumer(AsyncConsumer):
    async def websocket_connect(self, event):
        print('befor')

        await  self.send({

            "type": "websocket.accept",
            "text": "hellow"
        })
        print('after')
    async def websocket_disconnect(self, event):
        print(event)

there are more but i commented them too see problem is solving or not and guess what ...

    application = ProtocolTypeRouter({
        'websocket': AllowedHostsOriginValidator(
            AuthMiddlewareStack(
                URLRouter(
                    [
                        url(r"^ws/monitoring/$", LogConsumer),
    
                    ]
                )
            ),
    
        )
    })



ASGI_APPLICATION = "fradmin_mainserver.routing.application"



CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("localhost", 6379)],
        },
    },
}
ASGI_THREADS = 1000

supervisor config

[fcgi-program:asgi]
socket=tcp://localhost:8008
environment=PYTHONPATH=/home/datis/.pyenv/versions/cv/bin/python
User=datis
environment=HOME="/home/datis",USER="datis"
# Directory where your site's project files are located
directory=/home/datis/PycharmProjects/fradmin_mainserver/

# Each process needs to have a separate socket file, so we use process_num
# Make sure to update "django_chanels.asgi" to match your project name
command=/home/datis/.pyenv/versions/cv/bin/daphne -u /run/uwsgi/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers fradmin_mainserver.asgi:application

# Number of processes to startup, roughly the number of CPUs you have
numprocs=1

# Give each process a unique name so they can be told apart
process_name=asgi%(process_num)d

# Automatically start and recover processes
autostart=true
autorestart=true

# Choose where you want your log to go
stdout_logfile=/var/log/uwsgi/asgi.log
redirect_stderr=true

ok these are configurations .

when i use

daphne fradmin_mainserver.asgi:application --bind 0.0.0.0 --port 8008 --verbosity 1

there is no problem but when use this inside supervisor the only thing i get is :

2021-04-13 11:45:27,015 WARNING  Application instance <Task pending coro=<SessionMiddlewareInstance.__call__() running at /home/datis/.pyenv/versions/3.6.8/envs/cv/lib/python3.6/site-packages/channels/sessions.py:183> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f02e2222d38>()]>> for connection <WebSocketProtocol client=['127.0.0.1', 46234] path=b'/ws/monitoring/'> took too long to shut down and was killed.

even i tryed to start a service with currect code and i made :

[Unit]
Description=daphne daemon
After=network.target


[Service]
PIDFile=/run/daphne/pid
User=root
Group=root
WorkingDirectory=/home/datis/PycharmProjects/fradmin_mainserver/
Environment="DJANGO_SETTINGS_MODULE=fradmin_mainserver.settings"
ExecStart=/home/datis/.pyenv/versions/cv/bin/daphne --bind 0.0.0.0 --port 8008 --verbosity 0 fradmin_mainserver.asgi:application
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
Restart=on-abort
PrivateTmp=true
StandardOutput=file:/var/log/daphne/access.log
StandardError=file:/var/log/daphne/access.log


[Install]
WantedBy=multi-user.target

but the result was the same:

its like websocket_connec() is never called

I tried to create it with syncconsumer but problem whas the same

but when i stop supervisorctl all runs together

192.168.7.100:0 - - [13/Apr/2021:14:38:24] "WSCONNECTING /ws/monitoring/" - -
192.168.7.100:0 - - [13/Apr/2021:14:38:24] "WSCONNECT /ws/monitoring/" - -
before
192.168.7.100:0 - - [13/Apr/2021:14:39:25] "WSDISCONNECT /ws/monitoring/" - -
{'type': 'websocket.disconnect', 'code': 1001}
192.168.7.100:0 - - [13/Apr/2021:14:39:25] "WSCONNECTING /ws/monitoring/" - -
192.168.7.100:0 - - [13/Apr/2021:14:39:25] "WSCONNECT /ws/monitoring/" - -
before
192.168.7.100:0 - - [13/Apr/2021:14:39:27] "WSDISCONNECT /ws/monitoring/" - -

versions: python:3.6.8 django: 2.2.6 channels:2.4.0 channels_redis: 2.4.2 daphne : 2.5.0

help me please it a real product project and i dont what to do anymore i tried everything and readed every line in stack overflow github and etc .

Xeus
  • 92
  • 1
  • 6

1 Answers1

0

change AsyncConsumer to AsyncWebsocketConsumer

Tse7en
  • 1
  • 1