Commit 174d0868 authored by Gerion Entrup's avatar Gerion Entrup
Browse files

collector: add ability to not run as a thread

parent 77a7e6a1
......@@ -26,26 +26,35 @@ class Collector(threading.Thread):
Collects Tags and write them to the database.
"""
def __init__(self, session_fac):
super().__init__(target=self)
self._session = session_fac()
self.log = logging.getLogger('collector')
def __init__(self, session_fac, threaded=True):
if threaded:
super().__init__(target=self)
self._session = session_fac()
self._logger = logging.getLogger('collector')
@classmethod
def get_unthreaded_instance(cls, session, logger):
instance = cls(None, threaded=False)
instance._session = session
instance._logger = logger
def run(self):
while True:
action, data = Paths.get()
if action == NEW:
self.log.info("Adding file {} to the database.".format(data.path))
self._logger.info("Adding file {} to the database.".format(data.path))
self.fetch_recording(data.mbid, data.path)
self._session.commit()
elif action == DEFECT:
self.log.info("Repair broken entry with mbid {}".format(data))
self._logger.info("Repair broken entry with mbid {}".format(data))
#TODO
elif action == UPDATE:
self.log.info("Update outdated entry with mbid {}".format(data))
self._logger.info("Update outdated entry with mbid {}".format(data))
#TODO
else:
self.log.error("Unknow action: {}".format(action))
self._logger.error("Unknow action: {}".format(action))
Paths.task_done()
self._session.close()
......@@ -150,7 +159,7 @@ class Collector(threading.Thread):
self._session.commit()
except musicbrainzngs.WebServiceError as exc:
self._session.rollback()
self.log.error("Could not connect to Musicbrainz. Path: {} Request: {}".format(path, exc))
self._logger.error("Could not connect to Musicbrainz. Path: {} Request: {}".format(path, exc))
recording = None
return recording
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment