diff --git a/python_apps/media-monitor/MediaMonitor.py b/python_apps/media-monitor/MediaMonitor.py index c78cd2bc2..04510fe40 100644 --- a/python_apps/media-monitor/MediaMonitor.py +++ b/python_apps/media-monitor/MediaMonitor.py @@ -5,6 +5,7 @@ import logging import logging.config import sys import os +import signal from pyinotify import WatchManager, Notifier, ProcessEvent from multiprocessing import Process, Lock, Queue as mpQueue @@ -13,6 +14,14 @@ from airtimefilemonitor.airtimenotifier import AirtimeNotifier from airtimefilemonitor.airtimeprocessevent import AirtimeProcessEvent from airtimefilemonitor.mediaconfig import AirtimeMediaConfig +def handleSigTERM(signum, frame): + logger.info("Main Process Shutdown, TERM signal caught. %d") + if p is not None: + p.terminate() + + sys.exit(0) + + if __name__ == '__main__': # configure logging @@ -23,12 +32,11 @@ if __name__ == '__main__': sys.exit() logger = logging.getLogger() + p = None try: config = AirtimeMediaConfig() - logger.info("Initializing event processor") - pe = AirtimeProcessEvent(airtime_config=config) notifier = AirtimeNotifier(pe.wm, pe, read_freq=1, timeout=1, airtime_config=config) @@ -37,6 +45,8 @@ if __name__ == '__main__': p = Process(target=notifier.process_file_events, args=(pe.file_events,)) p.daemon = True p.start() + + signal.signal(signal.SIGTERM, handleSigTERM) logger.info("Setting up monitor") response = None @@ -64,5 +74,4 @@ if __name__ == '__main__': notifier.stop() except Exception, e: logger.error('Exception: %s', e) - finally: - p.terminate() + \ No newline at end of file diff --git a/python_apps/media-monitor/airtimefilemonitor/airtimenotifier.py b/python_apps/media-monitor/airtimefilemonitor/airtimenotifier.py index 976910156..2ebf90cc3 100644 --- a/python_apps/media-monitor/airtimefilemonitor/airtimenotifier.py +++ b/python_apps/media-monitor/airtimefilemonitor/airtimenotifier.py @@ -47,6 +47,12 @@ class AirtimeNotifier(Notifier): return True + """ + Messages received from RabbitMQ are handled here. These messages + instruct media-monitor of events such as a new directory being watched, + file metadata has been changed, or any other changes to the config of + media-monitor via the web UI. + """ def handle_message(self, body, message): # ACK the message to take it off the queue message.ack() @@ -139,11 +145,13 @@ class AirtimeNotifier(Notifier): self.api_client.update_media_metadata(md, mode) + #this function is run in its own process, and continuously + #checks the queue for any new file events. def process_file_events(self, queue): - + while True: event = queue.get() - self.logger.info("received event %s", event); + self.logger.info("received event %s", event) self.update_airtime(event) def walk_newly_watched_directory(self, directory):