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