def update_ranks(time_delta): """ Update Rank entries that belong to players who have played just now or ``time_delta`` ago. Args: time_delta - time in past relative to the current time (seconds/timedelta obj) """ min_date = timezone.now() - utils.force_timedelta(time_delta) queryset = ( models.Profile.objects .popular() .select_related('game_last') .filter(game_last__date_finished__gte=min_date) ) for profile in queryset: # aggregate stats relative to the last game's date year = profile.last_seen.year period = models.Rank.get_period_for_year(year) with transaction.atomic(): # aggregate stats for the specified period stats = profile.aggregate_mode_stats(models.Profile.SET_STATS_ALL, *period) models.Rank.objects.store_many(stats, year, profile)
def query_listed_servers(time_delta, interval): """ Attempt to query listed servers every ``interval`` for a total of ``time``. Args: time_delta - execution time (seconds/timedelta obj) interval - time between query cycles (seconds/timedelta obj) """ # list of available servers servers_listed = set() # list of servers that have responded to a query servers_live = set() # enforce max number of concurrently running threads semaphore = threading.Semaphore(config.MAX_STATUS_CONNECTIONS) interval = utils.force_timedelta(interval).seconds # calculate the time the task must be stopped at stop_time = time.time() + utils.force_timedelta(time_delta).seconds # cache queryset servers = models.Server.objects.listed() while time.time() < stop_time: threads = [] for server in servers: # keep track of servers being queried servers_listed.add(server) semaphore.acquire() thread = ServerQueryThread(server=server, semaphore=semaphore, live=servers_live) threads.append(thread) # queue the thread thread.start() # block untill all threads finished [thread.join() for thread in threads] # sleep for a while time.sleep(interval) # servers that have replied at least once live_servers_detected.send(sender=None, servers=servers_live) # servers that have never replied by the end of the task dead_servers_detected.send(sender=None, servers=servers_listed - servers_live)
def query_listed_servers(time_delta, interval): """ Attempt to query listed servers every ``interval`` for a total of ``time``. Args: time_delta - execution time (seconds/timedelta obj) interval - time between query cycles (seconds/timedelta obj) """ # list of available servers servers_listed = set() # list of servers that have responded to a query servers_live = set() # enforce max number of concurrently running threads semaphore = threading.Semaphore(config.MAX_STATUS_CONNECTIONS) interval = utils.force_timedelta(interval).seconds # calculate the time the task must be stopped at stop_time = time.time() + utils.force_timedelta(time_delta).seconds # cache queryset servers = models.Server.objects.listed() while time.time() < stop_time: threads = [] for server in servers: # keep track of servers being queried servers_listed.add(server) semaphore.acquire() thread = ServerQueryThread(server=server, semaphore=semaphore, live=servers_live) threads.append(thread) # queue the thread thread.start() # block untill all threads finished [thread.join() for thread in threads] # sleep for a while time.sleep(interval) # servers that have replied at least once live_servers_detected.send(sender=None, servers=servers_live) # servers that have never replied by the end of the task dead_servers_detected.send(sender=None, servers=servers_listed-servers_live)
def update_popular(time_delta): """ Update the profile popular fields such as name, country, loadout, etc that belong to players who have played just now or ``time_delta`` ago. Args: time_delta - time in past relative to the current time (seconds/timedelta obj) """ min_date = timezone.now() - utils.force_timedelta(time_delta) queryset = (models.Profile.objects.select_for_update().select_related( 'game_last').filter(game_last__date_finished__gte=min_date)) with transaction.atomic(): # update the popular fields for profile in queryset: profile.update_popular() profile.save()
def update_popular(time_delta): """ Update the profile popular fields such as name, country, loadout, etc that belong to players who have played just now or ``time_delta`` ago. Args: time_delta - time in past relative to the current time (seconds/timedelta obj) """ min_date = timezone.now() - utils.force_timedelta(time_delta) queryset = ( models.Profile.objects .select_for_update() .select_related('game_last') .filter(game_last__date_finished__gte=min_date) ) with transaction.atomic(): # update the popular fields for profile in queryset: profile.update_popular() profile.save()
def update_ranks(time_delta): """ Update Rank entries that belong to players who have played just now or ``time_delta`` ago. Args: time_delta - time in past relative to the current time (seconds/timedelta obj) """ min_date = timezone.now() - utils.force_timedelta(time_delta) queryset = ( models.Profile.objects.popular().select_related('game_last').filter( game_last__date_finished__gte=min_date)) for profile in queryset: # aggregate stats relative to the last game's date year = profile.last_seen.year period = models.Rank.get_period_for_year(year) with transaction.atomic(): # aggregate stats for the specified period stats = profile.aggregate_mode_stats(models.Profile.SET_STATS_ALL, *period) models.Rank.objects.store_many(stats, year, profile)