from datetime import datetime import sentry_sdk from sentry_sdk.utils import BadDsn import config import constants.common as consts import main_db from aggregate import aggregate_data from logger import logger from metric_data import get_metric_data from redis_db import get_redis_lock from update import update_data, update_metric_playlists try: sentry_sdk.init(dsn=config.SENTRY_DSN, environment=config.ENVIRONMENT) except BadDsn: pass def process_history_data(): """Aggregate summary data for missing playlists and update.""" full_new = aggregate_data() if config.AGGREGATE_MISSING_PLAYLISTS else {} if config.UPDATE_PLAYLIST_TABLE: for table_type in config.UPDATE_TABLE_LIST: update_metric_playlists(table_type) if config.UPDATE_AGGREGATED_DATA: update_table_list = list(config.UPDATE_TABLE_LIST) update_table_list = [i for i in update_table_list if not full_new.get(i, False)] if update_table_list: update_data(update_table_list) if config.AGGREGATE_MISSING_PLAYLISTS or config.UPDATE_AGGREGATED_DATA: latest_date = None for table_type in config.UPDATE_TABLE_LIST: metric_data = get_metric_data(table_type) metric_data.db.set_latest_date(metric_data.latest_date) if not latest_date or latest_date > metric_data.latest_date: latest_date = metric_data.latest_date if latest_date: main_db.set_date_value(consts.ApolloKey.PLAYLIST_DIGEST_LATEST_DATE, latest_date) main_db.set_value(consts.ApolloKey.PLAYLIST_DIGEST_JOB_RUN_TS, datetime.utcnow().isoformat()) def main(): lock = None try: if config.Redis.LOCK_ENABLED: lock = get_redis_lock() if not lock.acquire(blocking=False): logger.info("Can not get lock") return process_history_data() except Exception as ex: sentry_sdk.capture_exception(ex) raise finally: if config.Redis.LOCK_ENABLED and lock: lock.release() if __name__ == "__main__": main()