Permalink
Browse files

Refactored application start up and shutdown to account for cleanly k…

…illing of worker threads. Started work on an importer thread that currently does nothing
  • Loading branch information...
1 parent 6a5ea42 commit 238d4f26faa12cc0b11e29d600b5035d80578916 Jonathan Fritz committed Sep 26, 2012
Showing with 59 additions and 9 deletions.
  1. +36 −3 musik.py
  2. 0 musik/library/__init__.py
  3. +15 −0 musik/library/importer.py
  4. +8 −6 musik/web/application.py
View
@@ -1,7 +1,40 @@
-
import musik.web.application
+import musik.library.importer
+import signal
+import cherrypy
+
+importThread = None
+
+# cleans up and safely stops the application
+def cleanup(signum=None, frame=None):
+ if type(signum) == type(None):
+ pass
+ else:
+ print "Signal %i caught, saving and exiting..." % int(signum)
+
+ print "Stopping worker threads"
+ if importThread != None:
+ importThread.stop()
+ importThread.join(5)
+ if importThread.isAlive():
+ print "Failed to clean up importThread"
+
+ print "Stopping CherryPy Engine"
+ cherrypy.engine.exit()
+
+ print "Clean up complete"
+
# application entry - starts the database connection and dev server
if __name__ == '__main__':
- #TODO: start background processing threads
- self.app = musik.web.application.MusikWebApplication()
+ print "Registering for shutdown signals"
+ signal.signal(signal.SIGINT, cleanup)
+ signal.signal(signal.SIGTERM, cleanup)
+
+ print "Starting worker threads"
+ importThread = musik.library.importer.ImportThread(name="ImportThread")
+ importThread.start()
+
+ # this is a blocking call
+ print "Starting Web App"
+ musik.web.application.MusikWebApplication()
No changes.
View
@@ -0,0 +1,15 @@
+import threading
+import time
+
+class ImportThread(threading.Thread):
+
+ running = True
+
+ def run(self):
+ while self.running:
+ print "still here"
+ time.sleep(1)
+
+ def stop(self):
+ print "%s.stop has been called" % self.getName()
+ self.running = False
View
@@ -70,10 +70,12 @@ def index(self):
def importmedia(self):
return templates.get_template('importmedia.html').render()
+
+# starts the web app. this call will block until the server goes down
class MusikWebApplication:
- def __init__(self):
- SAEnginePlugin(cherrypy.engine).subscribe()
- cherrypy.tools.db = SATool()
- cherrypy.tree.mount(Musik(), '/', {'/': {'tools.db.on': True}})
- cherrypy.engine.start()
- cherrypy.engine.block()
+ def __init__(self):
+ SAEnginePlugin(cherrypy.engine).subscribe()
+ cherrypy.tools.db = SATool()
+ cherrypy.tree.mount(Musik(), '/', {'/': {'tools.db.on': True}})
+ cherrypy.engine.start()
+ cherrypy.engine.block()

0 comments on commit 238d4f2

Please sign in to comment.