##Just handles Server Start/Stop. Nor further functionality implemented here (Olive 22.05.2017)

from hotfolder_modules.util import handle_exception
from hotfolder_modules.manager import HotfolderManager, HotfolderManagerStop

def easydb_server_start(easydb_context):
    easydb_context.register_callback('process', {'name': 'hotfolder-manager'})

def run(easydb_context):
    global manager
    try:
        manager = HotfolderManager(easydb_context)
        manager.start()
        manager.join()
        # FIXME: this works with stop(), but it takes all the CPU
        # while manager.isAlive():
        #     pass
    except HotfolderManagerStop as e:
        return
    except Exception as e:
        logger = easydb_context.get_logger('pf.plugin.base.hotfolder')
        raise handle_exception(e, logger)
    if manager.exception is not None:
        raise manager.exception

def stop(easydb_context):
    global manager
    manager.stop()
