2011-04-25 18:49:01 +02:00
|
|
|
import time
|
2011-06-23 17:33:05 +02:00
|
|
|
import logging
|
|
|
|
import logging.config
|
2011-04-25 18:49:01 +02:00
|
|
|
import sys
|
2011-06-29 00:01:30 +02:00
|
|
|
import os
|
2011-06-27 22:56:20 +02:00
|
|
|
import signal
|
2011-04-25 18:49:01 +02:00
|
|
|
|
2011-08-05 20:40:03 +02:00
|
|
|
from api_clients import api_client as apc
|
2011-07-05 21:48:50 +02:00
|
|
|
|
2011-06-29 23:26:42 +02:00
|
|
|
from multiprocessing import Process, Queue as mpQueue
|
2011-04-21 23:37:59 +02:00
|
|
|
|
2011-07-16 01:49:35 +02:00
|
|
|
from threading import Thread
|
|
|
|
|
2011-07-05 21:48:50 +02:00
|
|
|
from pyinotify import WatchManager
|
|
|
|
|
2011-06-23 17:33:05 +02:00
|
|
|
from airtimefilemonitor.airtimenotifier import AirtimeNotifier
|
2011-07-13 23:38:11 +02:00
|
|
|
from airtimefilemonitor.mediamonitorcommon import MediaMonitorCommon
|
2011-06-23 17:33:05 +02:00
|
|
|
from airtimefilemonitor.airtimeprocessevent import AirtimeProcessEvent
|
|
|
|
from airtimefilemonitor.mediaconfig import AirtimeMediaConfig
|
2011-07-05 21:48:50 +02:00
|
|
|
from airtimefilemonitor.workerprocess import MediaMonitorWorkerProcess
|
2011-06-29 00:01:30 +02:00
|
|
|
from airtimefilemonitor.airtimemediamonitorbootstrap import AirtimeMediaMonitorBootstrap
|
2011-06-17 17:54:36 +02:00
|
|
|
|
2011-06-28 11:39:54 +02:00
|
|
|
# configure logging
|
|
|
|
try:
|
|
|
|
logging.config.fileConfig("logging.cfg")
|
|
|
|
except Exception, e:
|
|
|
|
print 'Error configuring logging: ', e
|
2011-06-29 00:01:30 +02:00
|
|
|
sys.exit(1)
|
2011-06-28 11:39:54 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger()
|
|
|
|
|
2011-07-18 21:27:22 +02:00
|
|
|
logger.info("\n\n*** Media Monitor bootup ***\n\n")
|
|
|
|
|
2011-06-28 11:39:54 +02:00
|
|
|
try:
|
2011-06-29 00:01:30 +02:00
|
|
|
config = AirtimeMediaConfig(logger)
|
2011-08-05 20:40:03 +02:00
|
|
|
api_client = apc.api_client_factory(config.cfg)
|
2011-09-16 23:51:28 +02:00
|
|
|
api_client.register_component("media-monitor")
|
2011-06-30 17:56:48 +02:00
|
|
|
|
2011-06-28 11:39:54 +02:00
|
|
|
logger.info("Setting up monitor")
|
|
|
|
response = None
|
|
|
|
while response is None:
|
2011-07-05 21:48:50 +02:00
|
|
|
response = api_client.setup_media_monitor()
|
2011-06-28 11:39:54 +02:00
|
|
|
time.sleep(5)
|
2011-06-30 17:56:48 +02:00
|
|
|
|
2011-08-05 20:40:03 +02:00
|
|
|
storage_directory = apc.encode_to(response["stor"], 'utf-8')
|
2011-08-23 02:36:16 +02:00
|
|
|
watched_dirs = apc.encode_to(response["watched_dirs"], 'utf-8')
|
2011-06-28 11:39:54 +02:00
|
|
|
logger.info("Storage Directory is: %s", storage_directory)
|
2011-07-11 18:21:30 +02:00
|
|
|
config.storage_directory = os.path.normpath(storage_directory)
|
|
|
|
config.imported_directory = os.path.normpath(storage_directory + '/imported')
|
|
|
|
config.organize_directory = os.path.normpath(storage_directory + '/organize')
|
2011-07-15 22:15:09 +02:00
|
|
|
config.recorded_directory = os.path.normpath(storage_directory + '/recorded')
|
2011-06-30 17:56:48 +02:00
|
|
|
|
2011-07-05 21:48:50 +02:00
|
|
|
multi_queue = mpQueue()
|
|
|
|
logger.info("Initializing event processor")
|
|
|
|
except Exception, e:
|
|
|
|
logger.error('Exception: %s', e)
|
|
|
|
|
|
|
|
try:
|
2011-07-13 23:38:11 +02:00
|
|
|
|
|
|
|
|
2011-07-05 21:48:50 +02:00
|
|
|
wm = WatchManager()
|
2011-10-04 23:20:22 +02:00
|
|
|
mmc = MediaMonitorCommon(config, wm=wm)
|
2011-09-27 20:49:03 +02:00
|
|
|
pe = AirtimeProcessEvent(queue=multi_queue, airtime_config=config, wm=wm, mmc=mmc, api_client=api_client)
|
2011-07-05 21:48:50 +02:00
|
|
|
|
2012-01-11 18:17:48 +01:00
|
|
|
bootstrap = AirtimeMediaMonitorBootstrap(logger, pe, api_client, mmc, wm)
|
2011-06-30 17:56:48 +02:00
|
|
|
bootstrap.scan()
|
2011-07-06 20:42:43 +02:00
|
|
|
|
2011-11-03 20:25:09 +01:00
|
|
|
notifier = AirtimeNotifier(wm, pe, read_freq=0, timeout=0, airtime_config=config, api_client=api_client, bootstrap=bootstrap, mmc=mmc)
|
2011-07-06 20:42:43 +02:00
|
|
|
notifier.coalesce_events()
|
|
|
|
|
2011-07-16 02:28:00 +02:00
|
|
|
#create 5 worker threads
|
2011-07-05 21:48:50 +02:00
|
|
|
wp = MediaMonitorWorkerProcess()
|
2011-06-30 17:56:48 +02:00
|
|
|
for i in range(5):
|
2011-08-05 00:36:40 +02:00
|
|
|
threadName = "Thread #%d" % i
|
|
|
|
t = Thread(target=wp.process_file_events, name=threadName, args=(multi_queue, notifier))
|
2011-07-16 01:49:35 +02:00
|
|
|
t.start()
|
2011-06-30 17:56:48 +02:00
|
|
|
|
2011-07-14 18:05:43 +02:00
|
|
|
wdd = notifier.watch_directory(storage_directory)
|
2011-06-28 11:39:54 +02:00
|
|
|
logger.info("Added watch to %s", storage_directory)
|
|
|
|
logger.info("wdd result %s", wdd[storage_directory])
|
2011-08-23 02:36:16 +02:00
|
|
|
|
|
|
|
for dir in watched_dirs:
|
|
|
|
wdd = notifier.watch_directory(dir)
|
|
|
|
logger.info("Added watch to %s", dir)
|
|
|
|
logger.info("wdd result %s", wdd[dir])
|
2011-06-28 11:39:54 +02:00
|
|
|
|
2011-06-29 10:46:46 +02:00
|
|
|
notifier.loop(callback=pe.notifier_loop_callback)
|
2011-06-29 00:01:30 +02:00
|
|
|
|
2011-06-28 11:39:54 +02:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
notifier.stop()
|
2011-06-29 00:01:30 +02:00
|
|
|
logger.info("Keyboard Interrupt")
|
2011-06-28 11:39:54 +02:00
|
|
|
except Exception, e:
|
2011-09-27 20:49:03 +02:00
|
|
|
import traceback
|
|
|
|
top = traceback.format_exc()
|
2011-06-28 11:39:54 +02:00
|
|
|
logger.error('Exception: %s', e)
|
2011-09-27 20:49:03 +02:00
|
|
|
logger.error("traceback: %s", top)
|