2014-05-26 11:21:19 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
|
2014-09-02 22:32:44 +00:00
|
|
|
from queue import Queue
|
2014-05-26 11:21:19 +00:00
|
|
|
from threading import Thread
|
|
|
|
|
|
|
|
from websocket import trigger_event
|
2016-01-31 16:45:14 +00:00
|
|
|
import state
|
2014-05-26 11:21:19 +00:00
|
|
|
|
|
|
|
import logging
|
2015-11-29 14:56:38 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-05-26 11:21:19 +00:00
|
|
|
|
|
|
|
class Tasks(Thread):
|
|
|
|
|
2014-08-09 16:33:59 +00:00
|
|
|
def __init__(self):
|
2014-05-26 11:21:19 +00:00
|
|
|
self.q = Queue()
|
|
|
|
Thread.__init__(self)
|
|
|
|
self.daemon = True
|
|
|
|
self.start()
|
2014-05-28 15:36:26 +00:00
|
|
|
self.queue('scan')
|
2014-05-26 11:21:19 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
import item.scan
|
2016-01-24 07:44:43 +00:00
|
|
|
from item.models import sync_metadata, get_preview
|
2016-01-14 12:22:39 +00:00
|
|
|
from user.models import export_list, update_user_peering
|
2016-01-31 16:45:14 +00:00
|
|
|
while not state.shutdown:
|
2014-05-26 11:21:19 +00:00
|
|
|
m = self.q.get()
|
2016-01-31 16:45:14 +00:00
|
|
|
if m and not state.shutdown:
|
2015-11-16 15:52:14 +00:00
|
|
|
try:
|
|
|
|
action, data = m
|
2016-01-23 16:49:34 +00:00
|
|
|
logger.debug('%s start', action)
|
2016-01-27 06:28:11 +00:00
|
|
|
if action == 'changelibrarypath':
|
|
|
|
item.scan.change_path(data[0], data[1])
|
|
|
|
elif action == 'export':
|
2016-01-24 07:44:43 +00:00
|
|
|
export_list(data)
|
|
|
|
elif action == 'getpreview':
|
|
|
|
get_preview(data)
|
2015-11-16 15:52:14 +00:00
|
|
|
elif action == 'import':
|
|
|
|
item.scan.run_import(data)
|
2016-01-24 07:44:43 +00:00
|
|
|
elif action == 'peering':
|
|
|
|
update_user_peering(*data)
|
|
|
|
elif action == 'ping':
|
|
|
|
trigger_event('pong', data)
|
2015-11-16 15:52:14 +00:00
|
|
|
elif action == 'scan':
|
|
|
|
item.scan.run_scan()
|
2016-01-18 12:02:42 +00:00
|
|
|
elif action == 'scanimport':
|
|
|
|
item.scan.import_folder()
|
2016-01-19 10:05:16 +00:00
|
|
|
elif action == 'syncmetadata':
|
2016-01-24 07:44:43 +00:00
|
|
|
sync_metadata(data)
|
2015-11-16 15:52:14 +00:00
|
|
|
else:
|
|
|
|
trigger_event('error', {'error': 'unknown action'})
|
2016-01-23 16:49:34 +00:00
|
|
|
logger.debug('%s done', action)
|
2015-11-16 15:52:14 +00:00
|
|
|
except:
|
2016-01-24 09:13:03 +00:00
|
|
|
logger.debug('task failed', exc_info=True)
|
2014-05-26 11:21:19 +00:00
|
|
|
self.q.task_done()
|
|
|
|
|
|
|
|
def join(self):
|
2014-08-09 18:32:41 +00:00
|
|
|
self.q.put(None)
|
2014-05-26 11:21:19 +00:00
|
|
|
return Thread.join(self)
|
|
|
|
|
2014-05-28 15:36:26 +00:00
|
|
|
def queue(self, action, data=None):
|
2016-01-31 16:45:14 +00:00
|
|
|
if not state.shutdown:
|
2016-01-31 13:22:30 +00:00
|
|
|
logger.debug('%s queued', action)
|
|
|
|
self.q.put((action, data))
|
2014-05-26 11:21:19 +00:00
|
|
|
|