Skip to content

Systemd socket activation #1458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 25, 2016
Merged

Systemd socket activation #1458

merged 4 commits into from
Jul 25, 2016

Conversation

xaiki
Copy link
Contributor

@xaiki xaiki commented Jul 21, 2016

This tiny patch implements systemd's socket activation mechanism[0] for
cherrypy servers, it's based on work sponsored by Endless Computers[1] to
package kalite[2] as a flatpak[3].

Socket Activation allows to setup a system so that systemd will sit on a
port and start services 'on demand' (a little bit like inetd and xinetd
used to do). This patch makes that mechanism work for any cherrypy
service without modification.

[0] http://0pointer.de/blog/projects/socket-activation.html
[1] https://endlessm.com
[2] https://learningequality.org/ka-lite/
[3] https://flatpak.org

https://phabricator.endlessm.com/T4489

Signed-off-by: Niv Sardi xaiki@evilgiggle.com

@@ -1939,7 +1939,11 @@ def start(self):
self.software = "%s Server" % self.version

# Select the appropriate socket
if isinstance(self.bind_addr, six.string_types):
self.socket = None
if os.environ.get('LISTEN_PID', None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why set default value to None? It's not necessary.
Moreover, os.getenv('LISTEN_PID') would be more verbose.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@xaiki
Copy link
Contributor Author

xaiki commented Jul 22, 2016

@webknjaz thanks for the review, i fixed your comments and pushed !

@webknjaz
Copy link
Member

LGTM, but there's some issue w/ TravisCI. Could you plz try restarting builds?
I think we can now ping @jaraco and see if he would merge this.

@jaraco
Copy link
Member

jaraco commented Jul 22, 2016

I tried restarting the builds, but the result is the same. Why would the tests not run on Travis with this patch?

@webknjaz
Copy link
Member

@jaraco I think, there's some issue with TravisCI itself. Look at the pip install output. It differs for different jobs. Did you mention this?
I have a strong feeling that it gets cut/halted at some point.

@webknjaz
Copy link
Member

@jaraco I've found the same issue, which happened in the past: https://travis-ci.org/cherrypy/cherrypy/jobs/139925725

@webknjaz
Copy link
Member

Okay.. I've tried running nosetests -s -v myself and I also get return code 70. So the issue is not in Travis

@webknjaz
Copy link
Member

webknjaz commented Jul 22, 2016

$ .tox/py27/bin/nosetests -s -v
nose.config: INFO: Set working dir to ~/src/cherrypy-xaiki/cherrypy
nose.config: INFO: Working directory ~/src/cherrypy-xaiki/cherrypy is a package; adding to sys.path
nose.config: INFO: Ignoring files matching ['^\\.', '^_', '^setup\\.py$']
nose.selector: INFO: ~/src/cherrypy-xaiki/cherrypy/cherryd is executable; skipped
nose.selector: INFO: ~/src/cherrypy-xaiki/cherrypy/daemon.py is executable; skipped
nose.selector: INFO: ~/src/cherrypy-xaiki/cherrypy/test/sessiondemo.py is executable; skipped
nose.selector: INFO: ~/src/cherrypy-xaiki/cherrypy/test/test_session.py is executable; skipped
$ echo $?
70

@webknjaz
Copy link
Member

It looks there's something malformed in this patch. Even if I generate diff and apply it to working master it breaks nose command.

This tiny patch implements systemd's socket activation mechanism[0] for
cherrypy servers, it's based on work sponsored by Endless Computers[1] to
package kalite[2] as a flatpak[3].

Socket Activation allows to setup a system so that systemd will sit on a
port and start services 'on demand' (a little bit like inetd and xinetd
used to do). This patch makes that mechanism work for any cherrypy
service without modification.

[0] http://0pointer.de/blog/projects/socket-activation.html
[1] https://endlessm.com
[2] https://learningequality.org/ka-lite/
[3] https://flatpak.org

https://phabricator.endlessm.com/T4489

Signed-off-by: Niv Sardi <xaiki@evilgiggle.com>
@xaiki
Copy link
Contributor Author

xaiki commented Jul 22, 2016

@webknjaz it was the removal of self.socket = None i added it back and it now unbreaks nose, i have no idea why ....

@webknjaz
Copy link
Member

@xaiki okay, seems good

Could you plz include some documentation/examples of running cherrypy app with use of this feature?

Also it would be nice to have some tests covering it.

@xaiki
Copy link
Contributor Author

xaiki commented Jul 22, 2016

a test is a bit hard as you need to have systemd setup,

we (endlessm.com) use that patch to enable kalite systemd activation, here's our system part:
https://github.com/mariospr/eos-kalite-system-helper

basically, you need to setup these files:
https://github.com/mariospr/eos-kalite-system-helper/blob/master/debian/eos-kalite-system-helper.service
https://github.com/mariospr/eos-kalite-system-helper/blob/master/debian/eos-kalite-system-helper.socket

to make it work

@webknjaz
Copy link
Member

@xaiki Could you plz describe this in docs/deploy.rst?
Ability of others to use this feature seamlessly will make this PR more valuable.
Also add some information about the feature to the docstring of start() method.

Signed-off-by: Niv Sardi <xaiki@evilgiggle.com>
@xaiki
Copy link
Contributor Author

xaiki commented Jul 25, 2016

docs added to deploy.rst
for start you mean start of the wcgiserver ? or the dameon start ? i'm not that familiar with those uses =)

Signed-off-by: Niv Sardi <xaiki@evilgiggle.com>
@xaiki
Copy link
Contributor Author

xaiki commented Jul 25, 2016

added to the bind_addr docstring as it seemed more relevant.

@webknjaz
Copy link
Member

LGTM

@jaraco I thinks this PR is ready for merge

Otherwise this will always end up killing the server in the socket-activated
scenario, as the port won't ever be occupied by cherrypy, since the socket
is handled by systemd.

Signed-off-by: Niv Sardi <xaiki@evilgiggle.com>
@xaiki
Copy link
Contributor Author

xaiki commented Jul 25, 2016

good, i had missed a patch from @mariospr just got it in the PR. thanks for a great review @webknjaz

@jaraco
Copy link
Member

jaraco commented Jul 25, 2016

Excellent. Thanks everyone for putting this together.

@jaraco jaraco merged commit 420a398 into cherrypy:master Jul 25, 2016
@jaraco
Copy link
Member

jaraco commented Jul 25, 2016

Pending release in 7.1.0.

@mariospr
Copy link
Contributor

@xaiki, @jaraco: that was fast! Thanks for accepting my patch :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants