From 7baf1584ee1dc3b1bc4c257896fa73c3fea005e6 Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Sat, 24 Sep 2011 09:43:38 +0100
Subject: [PATCH] fixes to the linux inotify thread; add signal handlers for
 clean exit

---
 client/djrandom_client/client.py        | 9 +++++++++
 client/djrandom_client/linux_watcher.py | 9 +++++++--
 client/djrandom_client/upload.py        | 4 ++++
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/client/djrandom_client/client.py b/client/djrandom_client/client.py
index f10a810..9a62592 100644
--- a/client/djrandom_client/client.py
+++ b/client/djrandom_client/client.py
@@ -60,6 +60,15 @@ def run_client(server_url, music_dir, api_key, run_once, bwlimit):
         else:
             log.warn('inotify/fsevents support not enabled on this platform')
 
+        def _cleanup(signum, frame):
+            if enable_watcher:
+                log.info('stopping watcher...')
+                wtch.stop()
+            log.info('got signal %d, exiting...' % signum)
+            upl.stop()
+        signal.signal(signal.SIGINT, _cleanup)
+        signal.signal(signal.SIGTERM, _cleanup)
+
     upl.run()
 
 
diff --git a/client/djrandom_client/linux_watcher.py b/client/djrandom_client/linux_watcher.py
index b3f59d4..797e1cf 100644
--- a/client/djrandom_client/linux_watcher.py
+++ b/client/djrandom_client/linux_watcher.py
@@ -4,16 +4,21 @@ import pyinotify
 log = logging.getLogger(__name__)
 
 
-class Watcher(object):
+class Watcher(pyinotify.ProcessEvent):
 
     def __init__(self, base, queue):
+        self.queue = queue
         self.wm = pyinotify.WatchManager()
-        self.wm.add_watch('/tmp', pyinotify.IN_CLOSE_WRITE, rec=True)
         self.notifier = pyinotify.ThreadedNotifier(self.wm, self)
         self.notifier.setDaemon(True)
         self.notifier.start()
+        self.wm.add_watch(base, pyinotify.IN_CLOSE_WRITE, rec=True)
+
+    def stop(self):
+        self.notifier.stop()
 
     def process_IN_CLOSE(self, event):
         if event.pathname.lower().endswith('.mp3'):
             log.debug('event in %s: %x' % (event.pathname, event.mask))
             self.queue.put(event.pathname)
+
diff --git a/client/djrandom_client/upload.py b/client/djrandom_client/upload.py
index 412643d..2066716 100644
--- a/client/djrandom_client/upload.py
+++ b/client/djrandom_client/upload.py
@@ -99,3 +99,7 @@ class Uploader(object):
         finally:
             log.debug('uploader thread exiting')
             self.db.close()
+
+    def stop(self):
+        self.queue.put(None)
+
-- 
GitLab