Browse Source

checkVideoStatusDaily.py

由 crontab 来控制每个小时执行一次
数据库连接增加报警
luojunhui 6 months ago
parent
commit
8262fa9d9d
3 changed files with 94 additions and 60 deletions
  1. 50 42
      checkVideoStatusDaily.py
  2. 26 0
      sh/run_check_video_status_hourly.sh
  3. 18 18
      threadAliveBot.py

+ 50 - 42
checkVideoStatusDaily.py

@@ -2,10 +2,7 @@
 @author: luojunhui
 @description: 校验视频状态,若视频状态为不通过,则修改视频状态
 """
-import os
-import time
-
-import multiprocessing
+import traceback
 
 from tqdm import tqdm
 
@@ -16,17 +13,47 @@ class VideoStatusManager(object):
     """
     视频状态校验 and 修改
     """
-    db_client = PQMySQL()
-    pq_api = PQAPI()
+    def __init__(self):
+        self.db_client = None
+        self.pq_api = None
 
-    @classmethod
-    def getVideoListStatus(cls, videoList):
+    def base_init(self):
+        """
+        初始化数据库连接和 pq 方法类
+        """
+        try:
+            self.db_client = PQMySQL()
+        except Exception as e:
+            error_msg = traceback.format_exc()
+            bot(
+                title="视频状态校验任务,每 20 分钟执行一次,数据库初始化异常",
+                detail={
+                    "error": str(e),
+                    "error_msg": error_msg
+                }
+            )
+            return False
+        try:
+            self.pq_api = PQAPI()
+        except Exception as e:
+            error_msg = traceback.format_exc()
+            bot(
+                title="视频状态校验任务,每 20 分钟执行一次,apollo连接异常",
+                detail={
+                    "error": str(e),
+                    "error_msg": error_msg
+                }
+            )
+            return False
+        return True
+
+    def get_video_list_status(self, videoList):
         """
         获取视频 list 的状态,并且返回状态为不通过的视频 list
         :param videoList: 视频 id_list, 最长为 20
         :return: bad video list
         """
-        response = cls.pq_api.getPQVideoListDetail(video_list=videoList)
+        response = self.pq_api.getPQVideoListDetail(video_list=videoList)
         detail_list = response.get('data', [])
         if detail_list:
             bad_video_list = [i for i in detail_list if i['auditStatus'] != 5]
@@ -35,8 +62,7 @@ class VideoStatusManager(object):
             bad_id_list = []
         return bad_id_list
 
-    @classmethod
-    def getPublishedVideoIdsDaily(cls):
+    def get_published_video_ids_daily(self):
         """
         获取每日发布的视频 id 的状态
         :return:
@@ -47,12 +73,11 @@ class VideoStatusManager(object):
         FROM get_off_videos
         WHERE check_status = 0 and video_status = 1;
         """
-        video_id_tuple = cls.db_client.select(select_sql)
+        video_id_tuple = self.db_client.select(select_sql)
         video_id_list = [i[0] for i in video_id_tuple]
         return video_id_list
 
-    @classmethod
-    def updateCheckStatus(cls, vid_list):
+    def update_check_status(self, vid_list):
         """
 
         :param vid_list:
@@ -63,36 +88,33 @@ class VideoStatusManager(object):
         SET check_status = %s
         where video_id in %s;
         """
-        cls.db_client.update(
+        self.db_client.update(
             sql=sql,
             params=(1, tuple(vid_list))
         )
         print("更新 check_status 成功")
 
-    @classmethod
-    def deal(cls):
+    def deal(self):
         """
         Deal Function
         :return:
         """
-
         def chunk_iterator(arr, chunk_size):
             """生成器函数,将数组分组成指定大小的chunks"""
             for i in range(0, len(arr), chunk_size):
                 yield arr[i:i + chunk_size]
 
-        video_id_list = cls.getPublishedVideoIdsDaily()
-
+        video_id_list = self.get_published_video_ids_daily()
         video_chunks = chunk_iterator(video_id_list, 10)
 
         bad_count = 0
         for video_temp in video_chunks:
-            bad_id_list = cls.getVideoListStatus(video_temp)
+            bad_id_list = self.get_video_list_status(video_temp)
             fail_list = []
             if bad_id_list:
                 bad_count += len(bad_id_list)
                 for bad_id in tqdm(bad_id_list):
-                    response = cls.pq_api.changeVideoStatus(bad_id)
+                    response = self.pq_api.changeVideoStatus(bad_id)
                     if not response:
                         fail_list.append(bad_id)
             if fail_list:
@@ -100,35 +122,21 @@ class VideoStatusManager(object):
                     title="修改视频状态失败",
                     detail=fail_list
                 )
-            cls.updateCheckStatus(video_temp)
+            self.update_check_status(video_temp)
         print("total", len(video_id_list))
         print("bad_total", bad_count)
 
 
-def task():
+def main():
     """
     task
     :return:
     """
-    while True:
-        VM = VideoStatusManager()
+
+    VM = VideoStatusManager()
+    if VM.base_init():
         VM.deal()
-        # print(1)
-        time.sleep(10 * 60)
 
 
 if __name__ == '__main__':
-    process = None
-
-    try:
-        while True:
-            if process is None or not process.is_alive():
-                process = multiprocessing.Process(target=task)
-                process.start()
-
-            time.sleep(60)
-    except KeyboardInterrupt:
-        if process and process.is_alive():
-            process.terminate()
-
-
+    main()

+ 26 - 0
sh/run_check_video_status_hourly.sh

@@ -0,0 +1,26 @@
+#!/bin/bash
+
+# 获取当前日期,格式为 YYYY-MM-DD
+CURRENT_DATE=$(date +%F)
+
+# 日志文件路径,包含日期
+LOG_FILE="/root/luojunhui/logs/check_video_status_task_log_$CURRENT_DATE.txt"
+
+# 重定向整个脚本的输出到带日期的日志文件
+exec >> "$LOG_FILE" 2>&1
+if pgrep -f "python3 checkVideoStatusDaily.py" > /dev/null
+then
+    echo "$(date '+%Y-%m-%d %H:%M:%S') - checkVideoStatusDaily.py is running"
+else
+    echo "$(date '+%Y-%m-%d %H:%M:%S') - trying to restart checkVideoStatusDaily.py"
+    # 切换到指定目录
+    cd /root/luojunhui/LongArticlesJob
+
+    # 激活 Conda 环境
+    source /root/miniconda3/etc/profile.d/conda.sh
+    conda activate tasks
+
+    # 在后台运行 Python 脚本并重定向日志输出
+    nohup python3 checkVideoStatusDaily.py >> "${LOG_FILE}" 2>&1 &
+    echo "$(date '+%Y-%m-%d %H:%M:%S') - successfully restarted checkVideoStatusDaily.py"
+fi

+ 18 - 18
threadAliveBot.py

@@ -28,13 +28,13 @@ def threadMonitor():
 
     # migrate_source_id_job = [line for line in output.splitlines() if 'python3 migrateRootSourceId.py' in line]
 
-    updateAccountAvgDaily = [line for line in output.splitlines() if 'python3 updateAccountAvgDaily.py' in line]
+    # updateAccountAvgDaily = [line for line in output.splitlines() if 'python3 updateAccountAvgDaily.py' in line]
 
     updateMinigramInfoDaily = [line for line in output.splitlines() if 'python3 updateMinigramInfoDaily.py' in line]
 
     updatePublishedMsgDaily = [line for line in output.splitlines() if 'python3 updatePublishedMsgDaily.py' in line]
 
-    checkVideoStatusDaily = [line for line in output.splitlines() if 'python3 checkVideoStatusDaily.py' in line]
+    # checkVideoStatusDaily = [line for line in output.splitlines() if 'python3 checkVideoStatusDaily.py' in line]
 
     if not get_off_job:
         bot(
@@ -45,14 +45,14 @@ def threadMonitor():
             }
         )
 
-    if not updateAccountAvgDaily:
-        bot(
-            title="定时任务进程异常挂掉",
-            detail={
-                "Job": "updateAccountAvgDaily",
-                "Time": datetime.datetime.now().__str__()
-            }
-        )
+    # if not updateAccountAvgDaily:
+    #     bot(
+    #         title="定时任务进程异常挂掉",
+    #         detail={
+    #             "Job": "updateAccountAvgDaily",
+    #             "Time": datetime.datetime.now().__str__()
+    #         }
+    #     )
 
     if not updateMinigramInfoDaily:
         bot(
@@ -72,14 +72,14 @@ def threadMonitor():
             }
         )
 
-    if not checkVideoStatusDaily:
-        bot(
-            title="定时任务进程异常挂掉",
-            detail={
-                "Job": "checkVideoStatusDaily",
-                "Time": datetime.datetime.now().__str__()
-            }
-        )
+    # if not checkVideoStatusDaily:
+    #     bot(
+    #         title="定时任务进程异常挂掉",
+    #         detail={
+    #             "Job": "checkVideoStatusDaily",
+    #             "Time": datetime.datetime.now().__str__()
+    #         }
+    #     )
 
 
 if __name__ == '__main__':