Debian Codegraph @

testing/web-platform/tests/tools/third_party/websockets/docs/howto/supervisor.rst

raw
1 Deploy with Supervisor
2 ======================
3
4 This guide proposes a simple way to deploy a websockets server directly on a
5 Linux or BSD operating system.
6
7 We'll configure Supervisor_ to run several server processes and to restart
8 them if needed.
9
10 .. _Supervisor: http://supervisord.org/
11
12 We'll bind all servers to the same port. The OS will take care of balancing
13 connections.
14
15 Create and activate a virtualenv:
16
17 .. code-block:: console
18
19 $ python -m venv supervisor-websockets
20 $ . supervisor-websockets/bin/activate
21
22 Install websockets and Supervisor:
23
24 .. code-block:: console
25
26 $ pip install websockets
27 $ pip install supervisor
28
29 Save this app to a file called ``app.py``:
30
31 .. literalinclude:: ../../example/deployment/supervisor/app.py
32
33 This is an echo server with two features added for the purpose of this guide:
34
35 * It shuts down gracefully when receiving a ``SIGTERM`` signal;
36 * It enables the ``reuse_port`` option of :meth:`~asyncio.loop.create_server`,
37 which in turns sets ``SO_REUSEPORT`` on the accept socket.
38
39 Save this Supervisor configuration to ``supervisord.conf``:
40
41 .. literalinclude:: ../../example/deployment/supervisor/supervisord.conf
42
43 This is the minimal configuration required to keep four instances of the app
44 running, restarting them if they exit.
45
46 Now start Supervisor in the foreground:
47
48 .. code-block:: console
49
50 $ supervisord -c supervisord.conf -n
51 INFO Increased RLIMIT_NOFILE limit to 1024
52 INFO supervisord started with pid 43596
53 INFO spawned: 'websockets-test_00' with pid 43597
54 INFO spawned: 'websockets-test_01' with pid 43598
55 INFO spawned: 'websockets-test_02' with pid 43599
56 INFO spawned: 'websockets-test_03' with pid 43600
57 INFO success: websockets-test_00 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
58 INFO success: websockets-test_01 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
59 INFO success: websockets-test_02 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
60 INFO success: websockets-test_03 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
61
62 In another shell, after activating the virtualenv, we can connect to the app —
63 press Ctrl-D to exit:
64
65 .. code-block:: console
66
67 $ python -m websockets ws://localhost:8080/
68 Connected to ws://localhost:8080/.
69 > Hello!
70 < Hello!
71 Connection closed: 1000 (OK).
72
73 Look at the pid of an instance of the app in the logs and terminate it:
74
75 .. code-block:: console
76
77 $ kill -TERM 43597
78
79 The logs show that Supervisor restarted this instance:
80
81 .. code-block:: console
82
83 INFO exited: websockets-test_00 (exit status 0; expected)
84 INFO spawned: 'websockets-test_00' with pid 43629
85 INFO success: websockets-test_00 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
86
87 Now let's check what happens when we shut down Supervisor, but first let's
88 establish a connection and leave it open:
89
90 .. code-block:: console
91
92 $ python -m websockets ws://localhost:8080/
93 Connected to ws://localhost:8080/.
94 >
95
96 Look at the pid of supervisord itself in the logs and terminate it:
97
98 .. code-block:: console
99
100 $ kill -TERM 43596
101
102 The logs show that Supervisor terminated all instances of the app before
103 exiting:
104
105 .. code-block:: console
106
107 WARN received SIGTERM indicating exit request
108 INFO waiting for websockets-test_00, websockets-test_01, websockets-test_02, websockets-test_03 to die
109 INFO stopped: websockets-test_02 (exit status 0)
110 INFO stopped: websockets-test_03 (exit status 0)
111 INFO stopped: websockets-test_01 (exit status 0)
112 INFO stopped: websockets-test_00 (exit status 0)
113
114 And you can see that the connection to the app was closed gracefully:
115
116 .. code-block:: console
117
118 $ python -m websockets ws://localhost:8080/
119 Connected to ws://localhost:8080/.
120 Connection closed: 1001 (going away).
121
122 In this example, we've been sharing the same virtualenv for supervisor and
123 websockets.
124
125 In a real deployment, you would likely:
126
127 * Install Supervisor with the package manager of the OS.
128 * Create a virtualenv dedicated to your application.
129 * Add ``environment=PATH="path/to/your/virtualenv/bin"`` in the Supervisor
130 configuration. Then ``python app.py`` runs in that virtualenv.
131