2014-05-26 11:21:19 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-02-11 14:26:12 +00:00
|
|
|
from queue import PriorityQueue
|
2014-05-26 11:21:19 +00:00
|
|
|
from threading import Thread
|
2016-02-11 14:29:51 +00:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import time
|
2014-05-26 11:21:19 +00:00
|
|
|
|
|
|
|
from websocket import trigger_event
|
2016-01-31 16:45:14 +00:00
|
|
|
import state
|
2016-02-11 06:14:40 +00:00
|
|
|
import settings
|
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
|
|
|
|
2016-02-11 14:29:51 +00:00
|
|
|
DEUBG_TASKS = False
|
|
|
|
|
2016-02-29 12:50:56 +00:00
|
|
|
class QueueElement(tuple):
|
|
|
|
def __lt__(self, rhs):
|
|
|
|
return self[0] < rhs[0]
|
|
|
|
def __gt__(self, rhs):
|
|
|
|
return self[0] > rhs[0]
|
|
|
|
def __le__(self, rhs):
|
|
|
|
return self[0] <= rhs[0]
|
|
|
|
def __ge__(self, rhs):
|
|
|
|
return self[0] >= rhs[0]
|
|
|
|
|
2014-05-26 11:21:19 +00:00
|
|
|
class Tasks(Thread):
|
2016-02-11 06:14:40 +00:00
|
|
|
_tasks = []
|
2014-05-26 11:21:19 +00:00
|
|
|
|
2014-08-09 16:33:59 +00:00
|
|
|
def __init__(self):
|
2016-02-11 06:14:40 +00:00
|
|
|
self._taskspath = os.path.join(settings.data_path, 'tasks.json')
|
2016-02-11 14:26:12 +00:00
|
|
|
self.q = PriorityQueue()
|
2014-05-26 11:21:19 +00:00
|
|
|
Thread.__init__(self)
|
|
|
|
self.daemon = True
|
|
|
|
self.start()
|
|
|
|
|
|
|
|
def run(self):
|
2016-02-11 14:26:12 +00:00
|
|
|
self.load_tasks()
|
2021-02-05 16:38:17 +00:00
|
|
|
if (time.mktime(time.gmtime()) - settings.server.get('last_scan', 0)) > 24*60*60:
|
2019-02-01 11:13:05 +00:00
|
|
|
self.queue('scan')
|
|
|
|
|
2014-05-26 11:21:19 +00:00
|
|
|
import item.scan
|
2016-02-11 06:14:40 +00:00
|
|
|
from item.models import sync_metadata, get_preview, get_cover
|
2016-03-14 13:31:56 +00:00
|
|
|
from user.models import (
|
2021-01-31 15:10:50 +00:00
|
|
|
export_list, update_user_peering,
|
|
|
|
add_local_info, remove_local_info,
|
|
|
|
upload
|
2016-03-14 13:31:56 +00:00
|
|
|
)
|
2016-02-11 06:14:40 +00:00
|
|
|
shutdown = False
|
|
|
|
while not shutdown:
|
2016-02-11 14:26:12 +00:00
|
|
|
p, m = self.q.get()
|
2016-02-11 06:14:40 +00:00
|
|
|
if m:
|
|
|
|
if state.shutdown:
|
2016-02-11 14:26:12 +00:00
|
|
|
self._tasks.append((p, m))
|
2016-02-11 06:14:40 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
action, data = m
|
2016-02-11 14:29:51 +00:00
|
|
|
if DEUBG_TASKS:
|
|
|
|
t0 = time.time()
|
|
|
|
logger.debug('%s start', action)
|
|
|
|
|
2016-02-11 06:14:40 +00:00
|
|
|
if action == 'changelibrarypath':
|
|
|
|
item.scan.change_path(data[0], data[1])
|
|
|
|
elif action == 'export':
|
|
|
|
export_list(data)
|
|
|
|
elif action == 'getcover':
|
|
|
|
get_cover(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)
|
2016-03-14 13:31:56 +00:00
|
|
|
elif action == 'addlocalinfo':
|
|
|
|
add_local_info(data)
|
|
|
|
elif action == 'removelocalinfo':
|
|
|
|
remove_local_info(data)
|
2016-02-11 06:14:40 +00:00
|
|
|
elif action == 'scan':
|
|
|
|
item.scan.run_scan()
|
|
|
|
elif action == 'scanimport':
|
|
|
|
item.scan.import_folder()
|
|
|
|
elif action == 'syncmetadata':
|
|
|
|
sync_metadata(data)
|
2019-01-17 18:19:23 +00:00
|
|
|
elif action == 'upload':
|
|
|
|
upload(data)
|
2016-02-11 06:14:40 +00:00
|
|
|
else:
|
|
|
|
trigger_event('error', {'error': 'unknown action'})
|
2016-02-11 14:29:51 +00:00
|
|
|
if DEUBG_TASKS:
|
|
|
|
logger.debug('%s done (%s)', action, time.time()-t0)
|
2016-02-11 06:14:40 +00:00
|
|
|
except:
|
|
|
|
logger.debug('task failed', exc_info=True)
|
|
|
|
else:
|
|
|
|
shutdown = True
|
2014-05-26 11:21:19 +00:00
|
|
|
self.q.task_done()
|
|
|
|
|
2016-02-11 06:14:40 +00:00
|
|
|
def load_tasks(self):
|
|
|
|
if os.path.exists(self._taskspath):
|
2021-01-31 15:10:50 +00:00
|
|
|
logger.debug('loading tasks')
|
2016-02-11 06:14:40 +00:00
|
|
|
try:
|
|
|
|
with open(self._taskspath) as f:
|
|
|
|
tasks = json.load(f)
|
|
|
|
for task in tasks:
|
2016-02-11 15:54:32 +00:00
|
|
|
if len(task) == 2:
|
2016-02-29 12:50:56 +00:00
|
|
|
self.q.put(QueueElement((task[0], tuple(task[1]))))
|
2016-02-11 06:14:40 +00:00
|
|
|
logger.debug('loaded %s tasks', len(tasks))
|
|
|
|
except:
|
|
|
|
logger.debug('failed to load saved tasks', exc_info=True)
|
|
|
|
os.unlink(self._taskspath)
|
|
|
|
|
|
|
|
def save_tasks(self):
|
|
|
|
if self._tasks:
|
|
|
|
logger.debug('saving %s tasks for later', len(self._tasks))
|
2016-03-23 13:42:16 +00:00
|
|
|
with open(self._taskspath, 'w', encoding='utf-8') as f:
|
2016-02-11 06:14:40 +00:00
|
|
|
json.dump(self._tasks, f)
|
|
|
|
|
2014-05-26 11:21:19 +00:00
|
|
|
def join(self):
|
2016-02-29 12:50:56 +00:00
|
|
|
self.q.put(QueueElement((1000, None)))
|
2016-02-11 06:14:40 +00:00
|
|
|
r = Thread.join(self)
|
|
|
|
self.save_tasks()
|
|
|
|
return r
|
2014-05-26 11:21:19 +00:00
|
|
|
|
2016-02-11 14:26:12 +00:00
|
|
|
def queue(self, action, data=None, priority=None):
|
|
|
|
if priority is None:
|
|
|
|
priority = 100
|
|
|
|
if action in ('getcover', 'getpreview'):
|
|
|
|
priority += 1
|
2016-02-14 07:33:15 +00:00
|
|
|
if action in ('import', 'export', 'scanimport'):
|
2016-02-11 14:26:12 +00:00
|
|
|
priority -= 1
|
|
|
|
if action in ('peering', 'changelibrarypath'):
|
|
|
|
priority -= 2
|
2016-01-31 16:45:14 +00:00
|
|
|
if not state.shutdown:
|
2016-02-11 14:26:12 +00:00
|
|
|
#logger.debug('queue: %s (%s)', action, data)
|
2016-02-29 12:50:56 +00:00
|
|
|
self.q.put(QueueElement((priority, (action, data))))
|
2016-02-27 04:49:45 +00:00
|
|
|
else:
|
|
|
|
self._tasks.append((priority, (action, data)))
|