Ticket #5601: 5601-systemd-documentation.patch

File 5601-systemd-documentation.patch, 4.9 KB (added by Richard Wall, 9 years ago)

an example of systemd socket activation of twistd web

  • doc/core/howto/listings/systemd/twistedweb.service

     
     1[Service]
     2ExecStart=/home/richard/projects/combinator_paths/bincache/twistd --nodaemon \
     3    web --port systemd:domain=INET:index=0 --path .
     4Environment=\
     5    PYTHONPATH=/home/richard/projects/Divmod/trunk/Combinator
     6WorkingDirectory=/home/richard/www
     7User=richard
     8NonBlocking=true
  • doc/core/howto/listings/systemd/twistedweb.socket

     
     1[Socket]
     2ListenStream=0.0.0.0:8080
     3
     4[Install]
     5WantedBy=sockets.target
  • doc/core/howto/endpoints.xhtml

     
    236236  </li>
    237237</ul>
    238238
     239<h4>Using systemd Server Endpoint</h4>
     240<p>Here is an example which demonstrates how to run a simple twisted
     241  web server using systemd socket activation.</p>
     242<ol>
     243  <li>Create a systemd socket file.
     244    This file configures systemd to listen on an IPv4 address
     245    on port 8080.
     246    <a href="listings/systemd/twistedweb.socket" class="listing">/etc/systemd/system/twistedweb.socket</a>
     247  </li>
     248
     249  <li>Create a systemd service file.
     250    This file contains:
     251    <ol>
     252      <li>a twistd command line: use the --nodaemon flag because systemd is
     253        designed to supervise normal child processes. The index=0 argument
     254        causes twisted to listen on the first file descriptor inherited from
     255        systemd.</li>
     256      <li>a process environment: in this example, twisted has been installed
     257        with Combinator so the Combinator path is added to PYTHONPATH.</li>
     258    </ol>
     259    <a href="listings/systemd/twistedweb.service" class="listing">/etc/systemd/system/twistedweb.service</a>
     260  </li>
     261  <li>Reload systemd. This forces it to read the new configuration
     262    files.
     263    <pre class="shell">
     264      $ sudo systemctl --system daemon-reload
     265    </pre>
     266  </li>
     267  <li>Start systemd listening on the new port. Twisted will not start
     268    until the first client connects
     269    <pre class="shell">
     270      $ sudo systemctl start twistedweb.socket
     271      $ sudo netstat -lntp | grep 8080
     272      tcp        0      0 127.0.0.1:8080              0.0.0.0:*                   LISTEN      1/init
     273      $ ps -ef | grep twistd
     274      richard   5815  4237  0 01:20 pts/5    00:00:00 grep --color=auto twistd
     275      $
     276    </pre>
     277  </li>
     278  <li>Connect to port 8080 using curl. Systemd will accept the
     279    connection and launch twistd, passing it the listening socket.
     280    <pre class="shell">
     281      $ curl http://localhost:8080
     282      $ sudo netstat -lntp | grep 8080
     283      tcp        0      0 127.0.0.1:8080              0.0.0.0:*                   LISTEN      1/init
     284      $ ps -ef | grep twistd
     285      richard   5831     1  1 01:29 ?        00:00:00 python /home/richard/projects/Twisted/trunk/bin/twistd --nodaemon web --port systemd:domain=INET:index=0 --path .
     286      richard   5836  4237  0 01:29 pts/5    00:00:00 grep --color=auto twistd
     287      $
     288    </pre>
     289    <pre class="shell">
     290      $ sudo tail -f /var/log/messages
     291      Apr  7 01:29:06 zorin twistd[5831]: 2012-04-07 01:29:06+0100 [-] Log opened.
     292      Apr  7 01:29:06 zorin twistd[5831]: 2012-04-07 01:29:06+0100 [-] twistd 12.0.0+r34099 (/usr/bin/python 2.7.2) starting up.
     293      Apr  7 01:29:06 zorin twistd[5831]: 2012-04-07 01:29:06+0100 [-] reactor class: twisted.internet.pollreactor.PollReactor.
     294      Apr  7 01:29:06 zorin twistd[5831]: 2012-04-07 01:29:06+0100 [-] Site starting on 8080
     295      Apr  7 01:29:06 zorin twistd[5831]: 2012-04-07 01:29:06+0100 [-] Starting factory &lt;<span class="function-name">twisted.web.server.Site</span> instance at 0x1dac7e8&gt;
     296      Apr  7 01:29:06 zorin twistd[5831]: 2012-04-07 01:29:06+0100 [HTTPChannel,0,127.0.0.1] 127.0.0.1 - - [07/Apr/2012:00:29:06 +0000] "GET / HTTP/1.1" 200 - "-" "curl/7.21.7 (x86_64-redhat-linux-gnu) libcurl/7.21.7 NSS/3.13.3.0 zlib/1.2.5 libidn/1.22 libssh2/1.2.7"
     297    </pre>
     298  </li>
     299  <li>Kill the twistd process. Systemd will log a warning when the
     300  twistd process is killed but will continue listening on port 8080.
     301  It will immediately re-launch twistd when another client connects to
     302  that port.
     303
     304    <pre class="shell">
     305      $ kill -9 5831
     306    </pre>
     307
     308    <pre class="shell">
     309      $ sudo tail -f /var/log/messages
     310      Apr  7 01:39:28 zorin systemd[1]: twistedweb.service: main process exited, code=killed, status=9
     311      Apr  7 01:39:28 zorin systemd[1]: Unit twistedweb.service entered failed state.
     312    </pre>
     313  </li>
     314</ol>
    239315</body>
    240316</html>