Browse Source

add try-except

liqian 2 years ago
parent
commit
4d06980fa3
3 changed files with 123 additions and 77 deletions
  1. 51 33
      region_rule_rank_h.py
  2. 36 24
      region_rule_rank_h_by24h.py
  3. 36 20
      rule_rank_h_by_24h.py

+ 51 - 33
region_rule_rank_h.py

@@ -7,6 +7,7 @@
 import multiprocessing
 import multiprocessing
 import os
 import os
 import sys
 import sys
+import traceback
 
 
 import gevent
 import gevent
 import datetime
 import datetime
@@ -16,7 +17,7 @@ from functools import reduce
 from odps import ODPS
 from odps import ODPS
 from threading import Timer, Thread
 from threading import Timer, Thread
 from utils import MysqlHelper, RedisHelper, get_data_from_odps, filter_video_status, filter_shield_video, \
 from utils import MysqlHelper, RedisHelper, get_data_from_odps, filter_video_status, filter_shield_video, \
-    check_table_partition_exits, filter_video_status_app
+    check_table_partition_exits, filter_video_status_app, send_msg_to_feishu
 from config import set_config
 from config import set_config
 from log import Log
 from log import Log
 from check_video_limit_distribute import update_limit_video_score
 from check_video_limit_distribute import update_limit_video_score
@@ -714,38 +715,55 @@ def h_rank_bottom(now_date, now_h, rule_params, region_code_list, rule_rank_h_fl
 
 
 
 
 def h_timer_check():
 def h_timer_check():
-    rule_rank_h_flag = sys.argv[1]
-    if rule_rank_h_flag == '48h':
-        rule_params = config_.RULE_PARAMS_REGION_APP_TYPE_48H
-    else:
-        rule_params = config_.RULE_PARAMS_REGION_APP_TYPE
-    project = config_.PROJECT_REGION_APP_TYPE
-    table = config_.TABLE_REGION_APP_TYPE
-    region_code_list = [code for region, code in region_code.items()]
-    now_date = datetime.datetime.today()
-    log_.info(f"now_date: {datetime.datetime.strftime(now_date, '%Y%m%d%H')}, rule_rank_h_flag: {rule_rank_h_flag}")
-    now_h = datetime.datetime.now().hour
-    now_min = datetime.datetime.now().minute
-    if now_h == 0:
-        h_rank_bottom(now_date=now_date, now_h=now_h, rule_params=rule_params, region_code_list=region_code_list,
-                      rule_rank_h_flag=rule_rank_h_flag)
-        return
-    # 查看当前小时更新的数据是否已准备好
-    h_data_count = h_data_check(project=project, table=table, now_date=now_date)
-    if h_data_count > 0:
-        log_.info(f'region_h_data_count = {h_data_count}')
-        # 数据准备好,进行更新
-        rank_by_h(now_date=now_date, now_h=now_h, rule_params=rule_params,
-                  project=project, table=table, region_code_list=region_code_list, rule_rank_h_flag=rule_rank_h_flag)
-        log_.info(f"region_h_data end!")
-    elif now_min > 50:
-        log_.info('h_recall data is None, use bottom data!')
-        h_rank_bottom(now_date=now_date, now_h=now_h, rule_params=rule_params, region_code_list=region_code_list,
-                      rule_rank_h_flag=rule_rank_h_flag)
-        log_.info(f"region_h_data end!")
-    else:
-        # 数据没准备好,1分钟后重新检查
-        Timer(60, h_timer_check).start()
+    try:
+        rule_rank_h_flag = sys.argv[1]
+        if rule_rank_h_flag == '48h':
+            rule_params = config_.RULE_PARAMS_REGION_APP_TYPE_48H
+        else:
+            rule_params = config_.RULE_PARAMS_REGION_APP_TYPE
+        project = config_.PROJECT_REGION_APP_TYPE
+        table = config_.TABLE_REGION_APP_TYPE
+        region_code_list = [code for region, code in region_code.items()]
+        now_date = datetime.datetime.today()
+        log_.info(f"now_date: {datetime.datetime.strftime(now_date, '%Y%m%d%H')}, rule_rank_h_flag: {rule_rank_h_flag}")
+        now_h = datetime.datetime.now().hour
+        now_min = datetime.datetime.now().minute
+        if now_h == 0:
+            h_rank_bottom(now_date=now_date, now_h=now_h, rule_params=rule_params, region_code_list=region_code_list,
+                          rule_rank_h_flag=rule_rank_h_flag)
+            return
+        # 查看当前小时更新的数据是否已准备好
+        h_data_count = h_data_check(project=project, table=table, now_date=now_date)
+        if h_data_count > 0:
+            log_.info(f'region_h_data_count = {h_data_count}')
+            # 数据准备好,进行更新
+            rank_by_h(now_date=now_date, now_h=now_h, rule_params=rule_params,
+                      project=project, table=table, region_code_list=region_code_list, rule_rank_h_flag=rule_rank_h_flag)
+            log_.info(f"region_h_data end!")
+        elif now_min > 50:
+            log_.info('h_recall data is None, use bottom data!')
+            h_rank_bottom(now_date=now_date, now_h=now_h, rule_params=rule_params, region_code_list=region_code_list,
+                          rule_rank_h_flag=rule_rank_h_flag)
+            log_.info(f"region_h_data end!")
+        else:
+            # 数据没准备好,1分钟后重新检查
+            Timer(60, h_timer_check).start()
+        send_msg_to_feishu(
+            webhook=config_.FEISHU_ROBOT['server_robot'].get('webhook'),
+            key_word=config_.FEISHU_ROBOT['server_robot'].get('key_word'),
+            msg_text=f"rov-offline{config_.ENV_TEXT} - 推荐视频数据更新完成\n "
+                     f"now_date: {datetime.datetime.strftime(now_date, '%Y%m%d%H')}\n"
+                     f"now_h: {now_h}"
+        )
+    except Exception as e:
+        log_.error(f"地域分组小时级数据更新失败, exception: {e}, traceback: {traceback.format_exc()}")
+        send_msg_to_feishu(
+            webhook=config_.FEISHU_ROBOT['server_robot'].get('webhook'),
+            key_word=config_.FEISHU_ROBOT['server_robot'].get('key_word'),
+            msg_text=f"rov-offline{config_.ENV_TEXT} - 地域分组小时级数据更新失败\n "
+                     f"exception: {e}\n "
+                     f"traceback: {traceback.format_exc()}"
+        )
 
 
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':

+ 36 - 24
region_rule_rank_h_by24h.py

@@ -7,6 +7,7 @@
 import time
 import time
 import multiprocessing
 import multiprocessing
 import os
 import os
+import traceback
 import gevent
 import gevent
 import datetime
 import datetime
 import pandas as pd
 import pandas as pd
@@ -15,7 +16,7 @@ from functools import reduce
 from odps import ODPS
 from odps import ODPS
 from threading import Timer, Thread
 from threading import Timer, Thread
 from utils import RedisHelper, get_data_from_odps, filter_video_status, check_table_partition_exits, \
 from utils import RedisHelper, get_data_from_odps, filter_video_status, check_table_partition_exits, \
-    filter_video_status_app
+    filter_video_status_app, send_msg_to_feishu
 from config import set_config
 from config import set_config
 from log import Log
 from log import Log
 
 
@@ -473,29 +474,40 @@ def h_rank_bottom(now_date, now_h, rule_params, region_code_list):
 
 
 
 
 def h_timer_check():
 def h_timer_check():
-    rule_params = config_.RULE_PARAMS_REGION_24H_APP_TYPE
-    project = config_.PROJECT_REGION_24H_APP_TYPE
-    table = config_.TABLE_REGION_24H_APP_TYPE
-    region_code_list = [code for region, code in region_code.items() if code != '-1']
-    now_date = datetime.datetime.today()
-    now_h = datetime.datetime.now().hour
-    now_min = datetime.datetime.now().minute
-    log_.info(f"now_date: {datetime.datetime.strftime(now_date, '%Y%m%d%H')}")
-    # 查看当天更新的数据是否已准备好
-    h_data_count = data_check(project=project, table=table, now_date=now_date)
-    if h_data_count > 0:
-        log_.info(f'region_24h_data_count = {h_data_count}')
-        # 数据准备好,进行更新
-        rank_by_24h(now_date=now_date, now_h=now_h, rule_params=rule_params,
-                    project=project, table=table, region_code_list=region_code_list)
-        log_.info(f"region_24h_data end!")
-    elif now_min > 50:
-        log_.info('24h_recall data is None, use bottom data!')
-        h_rank_bottom(now_date=now_date, now_h=now_h, rule_params=rule_params, region_code_list=region_code_list)
-        log_.info(f"region_24h_data end!")
-    else:
-        # 数据没准备好,1分钟后重新检查
-        Timer(60, h_timer_check).start()
+    try:
+        rule_params = config_.RULE_PARAMS_REGION_24H_APP_TYPE
+        project = config_.PROJECT_REGION_24H_APP_TYPE
+        table = config_.TABLE_REGION_24H_APP_TYPE
+        region_code_list = [code for region, code in region_code.items() if code != '-1']
+        now_date = datetime.datetime.today()
+        now_h = datetime.datetime.now().hour
+        now_min = datetime.datetime.now().minute
+        log_.info(f"now_date: {datetime.datetime.strftime(now_date, '%Y%m%d%H')}")
+        # 查看当天更新的数据是否已准备好
+        h_data_count = data_check(project=project, table=table, now_date=now_date)
+        if h_data_count > 0:
+            log_.info(f'region_24h_data_count = {h_data_count}')
+            # 数据准备好,进行更新
+            rank_by_24h(now_date=now_date, now_h=now_h, rule_params=rule_params,
+                        project=project, table=table, region_code_list=region_code_list)
+            log_.info(f"region_24h_data end!")
+        elif now_min > 50:
+            log_.info('24h_recall data is None, use bottom data!')
+            h_rank_bottom(now_date=now_date, now_h=now_h, rule_params=rule_params, region_code_list=region_code_list)
+            log_.info(f"region_24h_data end!")
+        else:
+            # 数据没准备好,1分钟后重新检查
+            Timer(60, h_timer_check).start()
+
+    except Exception as e:
+        log_.error(f"地域分组24h数据更新失败, exception: {e}, traceback: {traceback.format_exc()}")
+        send_msg_to_feishu(
+            webhook=config_.FEISHU_ROBOT['server_robot'].get('webhook'),
+            key_word=config_.FEISHU_ROBOT['server_robot'].get('key_word'),
+            msg_text=f"rov-offline{config_.ENV_TEXT} - 地域分组24h数据更新失败\n "
+                     f"exception: {e}\n "
+                     f"traceback: {traceback.format_exc()}"
+        )
 
 
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':

+ 36 - 20
rule_rank_h_by_24h.py

@@ -1,12 +1,14 @@
 import pandas as pd
 import pandas as pd
 import math
 import math
+import traceback
 from functools import reduce
 from functools import reduce
 from odps import ODPS
 from odps import ODPS
 from threading import Timer
 from threading import Timer
 from datetime import datetime, timedelta
 from datetime import datetime, timedelta
 from get_data import get_data_from_odps
 from get_data import get_data_from_odps
 from db_helper import RedisHelper
 from db_helper import RedisHelper
-from utils import filter_video_status, check_table_partition_exits, filter_video_status_app, request_post
+from utils import filter_video_status, check_table_partition_exits, filter_video_status_app, \
+    request_post, send_msg_to_feishu
 from config import set_config
 from config import set_config
 from log import Log
 from log import Log
 
 
@@ -430,26 +432,40 @@ def h_rank_bottom(now_date, now_h, rule_params):
 
 
 
 
 def h_timer_check():
 def h_timer_check():
-    project = config_.PROJECT_24H_APP_TYPE
-    table = config_.TABLE_24H_APP_TYPE
-    rule_params = config_.RULE_PARAMS_24H_APP_TYPE
-    now_date = datetime.today()
-    log_.info(f"now_date: {datetime.strftime(now_date, '%Y%m%d%H')}")
-    now_min = datetime.now().minute
-    now_h = datetime.now().hour
-    # 查看当前天级更新的数据是否已准备好
-    h_data_count = h_data_check(project=project, table=table, now_date=now_date, now_h=now_h)
-    if h_data_count > 0:
-        log_.info(f'h_by24h_data_count = {h_data_count}')
-        # 数据准备好,进行更新
-        rank_by_h(now_date=now_date, now_h=now_h, rule_params=rule_params, project=project, table=table)
-    elif now_min > 45:
-        log_.info('h_by24h_recall data is None!')
-        h_rank_bottom(now_date=now_date, now_h=now_h, rule_params=rule_params)
-    else:
-        # 数据没准备好,1分钟后重新检查
-        Timer(60, h_timer_check).start()
+    try:
+        project = config_.PROJECT_24H_APP_TYPE
+        table = config_.TABLE_24H_APP_TYPE
+        rule_params = config_.RULE_PARAMS_24H_APP_TYPE
+        now_date = datetime.today()
+        log_.info(f"now_date: {datetime.strftime(now_date, '%Y%m%d%H')}")
+        now_min = datetime.now().minute
+        now_h = datetime.now().hour
+        # 查看当前天级更新的数据是否已准备好
+        h_data_count = h_data_check(project=project, table=table, now_date=now_date, now_h=now_h)
+        if h_data_count > 0:
+            log_.info(f'h_by24h_data_count = {h_data_count}')
+            # 数据准备好,进行更新
+            rank_by_h(now_date=now_date, now_h=now_h, rule_params=rule_params, project=project, table=table)
+            log_.info(f"24h_data end!")
+        elif now_min > 45:
+            log_.info('h_by24h_recall data is None, use bottom data!')
+            h_rank_bottom(now_date=now_date, now_h=now_h, rule_params=rule_params)
+            log_.info(f"24h_data end!")
+        else:
+            # 数据没准备好,1分钟后重新检查
+            Timer(60, h_timer_check).start()
+
+    except Exception as e:
+        log_.error(f"不区分地域24h数据更新失败, exception: {e}, traceback: {traceback.format_exc()}")
+        send_msg_to_feishu(
+            webhook=config_.FEISHU_ROBOT['server_robot'].get('webhook'),
+            key_word=config_.FEISHU_ROBOT['server_robot'].get('key_word'),
+            msg_text=f"rov-offline{config_.ENV_TEXT} - 不区分地域24h数据更新失败\n "
+                     f"exception: {e}\n "
+                     f"traceback: {traceback.format_exc()}"
+        )
 
 
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
+    log_.info(f"24h_data start...")
     h_timer_check()
     h_timer_check()