def _callback(self, processed, total): if not Transfer.find_one({ '_id': self.transfer['_id'], 'finished': None, }): logger.info('aborted sftp transfer %s to %s', self.transfer['src'], self.transfer['dst']) sys.exit(1) # to avoid zombies now = time.time() if now - self.last_callback < settings.PROGRESS_CALLBACK_DELTA: return self.last_callback = now self.transfer['transferred'] = self.transferred self.transfer['info']['name'] = self.name Transfer.save(self.transfer, safe=True)
def manage_torrents(): client = get_torrent_client() for transfer in Transfer.find({ 'started': {'$ne': None}, 'finished': None, 'type': 'torrent', }): hash = transfer['info'].get('hash') if not hash: continue try: torrent = client.get_torrent(hash=hash) except TorrentError: continue if not torrent: transfer['finished'] = datetime.utcnow() logger.debug('torrent %s is not queued', hash) else: transfer['info'] = torrent transfer['transferred'] = torrent.transferred transfer['total'] = torrent.size transfer['progress'] = torrent.progress Transfer.save(transfer, safe=True) default_dir = str(Settings.get_settings('paths')['default']) for torrent in client.iter_torrents(): transfer = Transfer.find_one({'info.hash': torrent.hash}, sort=[('created', DESCENDING)]) if not transfer or (transfer['finished'] \ and torrent.date_added > transfer['finished']): now = datetime.utcnow() Transfer.add(torrent.magnet_url, default_dir, type='torrent', added=now, started=now, info={'hash': torrent.hash}) logger.info('started torrent transfer %s to %s', torrent.magnet_url, default_dir) elif transfer['finished']: client.remove_torrent(hash=torrent.hash, delete_data=True) logger.debug('removed finished torrent "%s" (%s)', torrent.name, torrent.hash) else: manage_torrent(client, hash=torrent.hash, dst=transfer['dst'])
def process_transfer(self, id): self.transfer = Transfer.find_one({'_id': id}) if not self.transfer: return src = self.transfer['src'] dst = self.transfer['dst'] if not isinstance(src, (tuple, list)): src = [src] temp_dir = Settings.get_settings('paths')['tmp'] res = self.process(src, dst, temp_dir) if res: self.transfer['info']['files'] = res self.transfer['transferred'] = self.total self.transfer['progress'] = 100 logger.info('finished http transfer %s to %s', src, dst) else: self.transfer['started'] = None logger.error('failed to process http transfer %s to %s', src, dst) self.transfer['finished'] = datetime.utcnow() Transfer.save(self.transfer, safe=True)
def _callback(self, download_total, downloaded, upload_total, uploaded): if not Transfer.find_one({ '_id': self.transfer['_id'], 'finished': None, }): self.aborted = True return now = time.time() if now - self.last_callback < settings.PROGRESS_CALLBACK_DELTA: return self.last_callback = now transferred = downloaded or uploaded self.transferred_current = transferred transferred += self.transferred self.transfer['total'] = self.total self.transfer['transferred'] = transferred total = float(self.transfer['total']) self.transfer['progress'] = transferred * 100 / total if total else 0 self.transfer['info']['name'] = self.name Transfer.save(self.transfer, safe=True)