openmedialibrary/oml/downloads.py

50 lines
1.3 KiB
Python
Raw Normal View History

2014-05-04 17:26:43 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division
from threading import Thread
import time
2014-05-17 14:26:59 +00:00
import logging
2014-05-04 17:26:43 +00:00
import state
2014-05-17 14:26:59 +00:00
logger = logging.getLogger('oml.downloads')
2014-05-04 17:26:43 +00:00
class Downloads(Thread):
def __init__(self, app):
self._app = app
self._running = True
Thread.__init__(self)
self.daemon = True
self.start()
def download_next(self):
import item.models
for i in item.models.Item.query.filter(
item.models.Item.transferadded!=None).filter(
2014-05-18 23:24:04 +00:00
item.models.Item.transferprogress<1).order_by(item.models.Item.transferadded):
2014-05-19 18:12:02 +00:00
for u in i.users:
if state.nodes.check_online(u.id):
logger.debug('DOWNLOAD %s %s', i, u)
r = state.nodes.download(u.id, i)
2014-05-17 14:26:59 +00:00
logger.debug('download ok? %s', r)
2014-05-04 17:26:43 +00:00
return True
return False
def run(self):
time.sleep(2)
with self._app.app_context():
while self._running:
if state.online:
self.download_next()
time.sleep(10)
else:
time.sleep(20)
def join(self):
self._running = False
self._q.put(None)
return Thread.join(self)