Swoole-src: Support unit systemd.socket

Created on 15 Mar 2016  路  9Comments  路  Source: swoole/swoole-src

Hello

Very need implement in swoole_server acept systemd.socket

https://www.freedesktop.org/software/systemd/man/systemd.socket.html#

Thank

enhancement

Most helpful comment

This does not require swoole extension to support.

/etc/systemd/system/echo.service

[Unit]
Description=Echo Http Server
After=network.target
After=syslog.target

[Service]
Type=forking
PIDFile=/opt/servers/echo/server.pid
ExecStart=/home/htf/bin/php /opt/servers/echo/server.php
ExecStop=/bin/kill $MAINPID
ExecReload=/bin/kill -USR1 $MAINPID
Restart=always

[Install]
WantedBy=multi-user.target graphical.target

Need to restart the systemd daemon, sudo systemctl --system daemon-reload

/opt/servers/echo/server.php

$http = new swoole_http_server("0.0.0.0", 9501);

$http->set(['daemonize' => true,
        'pid_file' => __DIR__.'/server.pid',
]);

$http->on('request', function ($request, $response) {
    $response->header("Content-Type", "text/html; charset=utf-8");
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});

$http->start();

start/reload/stop

sudo systemctl start echo.service
sudo systemctl reload echo.service
sudo systemctl stop echo.service

All 9 comments

Maybe such an API

new swoole_server($_SERVER['LISTEN_FDS'], $_SERVER['LISTEN_PID'], SWOOLE_PROCESS,  SWOOLE_SOCK_SYSTEMD);

This will make, fast activation and reload demon save all open connections.

swoole.socket

[Socket]
Backlog       = 1024
ListenStream  = /run/www/swoole.sock

[Install]
WantedBy = sockets.target

swoole.service

[Service]
ExecStart = /var/www/swoole/server.php

[Install]
WantedBy = multi-user.target

Get env params in server.php:
$_SERVER['LISTEN_FDS'] // 1 - file descriptor
$_SERVER['LISTEN_PID']; // current pid

But how to work with them and open the connection, I do not know :)

Here's how to do Node.js
https://www.npmjs.com/package/systemd

This does not require swoole extension to support.

/etc/systemd/system/echo.service

[Unit]
Description=Echo Http Server
After=network.target
After=syslog.target

[Service]
Type=forking
PIDFile=/opt/servers/echo/server.pid
ExecStart=/home/htf/bin/php /opt/servers/echo/server.php
ExecStop=/bin/kill $MAINPID
ExecReload=/bin/kill -USR1 $MAINPID
Restart=always

[Install]
WantedBy=multi-user.target graphical.target

Need to restart the systemd daemon, sudo systemctl --system daemon-reload

/opt/servers/echo/server.php

$http = new swoole_http_server("0.0.0.0", 9501);

$http->set(['daemonize' => true,
        'pid_file' => __DIR__.'/server.pid',
]);

$http->on('request', function ($request, $response) {
    $response->header("Content-Type", "text/html; charset=utf-8");
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});

$http->start();

start/reload/stop

sudo systemctl start echo.service
sudo systemctl reload echo.service
sudo systemctl stop echo.service

No, I'm talking about the activation on socket, you have written how to create a service that is started manually.
Here is an example of your server with activation on sockets, it will not work in Swoole

/etc/systemd/system/echo.socket

[Socket]
ListenStream = 9501

https://www.freedesktop.org/software/systemd/man/systemd.socket.html#

Has been supported.

$http = new swoole_http_server("systemd");

file_put_contents("/tmp/s.txt", var_export($http->ports, true));

$http->set([
    'daemonize' => true,
    'pid_file' => __DIR__.'/server.pid',
]);

$http->on('request', function ($request, $response) {
    $response->header("Content-Type", "text/html; charset=utf-8");
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});

$http->start();

Thank!

@matyhtf @ParkFramework
Does this technique allow Swoole to listen to a Unix socket instead of the TCP port?
Is it as simple as configuring ListenStream=/var/run/swoole.sock?

is this undocumented feature still supported as of swoole 4.5?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

erDong01 picture erDong01  路  3Comments

morozovsk picture morozovsk  路  3Comments

Gemorroj picture Gemorroj  路  3Comments

andreybolonin picture andreybolonin  路  4Comments

sshymko picture sshymko  路  3Comments