save queued task on shutdown, add getcover task
This commit is contained in:
parent
813e3b591b
commit
acd64d3186
2 changed files with 87 additions and 37 deletions
|
@ -450,6 +450,8 @@ class Item(db.Model):
|
||||||
def update_icons(self):
|
def update_icons(self):
|
||||||
if state.online:
|
if state.online:
|
||||||
self.update_cover()
|
self.update_cover()
|
||||||
|
else:
|
||||||
|
state.tasks.queue('getcover', self.id)
|
||||||
self.update_preview()
|
self.update_preview()
|
||||||
|
|
||||||
def load_metadata(self):
|
def load_metadata(self):
|
||||||
|
@ -823,12 +825,28 @@ def update_sort_table():
|
||||||
s.commit()
|
s.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def get_cover(id):
|
||||||
|
delay = 60
|
||||||
|
if state.online:
|
||||||
|
#logger.debug('get_cover(%s)', id)
|
||||||
|
with db.session():
|
||||||
|
i = Item.get(id)
|
||||||
|
if i:
|
||||||
|
i.update_cover()
|
||||||
|
else:
|
||||||
|
state.main.call_later(delay, lambda: state.tasks.queue('getcover', id))
|
||||||
|
|
||||||
|
|
||||||
def get_preview(id):
|
def get_preview(id):
|
||||||
|
delay = 60
|
||||||
|
if state.online:
|
||||||
#logger.debug('get_preview(%s)', id)
|
#logger.debug('get_preview(%s)', id)
|
||||||
with db.session():
|
with db.session():
|
||||||
i = Item.get(id)
|
i = Item.get(id)
|
||||||
if i:
|
if i:
|
||||||
i.get_preview()
|
i.get_preview()
|
||||||
|
else:
|
||||||
|
state.main.call_later(delay, lambda: state.tasks.queue('getpreview', id))
|
||||||
|
|
||||||
def sync_metadata(ids=None):
|
def sync_metadata(ids=None):
|
||||||
#logger.debug('sync_metadata(%s)', len(ids) if len(ids) > 10 else ids)
|
#logger.debug('sync_metadata(%s)', len(ids) if len(ids) > 10 else ids)
|
||||||
|
|
44
oml/tasks.py
44
oml/tasks.py
|
@ -1,31 +1,40 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# vi:si:et:sw=4:sts=4:ts=4
|
# vi:si:et:sw=4:sts=4:ts=4
|
||||||
|
import os
|
||||||
|
import json
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
|
||||||
from websocket import trigger_event
|
from websocket import trigger_event
|
||||||
import state
|
import state
|
||||||
|
import settings
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class Tasks(Thread):
|
class Tasks(Thread):
|
||||||
|
_tasks = []
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self._taskspath = os.path.join(settings.data_path, 'tasks.json')
|
||||||
self.q = Queue()
|
self.q = Queue()
|
||||||
Thread.__init__(self)
|
Thread.__init__(self)
|
||||||
self.daemon = True
|
self.daemon = True
|
||||||
self.start()
|
self.start()
|
||||||
self.queue('scan')
|
self.queue('scan')
|
||||||
|
self.load_tasks()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
import item.scan
|
import item.scan
|
||||||
from item.models import sync_metadata, get_preview
|
from item.models import sync_metadata, get_preview, get_cover
|
||||||
from user.models import export_list, update_user_peering
|
from user.models import export_list, update_user_peering
|
||||||
while not state.shutdown:
|
shutdown = False
|
||||||
|
while not shutdown:
|
||||||
m = self.q.get()
|
m = self.q.get()
|
||||||
if m and not state.shutdown:
|
if m:
|
||||||
|
if state.shutdown:
|
||||||
|
self._tasks.append(m)
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
action, data = m
|
action, data = m
|
||||||
logger.debug('%s start', action)
|
logger.debug('%s start', action)
|
||||||
|
@ -33,6 +42,8 @@ class Tasks(Thread):
|
||||||
item.scan.change_path(data[0], data[1])
|
item.scan.change_path(data[0], data[1])
|
||||||
elif action == 'export':
|
elif action == 'export':
|
||||||
export_list(data)
|
export_list(data)
|
||||||
|
elif action == 'getcover':
|
||||||
|
get_cover(data)
|
||||||
elif action == 'getpreview':
|
elif action == 'getpreview':
|
||||||
get_preview(data)
|
get_preview(data)
|
||||||
elif action == 'import':
|
elif action == 'import':
|
||||||
|
@ -52,14 +63,35 @@ class Tasks(Thread):
|
||||||
logger.debug('%s done', action)
|
logger.debug('%s done', action)
|
||||||
except:
|
except:
|
||||||
logger.debug('task failed', exc_info=True)
|
logger.debug('task failed', exc_info=True)
|
||||||
|
else:
|
||||||
|
shutdown = True
|
||||||
self.q.task_done()
|
self.q.task_done()
|
||||||
|
|
||||||
|
def load_tasks(self):
|
||||||
|
if os.path.exists(self._taskspath):
|
||||||
|
try:
|
||||||
|
with open(self._taskspath) as f:
|
||||||
|
tasks = json.load(f)
|
||||||
|
for task in tasks:
|
||||||
|
self.q.put(task)
|
||||||
|
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))
|
||||||
|
with open(self._taskspath, 'w') as f:
|
||||||
|
json.dump(self._tasks, f)
|
||||||
|
|
||||||
def join(self):
|
def join(self):
|
||||||
self.q.put(None)
|
self.q.put(None)
|
||||||
return Thread.join(self)
|
r = Thread.join(self)
|
||||||
|
self.save_tasks()
|
||||||
|
return r
|
||||||
|
|
||||||
def queue(self, action, data=None):
|
def queue(self, action, data=None):
|
||||||
if not state.shutdown:
|
if not state.shutdown:
|
||||||
logger.debug('%s queued', action)
|
logger.debug('%s queued', action)
|
||||||
self.q.put((action, data))
|
self.q.put((action, data))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue