puma icon indicating copy to clipboard operation
puma copied to clipboard

Support for on-demand starting

Open hadmut opened this issue 1 year ago • 5 comments

Problem: Quite often a web/api/rest server is only need sometimes to run, and usually on small and older machines not packed with heaps of RAM it is reasonable to run only those processes that are actually needed, i.e. start them on demand only.

There is currently two common and well established methods to run servers on demand:

  1. The old fashioned way to start them with inetd / xinetd, which open a socket, wait for connections, and once a client connect, spawn a process to handle all incoming requests, and once the process terminates itself (e.g. due to timeout), again, wait and spawn if needed.

  2. The newer systemd, which virtually does the same, but supports two different methods of passing the socket to the server application, which is a) the traditional inetd method and b) the newer way sd_listen_fds(3), see man systemd.socket and man 3 sd_listen_fds

Would be great if it was possible to have small web servers or REST APIs, that are needed occasionally only, be able to be started on demand through systemd.

Describe alternatives you've considered Start the server through systemd permanently (as a system task) or once the user is logged in (as a user unit). Means the server is running always, even if not needed.

hadmut avatar May 16 '24 10:05 hadmut

Is there anything needed to be done in Puma to support this?

dentarg avatar Jun 05 '24 15:06 dentarg

I wouldn't see how to start Puma/rack in its current state. If it is possible, there should be something in the docs explaining it.

hadmut avatar Jun 05 '24 15:06 hadmut

You start Puma by running puma? Or bundle exec puma

Have you seen https://github.com/puma/puma/blob/master/docs/systemd.md?

dentarg avatar Jun 05 '24 15:06 dentarg

No, didn't see this.

I recently spent some time trying to find a solution for rack applications with different servers such as puma, but for whatever reason, I did not find this, not a single hint that puma is capable of running from systemd with socket activation.

But it seems to solve my problem only partly.

If you need to pass the TLS key file with a -b option like ExecStart=/<FULLPATH>/puma -b tcp://0.0.0.0:9292 -b ssl://0.0.0.0:9293?key=key.pem&cert=cert.pem

will this still work with socket activation? The Examples seem to intermix a permanent start as a regular service with a socket activation.

hadmut avatar Jun 05 '24 16:06 hadmut

I'm not using systemd myself so I'm not sure. Maybe you can test it and report back. Maybe you can even improve the docs if there's need for that. This is open source after all :-)

dentarg avatar Jun 05 '24 18:06 dentarg

I don't think anything needs to happen in Puma to accomplish this, so I'll close the issue. Happy to be proven wrong though!

dentarg avatar Jul 02 '24 07:07 dentarg