Ver código fonte

accident shutdown prevention

ming 3 anos atrás
pai
commit
5d17331081
1 arquivos alterados com 73 adições e 47 exclusões
  1. 73 47
      api/main.py

+ 73 - 47
api/main.py

@@ -83,7 +83,7 @@ video_dest = '/var/www/html/'+video_sub_folder
 avatar_dest = '/var/www/html/'+avatar_sub_folder
 pttx_dest = '/var/www/html/'+pttx_sub_folder
 
-
+clear_video_queue()
 
 # @app.get("/index2")
 # async def index2():
@@ -765,26 +765,33 @@ def notify_group(msg):
 
 def gen_video_long_queue(name_hash,name,text_content, image_urls,avatar,multiLang,video_id,user_id):
     db_check()
-    time_stamp = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
-    txt_content_seperate_by_dot = ''
-    for txt in text_content:
-        txt_content_seperate_by_dot += txt+","
-    txt_content_seperate_by_dot = txt_content_seperate_by_dot[:-1]
-    img_urls_seperate_by_dot = ''
-    for iurl in image_urls:
-        img_urls_seperate_by_dot += iurl+","
-    img_urls_seperate_by_dot = img_urls_seperate_by_dot[:-1]
+    if name_hash == 'keepRunning':
+        if first(db.query('SELECT COUNT(1) FROM video_queue where video_type="longvideo"'))['COUNT(1)'] == 0:
+            return 
+        db.query('UPDATE video_queue_status SET status = 0;')
+        print('start clear long video')
+    else:     
+        time_stamp = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
+        txt_content_seperate_by_dot = ''
+        for txt in text_content:
+            txt_content_seperate_by_dot += txt+","
+        txt_content_seperate_by_dot = txt_content_seperate_by_dot[:-1]
+        img_urls_seperate_by_dot = ''
+        for iurl in image_urls:
+            img_urls_seperate_by_dot += iurl+","
+        img_urls_seperate_by_dot = img_urls_seperate_by_dot[:-1]  
+        db['video_queue'].insert({'name_hash':name_hash,'name':name,'text_content':txt_content_seperate_by_dot
+        ,'image_urls':img_urls_seperate_by_dot,'multiLang':multiLang,'video_type':'longvideo','avatar':avatar,'timestamp':time_stamp})
     
-    db['video_queue'].insert({'name_hash':name_hash,'name':name,'text_content':txt_content_seperate_by_dot,'image_urls':img_urls_seperate_by_dot,'multiLang':multiLang,'avatar':avatar,'timestamp':time_stamp})
     while True:
         
         if first(db.query('SELECT * FROM video_queue_status'))['status'] == 1:#only one row in this table, which is the id 1 one
             print('another process running, leave loop')#1 means already running
             break
-        if first(db.query('SELECT COUNT(1) FROM video_queue'))['COUNT(1)'] == 0:
+        if first(db.query('SELECT COUNT(1) FROM video_queue where video_type="longvideo"'))['COUNT(1)'] == 0:
             print('all finish, leave loop')
             break
-        top1 = first(db.query('SELECT * FROM video_queue'))
+        top1 = first(db.query('SELECT * FROM video_queue where video_type="longvideo"'))
         try:
         # if True:
             db.query('UPDATE video_queue_status SET status = 1;')
@@ -821,28 +828,36 @@ def gen_video_long_queue(name_hash,name,text_content, image_urls,avatar,multiLan
             notify_group('長影片錯誤-測試')
         db['video_queue'].delete(id=top1['id'])
         db.query('UPDATE video_queue_status SET status = 0')
+
 def gen_video_queue(name_hash,name,text_content, image_urls,avatar,multiLang,video_id,user_id):
     db_check()
-    time_stamp = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
-    txt_content_seperate_by_dot = ''
-    for txt in text_content:
-        txt_content_seperate_by_dot += txt+","
-    txt_content_seperate_by_dot = txt_content_seperate_by_dot[:-1]
-    img_urls_seperate_by_dot = ''
-    for iurl in image_urls:
-        img_urls_seperate_by_dot += iurl+","
-    img_urls_seperate_by_dot = img_urls_seperate_by_dot[:-1]
-    
-    db['video_queue'].insert({'name_hash':name_hash,'name':name,'text_content':txt_content_seperate_by_dot,'image_urls':img_urls_seperate_by_dot,'multiLang':multiLang,'avatar':avatar,'timestamp':time_stamp})
+    if name_hash == 'keepRunning':
+        if first(db.query('SELECT COUNT(1) FROM video_queue where video_type="short"'))['COUNT(1)'] == 0:
+            return 
+        db.query('UPDATE video_queue_status SET status = 0;')
+        print('start clear short video')
+    else:        
+        time_stamp = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
+        txt_content_seperate_by_dot = ''
+        for txt in text_content:
+            txt_content_seperate_by_dot += txt+","
+        txt_content_seperate_by_dot = txt_content_seperate_by_dot[:-1]
+        img_urls_seperate_by_dot = ''
+        for iurl in image_urls:
+            img_urls_seperate_by_dot += iurl+","
+        img_urls_seperate_by_dot = img_urls_seperate_by_dot[:-1]
+        
+        db['video_queue'].insert({'name_hash':name_hash,'name':name,'text_content':txt_content_seperate_by_dot
+        ,'image_urls':img_urls_seperate_by_dot,'multiLang':multiLang,'video_type':'short','avatar':avatar,'timestamp':time_stamp})
     while True:
         
         if first(db.query('SELECT * FROM video_queue_status'))['status'] == 1:#only one row in this table, which is the id 1 one
             print('another process running, leave loop')#1 means already running
             break
-        if first(db.query('SELECT COUNT(1) FROM video_queue'))['COUNT(1)'] == 0:
+        if first(db.query('SELECT COUNT(1) FROM video_queue where video_type="short"'))['COUNT(1)'] == 0:
             print('all finish, leave loop')
             break
-        top1 = first(db.query('SELECT * FROM video_queue'))
+        top1 = first(db.query('SELECT * FROM video_queue where video_type="short"'))
         try:
         # if True:
             db.query('UPDATE video_queue_status SET status = 1;')
@@ -882,28 +897,35 @@ def gen_video_queue(name_hash,name,text_content, image_urls,avatar,multiLang,vid
 
 def gen_video_queue_eng(name_hash,name,text_content, image_urls,sub_titles,avatar,video_id):
     db_check()
-    time_stamp = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
-    txt_content_seperate_by_dot = ''
-    for txt in text_content:
-        txt_content_seperate_by_dot += txt+","
-    txt_content_seperate_by_dot = txt_content_seperate_by_dot[:-1]
-    img_urls_seperate_by_dot = ''
-    for iurl in image_urls:
-        img_urls_seperate_by_dot += iurl+","
-    img_urls_seperate_by_dot = img_urls_seperate_by_dot[:-1]
-    subtitles_seperate_by_dot = ''
-    for sub in sub_titles:
-        subtitles_seperate_by_dot += sub+","
-    subtitles_seperate_by_dot = subtitles_seperate_by_dot[:-1]
-    db['video_queue'].insert({'name_hash':name_hash,'name':name,'text_content':txt_content_seperate_by_dot,'image_urls':img_urls_seperate_by_dot,'subtitles':subtitles_seperate_by_dot,'avatar':avatar,'timestamp':time_stamp})
+    if name_hash == 'keepRunning':
+        if first(db.query('SELECT COUNT(1) FROM video_queue where video_type="eng"'))['COUNT(1)'] == 0:
+            return 
+        db.query('UPDATE video_queue_status SET status = 0;')
+        print('start clear eng video')
+    else: 
+        time_stamp = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
+        txt_content_seperate_by_dot = ''
+        for txt in text_content:
+            txt_content_seperate_by_dot += txt+","
+        txt_content_seperate_by_dot = txt_content_seperate_by_dot[:-1]
+        img_urls_seperate_by_dot = ''
+        for iurl in image_urls:
+            img_urls_seperate_by_dot += iurl+","
+        img_urls_seperate_by_dot = img_urls_seperate_by_dot[:-1]
+        subtitles_seperate_by_dot = ''
+        for sub in sub_titles:
+            subtitles_seperate_by_dot += sub+","
+        subtitles_seperate_by_dot = subtitles_seperate_by_dot[:-1]
+        db['video_queue'].insert({'name_hash':name_hash,'name':name,'text_content':txt_content_seperate_by_dot,
+        'image_urls':img_urls_seperate_by_dot,'subtitles':subtitles_seperate_by_dot,'video_type':'eng','avatar':avatar,'timestamp':time_stamp})
     while True:
         if first(db.query('SELECT * FROM video_queue_status'))['status'] == 1:#only one row in this table, which is the id 1 one
             print('another process running, leave loop')
             break
-        if first(db.query('SELECT COUNT(1) FROM video_queue'))['COUNT(1)'] == 0:
+        if first(db.query('SELECT COUNT(1) FROM video_queue where video_type=="eng"'))['COUNT(1)'] == 0:
             print('all finish, leave loop')
             break
-        top1 = first(db.query('SELECT * FROM video_queue'))
+        top1 = first(db.query('SELECT * FROM video_queue where video_type=="eng"'))
         try:
             db.query('UPDATE video_queue_status SET status = 1;')
             c = rpyc.connect("localhost", 8858)
@@ -990,11 +1012,15 @@ def db_check():
     db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
 
 
-    
-      
-        
-        
-        
+def clear_video_queue():
+    x = threading.Thread(target=gen_video_queue_eng, args=('keepRunning','', '', '','','',''))
+    x.start()
+
+    x2 = threading.Thread(target=gen_video_queue, args=('keepRunning','', '', '','','','',''))
+    x2.start()
+
+    x3 = threading.Thread(target=gen_video_long_queue, args=('keepRunning','', '', '','','','',''))
+    x3.start()