openmedialibrary/oml/tasks.py

65 lines
2.1 KiB
Python

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from queue import Queue
from threading import Thread
from websocket import trigger_event
import logging
logger = logging.getLogger(__name__)
class Tasks(Thread):
def __init__(self):
self.q = Queue()
self.connected = True
Thread.__init__(self)
self.daemon = True
self.start()
self.queue('scan')
def run(self):
import item.scan
from item.models import sync_metadata, get_preview
from user.models import export_list, update_user_peering
while self.connected:
m = self.q.get()
if m:
try:
action, data = m
logger.debug('%s start', action)
if action == 'changelibrarypath':
item.scan.change_path(data[0], data[1])
elif action == 'export':
export_list(data)
elif action == 'getpreview':
get_preview(data)
elif action == 'import':
item.scan.run_import(data)
elif action == 'peering':
update_user_peering(*data)
elif action == 'ping':
trigger_event('pong', data)
elif action == 'scan':
item.scan.run_scan()
elif action == 'scanimport':
item.scan.import_folder()
elif action == 'syncmetadata':
sync_metadata(data)
else:
trigger_event('error', {'error': 'unknown action'})
logger.debug('%s done', action)
except:
logger.debug('task failed', exc_info=True)
self.q.task_done()
def join(self):
self.connected = False
self.q.put(None)
return Thread.join(self)
def queue(self, action, data=None):
logger.debug('%s queued', action)
self.q.put((action, data))