openmedialibrary/oml/tasks.py

62 lines
1.8 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import os
2014-09-02 22:32:44 +00:00
from queue import Queue
from threading import Thread
from websocket import trigger_event
import logging
2015-11-29 14:56:38 +00:00
logger = logging.getLogger(__name__)
class Tasks(Thread):
2014-08-09 16:33:59 +00:00
def __init__(self):
self.q = Queue()
self.connected = True
Thread.__init__(self)
self.daemon = True
self.start()
2014-05-28 15:36:26 +00:00
self.queue('scan')
def run(self):
import item.scan
2016-01-19 10:05:16 +00:00
from item.models import sync_metadata
2016-01-14 12:22:39 +00:00
from user.models import export_list, update_user_peering
while self.connected:
m = self.q.get()
if m:
2015-11-16 15:52:14 +00:00
try:
action, data = m
if action == 'ping':
trigger_event('pong', data)
elif action == 'import':
item.scan.run_import(data)
2016-01-14 08:59:11 +00:00
elif action == 'export':
2016-01-14 12:22:39 +00:00
export_list(data)
2015-11-16 15:52:14 +00:00
elif action == 'scan':
item.scan.run_scan()
elif action == 'scanimport':
item.scan.import_folder()
2016-01-14 12:22:39 +00:00
elif action == 'peering':
update_user_peering(*data)
2016-01-19 10:05:16 +00:00
elif action == 'syncmetadata':
if not data:
data = []
sync_metadata(*data)
2015-11-16 15:52:14 +00:00
else:
trigger_event('error', {'error': 'unknown action'})
except:
logger.debug('task failed', exc_info=1)
self.q.task_done()
def join(self):
self.connected = False
2014-08-09 18:32:41 +00:00
self.q.put(None)
return Thread.join(self)
2014-05-28 15:36:26 +00:00
def queue(self, action, data=None):
self.q.put((action, data))