Exemple #1
0
    def run(self):
        """ Gets one torrent from good or unknown list and checks it """

        if prctlimported:
            prctl.set_name("Tribler" + currentThread().getName())

        # request new infohash from queue, start database
        while not self.shouldquit:
            start = time()
            self.sleepEvent.clear()
            try:
                torrent = None
                try:
                    self.queueLock.acquire()
                    while True:
                        torrent = self.queue.popleft()
                        self.queueset.discard(torrent['infohash'])
                        if DEBUG:
                            print >> sys.stderr, "TorrentChecking: get value from QUEUE:", torrent
                        break
                except:
                    pass
                finally:
                    self.queueLock.release()

                if torrent:
                    self.dbDoCheck(torrent)
                else:
                    self.dbSelectTorrentToCheck(self.dbDoCheck)

                torrent = self.announceQueue.get()
                if torrent:
                    if DEBUG:
                        print >> sys.stderr, "TorrentChecking: tracker checking", torrent["info"].get("announce", ""), torrent["info"].get("announce-list", "")

                    trackerStart = time()
                    multi_announce_dict = multiTrackerChecking(torrent, self.getInfoHashesForTracker)
                    if DEBUG:
                        print >> sys.stderr, "TorrentChecking: tracker checking took ", time() - trackerStart, torrent["info"].get("announce", ""), torrent["info"].get("announce-list", "")

                    if not self.shouldquit:
                        # Modify last_check time such that the torrents in queue will be skipped if present in this multi-announce
                        with self.queueLock:
                            for tor in self.queue:
                                if tor['infohash'] in multi_announce_dict:
                                    tor['last_check'] = time()

                        # Update torrent with new status
                        self.dbUpdateTorrents(torrent, multi_announce_dict)

                self.announceQueue.task_done()

            except:  # make sure we do not crash while True loop
                print_exc()

            # schedule sleep time, only if we do not have any infohashes scheduled
            if len(self.queue) == 0 and not self.shouldquit:
                diff = time() - start
                remaining = int(self.interval - diff)
                if remaining > 0:
                    if DEBUG:
                        print >> sys.stderr, "TorrentChecking: going to sleep for", remaining
                    self.sleepEvent.wait(remaining)
    def run(self):
        """ Gets one torrent from good or unknown list and checks it """
        #request new infohash from queue
        while True:
            start = time()
            self.sleepEvent.clear()
            fromQueue = False
            didTrackerCheck = False

            try:
                try:
                    self.queueLock.acquire()

                    while True:
                        torrent = self.queue.popleft()
                        self.queueset.discard(torrent['infohash'])

                        fromQueue = True

                        diff = time() - (torrent['last_check'] or 0)
                        if diff < 1800:
                            if DEBUG:
                                print >> sys.stderr, "Torrent Checking: checking too soon:", torrent
                            continue

                        if DEBUG:
                            print >> sys.stderr, "Torrent Checking: get value from QUEUE:", torrent

                        break

                    self.queueLock.release()

                except:
                    self.queueLock.release()

                    policy = self.selectPolicy()
                    torrent = self.torrentdb.selectTorrentToCheck(
                        policy=policy)

                    if DEBUG:
                        print >> sys.stderr, "Torrent Checking: get value from DB:", torrent

                if torrent:
                    notify = []

                    if fromQueue and torrent['ignored_times'] > 0:
                        #ignoring this torrent
                        if DEBUG:
                            print >> sys.stderr, 'Torrent Checking: ignoring torrent:', torrent

                        kw = {'ignored_times': torrent['ignored_times'] - 1}

                    else:
                        # read the torrent from disk / use other sources to specify trackers
                        torrent = self.readTrackers(torrent)
                        if self.hasTrackers(torrent):
                            if DEBUG:
                                print >> sys.stderr, "Torrent Checking: tracker checking", torrent[
                                    "info"].get("announce",
                                                ""), torrent["info"].get(
                                                    "announce-list", "")
                                trackerStart = time()

                            multidict = multiTrackerChecking(
                                torrent, self.GetInfoHashesForTracker)
                            didTrackerCheck = True
                            if DEBUG:
                                print >> sys.stderr, "Torrent Checking: tracker checking took ", time(
                                ) - trackerStart, torrent["info"].get(
                                    "announce", ""), torrent["info"].get(
                                        "announce-list", "")

                            for key, values in multidict.iteritems():
                                if key != torrent['infohash']:
                                    seeder, leecher = values

                                    if seeder > 0 or leecher > 0:
                                        #store result
                                        curkw = {
                                            'seeder': seeder,
                                            'leecher': leecher,
                                            'ignored_times': 0,
                                            'last_check_time': long(time()),
                                            'status': 'good'
                                        }
                                        self.torrentdb.updateTorrent(
                                            key,
                                            commit=False,
                                            notify=False,
                                            **curkw)
                                        notify.append(key)

                                        if DEBUG:
                                            print >> sys.stderr, "Torrent Checking: new status:", curkw

                                    for tor in self.queue:
                                        if tor and tor['infohash'] == key:
                                            tor['last_check'] = time()

                        if not didTrackerCheck:
                            torrent["seeder"] = -2
                            torrent["leecher"] = -2

                        # Update torrent with new status
                        self.updateTorrentInfo(torrent)

                        # Save in DB
                        kw = {
                            'last_check_time': int(time()),
                            'seeder': torrent['seeder'],
                            'leecher': torrent['leecher'],
                            'status': torrent['status'],
                            'ignored_times': torrent['ignored_times'],
                            'retried_times': torrent['retried_times']
                        }

                    # Must come after tracker check, such that if tracker dead and DHT still alive, the
                    # status is still set to good
                    if torrent['status'] == 'dead':
                        self.mldhtchecker.lookup(torrent['infohash'])

                    if DEBUG:
                        print >> sys.stderr, "Torrent Checking: new status:", kw

                    self.torrentdb.updateTorrent(torrent['infohash'],
                                                 notify=didTrackerCheck,
                                                 **kw)

                    #notify after commit
                    for infohash in notify:
                        self.notifier.notify(NTFY_TORRENTS, NTFY_UPDATE,
                                             infohash)
            except:
                print_exc()

            # schedule sleep time, only if we do not have any infohashes scheduled
            if len(self.queue) == 0:
                diff = time() - start
                remaining = int(self.interval - diff)
                if remaining > 0:
                    self.sleepEvent.wait(remaining)
 def run(self):
     """ Gets one torrent from good or unknown list and checks it """
     #request new infohash from queue
     while True:
         start = time()
         self.sleepEvent.clear()
         fromQueue = False
         didTrackerCheck = False
         
         try:    
             try:
                 self.queueLock.acquire()
                 
                 while True:
                     torrent = self.queue.popleft()
                     self.queueset.discard(torrent['infohash'])
                 
                     fromQueue = True
                 
                     diff = time() - (torrent['last_check'] or 0)
                     if diff < 1800:
                         if DEBUG:
                             print >> sys.stderr, "Torrent Checking: checking too soon:", torrent
                         continue    
                 
                     if DEBUG:
                         print >> sys.stderr, "Torrent Checking: get value from QUEUE:", torrent
                         
                     break
                 
                 self.queueLock.release()
             
             except:
                 self.queueLock.release()
                 
                 policy = self.selectPolicy()
                 torrent = self.torrentdb.selectTorrentToCheck(policy=policy)
                 
                 if DEBUG:
                     print >> sys.stderr, "Torrent Checking: get value from DB:", torrent
                 
             if torrent:
                 notify = []
                 
                 if fromQueue and torrent['ignored_times'] > 0:
                     #ignoring this torrent
                     if DEBUG:
                         print >> sys.stderr, 'Torrent Checking: ignoring torrent:', torrent
                         
                     kw = { 'ignored_times': torrent['ignored_times'] -1 }
                     
                 else:
                     # read the torrent from disk / use other sources to specify trackers
                     torrent = self.readTrackers(torrent)
                     if self.hasTrackers(torrent):
                         if DEBUG:
                             print >> sys.stderr, "Torrent Checking: tracker checking", torrent["info"].get("announce", "") ,torrent["info"].get("announce-list", "")
                             trackerStart = time()
                                
                         multidict = multiTrackerChecking(torrent, self.GetInfoHashesForTracker)
                         didTrackerCheck = True
                         if DEBUG:
                             print >> sys.stderr, "Torrent Checking: tracker checking took ", time() - trackerStart, torrent["info"].get("announce", "") ,torrent["info"].get("announce-list", "")
                         
                         for key, values in multidict.iteritems():
                             if key != torrent['infohash']:
                                 seeder, leecher = values
                                 
                                 if seeder > 0 or leecher > 0:
                                     #store result
                                     curkw = {'seeder':seeder, 'leecher':leecher, 'ignored_times': 0, 'last_check_time': long(time()), 'status':'good'}
                                     self.torrentdb.updateTorrent(key, commit = False, notify = False, **curkw)
                                     notify.append(key)
                                     
                                     if DEBUG:
                                         print >> sys.stderr, "Torrent Checking: new status:", curkw
                                 
                                 for tor in self.queue:
                                     if tor and tor['infohash'] == key:
                                         tor['last_check'] = time()
                     
                     if not didTrackerCheck:
                         torrent["seeder"] = -2
                         torrent["leecher"] = -2
                         
                     # Update torrent with new status
                     self.updateTorrentInfo(torrent)
 
                     # Save in DB                    
                     kw = {
                         'last_check_time': int(time()),
                         'seeder': torrent['seeder'],
                         'leecher': torrent['leecher'],
                         'status': torrent['status'],
                         'ignored_times': torrent['ignored_times'],
                         'retried_times': torrent['retried_times']
                     }
                     
                 # Must come after tracker check, such that if tracker dead and DHT still alive, the
                 # status is still set to good
                 if torrent['status'] == 'dead':
                     self.mldhtchecker.lookup(torrent['infohash'])
             
                 if DEBUG:
                     print >> sys.stderr, "Torrent Checking: new status:", kw
             
                 self.torrentdb.updateTorrent(torrent['infohash'], notify = didTrackerCheck, **kw)
                 
                 #notify after commit
                 for infohash in notify:
                     self.notifier.notify(NTFY_TORRENTS, NTFY_UPDATE, infohash)
         except:
             print_exc()
                 
         # schedule sleep time, only if we do not have any infohashes scheduled
         if len(self.queue) == 0:
             diff = time() - start
             remaining = int(self.interval - diff)
             if remaining > 0:
                 self.sleepEvent.wait(remaining)