CherryPy Recipes / deployment / supervisor_ / __init__.py

# -*- coding: utf-8 -*-
""" Basic recipe to demonstrate how to integrate
CherryPy with supervisord:

http://supervisord.org/

In order to rest this recipe, install supervisord and run

 $ supervisord -c supervisord.conf
 $ supervisorctl update

Then go to http://localhost:8090/

You will see the PID of the running Python processor.

Click on the provided link that will trigger the
shutdown of the server and fake an error by exiting
with code number of 1.

After a few seconds go back to http://localhost:8090/
and see if the PID has changed (it should).

To confirm this, read the error.log as well.

You may shutdown supervisor as follow:

 $ supervisorctl shutdown
"""
import sys
import os
import time

import cherrypy

SHUTDOWN_FLAG = False
def shutdown():
    while True:
        if SHUTDOWN_FLAG:
            time.sleep(0.1)
            cherrypy.log("Aborting server...")
            cherrypy.engine.exit()
            break
    
class Root(object):
    @cherrypy.expose
    def index(self):
        return """<html>
<head />
<body>
    <p>CURRENT PROC ID: %d</p>

    <p><a href="/abort">Fake a failure</a> of the process so that supervisord restarts it...</p>

    <p>Once you have done so, simply come back to this page after a few seconds.</p>
</body>
</html>""" % os.getpid()

    @cherrypy.expose
    def abort(self):
        global SHUTDOWN_FLAG
        SHUTDOWN_FLAG = True
     
if __name__ == '__main__':
    cherrypy.config.update({'server.socket_port': 8090,
                            'engine.autoreload_on': False,
                            'log.access_file': './access.log',
                            'log.error_file': './error.log'})
    cherrypy.tree.mount(Root())
    
    t = cherrypy.engine.start_with_callback(shutdown)
    cherrypy.engine.block()
    
    cherrypy.log("Server now down. Let's quit messily...")
    sys.exit(1)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.