alexsilva / supervisor

Supervisor process control system for Windows
http://supervisord.org
Other
118 stars 26 forks source link

Unable to run a Uvicorn server from supervisor? #18

Closed seraph-g closed 4 years ago

seraph-g commented 4 years ago

I truly cannot for the life of me figure out what the issue is, I'm trying to move over from circus and would like to run this Uvicorn server with supervisor, but the process keeps exiting for some reason.

Config:

[supervisord]
loglevel=debug

[fcgi-program:uvicorn]
socket=tcp://127.0.0.1:8000
command="C:/Program Files/Python37/Scripts/uvicorn.exe" --fd 0 run:app
numprocs=1  
process_name=uvicorn-%(process_num)d
stdout_logfile=uvicorn.log
autorestart = false
redirect_stderr=true

supervisord.log:

2020-07-01 13:18:26,486 INFO Creating socket tcp://127.0.0.1:8000
2020-07-01 13:18:26,487 INFO Closing socket tcp://127.0.0.1:8000
2020-07-01 13:18:26,487 INFO process group added: 'uvicorn'
2020-07-01 13:18:26,491 INFO supervisord started with pid 9640
2020-07-01 13:18:26,491 INFO Creating socket tcp://127.0.0.1:8000
2020-07-01 13:18:26,493 INFO Spawned: 'uvicorn-0' with pid 5812
2020-07-01 13:18:26,495 DEBG fd <_io.TextIOWrapper name=4 encoding='cp1252'> closed, stopped monitoring <PInputDispatcher at 2479695248904 for <Subprocess at 2479718409928 with name uvicorn-0 in state STARTING> (stdin)>
2020-07-01 13:18:26,495 INFO Closing socket tcp://127.0.0.1:8000
2020-07-01 13:18:26,995 DEBG 'uvicorn-0' stdout output:
INFO:     Started server process [8440]

2020-07-01 13:18:27,495 DEBG 'uvicorn-0' stdout output:
INFO:     Waiting for application startup.

2020-07-01 13:18:27,495 INFO success: uvicorn-0 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2020-07-01 13:18:27,495 INFO exited: uvicorn-0 (exit status 1; not expected)
2020-07-01 13:18:27,497 DEBG fd <StreamAsync(stdout-pid-5812, started daemon 14008)> closed, stopped monitoring <PStreamOutputDispatcher at 2479719010888 for <Subprocess at 2479718409928 with name uvicorn-0 in state EXITED> (stdout)>

uvicorn.log:

INFO:     Started server process [8440]
INFO:     Waiting for application startup.
alexsilva commented 4 years ago

The uvicorn library with the --fd option, it doesn't seem to be supported by windows because it uses a unix socket. uvicorn/main.py

In windows the line above should be as follows:

sock = socket.fromfd(config.fd, socket.AF_INET, socket.SOCK_STREAM)

However after I made the necessary corrections in the supervisor and also in the uvicorn I was able to make the two work together.

Realize that there is a difference in the documented configuration of the supervisor. This is because on windows sharing file descriptor works differently. socket_fd_param=--fd (It is the name of the option that would be placed in the command)

[fcgi-program:uvicorn]
socket=tcp://127.0.0.1:8105
command="C:\Python37\python.exe" -m uvicorn app:run
socket_fd_param=--fd
numprocs=2  
#directory=%(here)s
process_name=uvicorn-%(process_num)d
#stdout_logfile=%(here)s/uvicorn.log
autorestart = false
redirect_stderr=true

Changes have been applied to the windows branch https://github.com/alexsilva/supervisor/commit/fdb591317068a9c129a04bd7f5c2ddbc18f9e6d2

If you want to test install from git python -m pip install git+https://github.com/alexsilva/supervisor.git@windows -U

alexsilva commented 4 years ago

The new supervisor implementation solves the fastcgi problem but the web server needs to support AF_INET sockets with socket.fromfd. https://docs.python.org/2/library/socket.html#socket.AF_INET

seraph-g commented 4 years ago

Thanks! I actually already modified Uvicorn to get it running with AF_INET sockets before, so the tweak to Supervisor is all I need!