def refetch_past_seasons(check_stop=lambda: None, bnet_client=None, now=None, skip_fetch_new=False): bnet_client = bnet_client or BnetClient() now = now or utcnow() # Wait for this date before refetching season. season_end_limit = now - timedelta( days=Season.REFETCH_PAST_MIN_DAYS_AFTER_SEASON_END) # Fetch new for a while after season close to make sure we got all ladders. Since we don't save non 200 leagues # we can still miss ladders here, but unlikely. There is no point in continuing after need_refetch_limit since they # will not be picked up in the ranking anyway. prev_season = Season.get_current_season().get_prev() need_refetch_limit = prev_season.end_time() + timedelta( days=Season.REFETCH_PAST_UNTIL_DAYS_AFTER_SEASON_END) if not skip_fetch_new and prev_season.end_time( ) < now <= need_refetch_limit: for region in Region.ranking_ids: fetch_new_in_region(check_stop, bnet_client, prev_season, region) # Refetch all past seasons. for season in Season.objects.filter( id__gt=14, end_date__lt=season_end_limit).order_by('-id'): refetch_past_season(season, now, check_stop, bnet_client) check_stop()
def fetch_new(region=None, check_stop=lambda: None, bnet_client=None): with transaction.atomic(): current_season = Season.get_current_season() for count in range(1, 3): check_stop() res = bnet_client.fetch_current_season(region) api_season = res.api_season if res.status == 200: update_season_cache(api_season, region, res) # Check season. if current_season.id == api_season.season_id(): # We already have latest season, continue with fetch. break elif current_season.id + 1 == api_season.season_id(): # New season detected, create new season and wait for next fetch new. create_new_season(current_season, api_season.start_date()) return elif current_season.near_start(utcnow(), days=2): logger.info( "current season %d near start, blizzard says %d, probably cached or other region" ", bailing" % (current_season.id, api_season.season_id())) return else: raise Exception( "season mismatch blizzard says %d, current in db is %d" % (api_season.season_id(), current_season.id)) else: # Is should be safe to continue after this since season id is in call to blizzard api. This is info logging # because it happens all the time due to some blizzard bug. logger.log( INFO, "could not get season info from %s after %d tries, status %s, skipping season check" % (api_season.url, count, res.status)) fetch_new_in_region(check_stop, bnet_client, current_season, region)
def fetch_new(region=None, check_stop=lambda: None, bnet_client=None): with transaction.atomic(): current_season = Season.get_current_season() for count in range(1, 6): check_stop() res = bnet_client.fetch_current_season(region) api_season = res.api_season if region == Region.SEA: if res.status != 200: logger.info( "could not get season info from %s, status %s, bailing" % (api_season.url, res.status)) return else: logger.warning("sea is alive") break if res.status == 200: break else: level = INFO if region == Region.CN else WARNING logger.log( level, "could not get season info from %s after %d tries, status %s, bailing" % (api_season.url, count, res.status)) return # Update or create the season cache, just for bookkeeping. try: cache = Cache.objects.get(region=region, type=Cache.SEASON, bid=1) cache.updated = res.fetch_time cache.data = api_season.to_text() cache.save() except Cache.DoesNotExist: Cache.objects.create(url=api_season.url, bid=1, type=Cache.SEASON, region=region, status=res.status, created=res.fetch_time, updated=res.fetch_time, data=api_season.to_text()) # Check season. if current_season.id == api_season.season_id(): # We already have latest season. pass elif current_season.id + 1 == api_season.season_id(): # New season detected, create new season. create_new_season(current_season, api_season.start_date()) return elif current_season.near_start(utcnow(), days=2): logger.info( "current season %d near start, blizzard says %d, probably cached or other region, bailing" % (current_season.id, api_season.season_id())) return else: raise Exception( "season mismatch blizzard says %d, current in db is %d" % (api_season.season_id(), current_season.id)) fetch_new_in_region(check_stop, bnet_client, current_season, region)