openmedialibrary/oml/downloads.py

96 lines
2.8 KiB
Python
Raw Normal View History

2014-05-04 19:26:43 +02:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2014-09-03 00:32:44 +02:00
2016-02-11 21:25:41 +05:30
import os
2014-05-04 19:26:43 +02:00
from threading import Thread
import time
2016-02-11 21:25:41 +05:30
from sqlitedict import SqliteDict
2014-08-09 18:14:14 +02:00
import db
2014-05-04 19:26:43 +02:00
import state
import settings
import update
2016-02-23 13:47:10 +05:30
import utils
2014-05-04 19:26:43 +02:00
import logging
2015-11-29 15:56:38 +01:00
logger = logging.getLogger(__name__)
2014-05-17 16:26:59 +02:00
2014-05-04 19:26:43 +02:00
class Downloads(Thread):
2014-08-09 18:33:59 +02:00
def __init__(self):
2014-05-04 19:26:43 +02:00
Thread.__init__(self)
self.daemon = True
self.start()
2016-02-11 21:25:41 +05:30
self._dbpath = os.path.join(settings.data_path, 'transfers.db')
self.transfers = SqliteDict(self._dbpath, tablename='transfers', autocommit=False)
2014-05-04 19:26:43 +02:00
def download_updates(self):
now = int(time.time())
if now > settings.server.get('last_update_check', 0) + 24*60*60:
settings.server['last_update_check'] = now
update.download()
state.user().export_library()
if settings.preferences.get('sendDiagnostics') and \
2016-02-27 15:50:10 +05:30
now > settings.server.get('last_send_diagnostics', 0) + 60*60 and \
state.online:
utils.send_debug()
settings.server['last_send_diagnostics'] = now
2014-05-04 19:26:43 +02:00
def download_next(self):
import item.models
self.download_updates()
2016-02-11 21:25:41 +05:30
downloads = list(self.transfers.items())
2016-04-05 01:21:43 +02:00
try:
downloads.sort(key=lambda t: t[1].get('added'))
except:
pass
2016-02-11 21:25:41 +05:30
for itemid, t in downloads:
if state.shutdown:
2015-11-18 01:27:53 +01:00
return False
2016-02-14 16:11:18 +05:30
if itemid not in self.transfers:
continue
2016-03-01 17:07:45 +05:30
f = item.models.File.get(itemid)
if f:
del self.transfers[itemid]
continue
2016-02-11 21:25:41 +05:30
if t.get('added') and t.get('progress', -1) < 1:
2017-06-03 22:50:14 +02:00
if 'users' not in t:
i = item.models.Item.get(itemid)
2017-06-03 22:50:14 +02:00
if not i:
del self.transfers[itemid]
continue
t['users'] = [u.id for u in i.users]
for uid in t['users']:
2016-02-11 21:25:41 +05:30
if state.shutdown:
return False
if state.nodes.is_online(uid):
logger.debug('DOWNLOAD %s %s', i, uid)
if state.nodes.download(uid, i):
break
2014-05-04 19:26:43 +02:00
return False
def run(self):
2016-01-14 09:57:26 +05:30
self.wait(10)
while not state.shutdown:
with db.session():
self.download_next()
2016-03-01 17:55:50 +05:30
self.wait(60)
2014-05-04 19:26:43 +02:00
def join(self):
2016-02-11 21:25:41 +05:30
self.transfers.commit()
self.transfers.close(do_log=False)
2014-05-04 19:26:43 +02:00
return Thread.join(self)
2015-03-07 21:54:07 +05:30
2016-01-14 09:57:26 +05:30
def wait_online(self):
2016-02-24 12:48:01 +05:30
while not state.online and not state.shutdown:
2016-01-14 09:57:26 +05:30
self.wait(5)
2016-02-24 12:48:01 +05:30
return not state.shutdown
2016-01-14 09:57:26 +05:30
def wait(self, timeout):
step = min(timeout, 1)
while not state.shutdown and timeout > 0:
2016-01-14 09:57:26 +05:30
time.sleep(step)
timeout -= step