Commits

Eric Larson  committed 6934815 Draft

Added a ServiceStarter plugin in case someone would rather use the watcher processes from sass/less (or start other services like databases).

  • Participants
  • Parent commits a766a29

Comments (0)

Files changed (1)

File autorebuild.py

 """
 import os
 import glob
+import subprocess
+import shlex
 
 from cherrypy.process.plugins import Autoreloader
+from cherrypy.process.plugins import SimplePlugin
 
 
 class AutoRebuild(Autoreloader):
             task()
 
 
+class ServiceStarter(SimplePlugin):
+    """
+    A simple service starting plugin that allows you to configure
+    other processes to start/stop along side your main cherrypy
+    process.
+
+    To use: ::
+
+      # subscribe the plugin
+      cherrypy.engine.some_proc = ServiceStarter(cherrypy.engine)
+      cherrypy.engine.some_proc.subscribe()
+
+      # configure it
+      import suprocess
+
+      cherrypy.config.update({
+          'engine.some_proc.cmd': 'my_service -p 9000 -d $dbstore',
+          'engine.some_proc.kw': {'stderr': subprocess.PIPE,
+                                  'stdout': open('/var/log/some_proc.log'),
+                                  'shell': True,}
+      })
+
+    You can set the 'args' and/or 'kw' attributes on the
+    ServiceStarter object. They will be passed directly into the
+    subprocess.Popen constructor.
+    """
+
+    def __init__(self, *args, **kw):
+        super(ServiceStarter, self).__init__(*args, **kw)
+        self.proc = None
+        self.cmd = None
+        self.args = []
+        self.kw = {}
+
+    def start(self):
+        if self.cmd:
+            if isinstance(self.cmd, basestring):
+                self.cmd = shlex.split(self.cmd)
+            self.proc = subprocess.Popen(self.cmd,
+                                         *self.args,
+                                         **self.kw)
+
+    def stop(self):
+        if self.proc and self.proc.poll():
+            self.proc.terminate()
+            self.proc.wait()
+
+
 if __name__ == '__main__':
     import cherrypy
     from subprocess import call
     def build_css():
         call(['make', 'css'])
 
+    cherrypy.engine.tail_proc = ServiceStarter(cherrypy.engine)
+    cherrypy.engine.tail_proc.subscribe()
+
     cherrypy.engine.autorebuild = AutoRebuild(cherrypy.engine)
     cherrypy.engine.autorebuild.subscribe()
 
     cherrypy.config.update({
+            'engine.tail_proc.cmd': 'python -m SimpleHTTPServer 9000',
             'engine.autorebuild.on': True,
             'engine.autorebuild.taskmap': {'*.less': build_css}})