Commits

Sylvain Hellegouarch committed 4f5823b

added basc recipe for supervisord

  • Participants
  • Parent commits 274df94

Comments (0)

Files changed (2)

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)

deployment/supervisor_/supervisord.conf

+[unix_http_server]
+file=/tmp/supervisor.sock 
+
+[supervisord]
+logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)
+logfile_maxbytes=50MB        ; (max main logfile bytes b4 rotation;default 50MB)
+logfile_backups=10           ; (num of main logfile rotation backups;default 10)
+loglevel=info                ; (log level;default info; others: debug,warn,trace)
+pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
+nodaemon=false               ; (start in foreground if true;default false)
+minfds=1024                  ; (min. avail startup file descriptors;default 1024)
+minprocs=200                 ; (min. avail process descriptors;default 200)
+
+[rpcinterface:supervisor]
+supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
+
+[supervisorctl]
+serverurl=unix:///tmp/supervisor.sock
+
+[program:myapp]
+command=python __init__.py
+environment=PYTHONPATH=.   
+directory=.