Skip to content

Commit e05169e

Browse files
authored
fix: add retry (#260)
fix #255
1 parent 42c3dae commit e05169e

4 files changed

Lines changed: 67 additions & 23 deletions

File tree

settings.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ reserve_for_fixing = false # If encounter MOOV crash error, delete the video or
2727
upload_line = "auto" # The upload line to be used, default None is auto detect(recommended), if you want to specify, it can be "bldsa", "ws", "tx", "qn", "bda2".
2828

2929
[slice]
30-
auto_slice = false # General control
30+
auto_slice = false # General control: true or false
3131
slice_duration = 60 # better not exceed 300 seconds
32-
slice_num = 2
33-
slice_overlap = 30
34-
slice_step = 1
32+
slice_num = 2 # the number of slices
33+
slice_overlap = 30 # the overlap of slices(seconds) see my package https://github.com/timerring/auto-slice-video for more details
34+
slice_step = 1 # the step of slices(seconds)
3535
min_video_size = 200 # The minimum video size to be sliced (MB)
3636
mllm_model = "gemini" # the multi-model LLMs, can be "gemini" or "zhipu" or "qwen"
3737
zhipu_api_key = "" # Apply for your own GLM-4v-Plus API key at https://www.bigmodel.cn/invite?icode=shBtZUfNE6FfdMH1R6NybGczbXFgPRGIalpycrEwJ28%3D

src/db/conn.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import sqlite3
22
import os
33

4-
# DATA_BASE_FILE ='./data.db'
54
DATA_BASE_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data.db')
65

76
def connect():
@@ -122,4 +121,4 @@ def delete_all_queue():
122121
# Get the single upload queue after delete, should be None
123122
# print(get_single_upload_queue())
124123
# Delete all queue
125-
delete_all_queue()
124+
# delete_all_queue()

src/log/retry.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from time import sleep
2+
from src.log.logger import scan_log
3+
from typing import Any, Tuple
4+
5+
class Retry:
6+
def __init__(self, max_retry: int, interval: int = 5, check_func = lambda r: r, default = None):
7+
'''retry the function and check the return value
8+
Args:
9+
max_retry: the maximum of retries
10+
interval: the interval of retries
11+
check_func: the function to check the return value
12+
default: the default return value
13+
14+
Return:
15+
status: the status of the excution of the function
16+
return_value: the return value of the function
17+
'''
18+
self.max_retry = max_retry
19+
self.check_func = check_func
20+
self.interval = interval
21+
self.default = default
22+
23+
def run(self, func, *args, **kwargs) -> Tuple[bool, Any]:
24+
status = (False, self.default)
25+
for i in range(0, self.max_retry):
26+
try:
27+
return_value = func(*args, **kwargs)
28+
if self.check_func(return_value):
29+
status = (True,return_value)
30+
break
31+
except Exception as e:
32+
scan_log.error(f"Exceptions in trial {i+1}/{self.max_retry} : {e}")
33+
sleep(self.interval)
34+
35+
return status
36+
37+
def decorator(self, func):
38+
def _(*args, **kwargs):
39+
return self.run(func, *args, **kwargs)[1]
40+
return _

src/upload/upload.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@
99
from src.upload.extract_video_info import generate_title
1010
from src.log.logger import upload_log
1111
import time
12-
import fcntl
1312
from concurrent.futures import ThreadPoolExecutor, as_completed
1413
from db.conn import get_single_upload_queue, delete_upload_queue, update_upload_queue_lock, get_single_lock_queue
15-
import threading
1614
from .bilitool.bilitool import UploadController, FeedController, LoginController
15+
from src.log.retry import Retry
1716

18-
# read_lock = threading.Lock()
19-
17+
@Retry(max_retry = 3, interval = 5).decorator
2018
def upload_video(upload_path):
2119
try:
2220
if upload_path.endswith('.flv'):
2321
copyright, title, tid, tag = generate_slice_data(upload_path)
22+
yaml, desc, source, cover, dynamic = ("",) * 5
2423
if title is None:
2524
upload_log.error("Fail to upload slice video, the files will be reserved.")
2625
update_upload_queue_lock(upload_path, 0)
@@ -33,6 +32,7 @@ def upload_video(upload_path):
3332
upload_log.info("Upload successfully, then delete the video")
3433
os.remove(upload_path)
3534
delete_upload_queue(upload_path)
35+
return True
3636
else:
3737
upload_log.error("Fail to upload, the files will be reserved.")
3838
update_upload_queue_lock(upload_path, 0)
@@ -43,6 +43,7 @@ def upload_video(upload_path):
4343
update_upload_queue_lock(upload_path, 0)
4444
return False
4545

46+
@Retry(max_retry = 3, interval = 5).decorator
4647
def append_upload(upload_path, bv_result):
4748
try:
4849
result = UploadController().append_video_entry(upload_path, bv_result, cdn=UPLOAD_LINE)
@@ -51,6 +52,7 @@ def append_upload(upload_path, bv_result):
5152
upload_log.info("Upload successfully, then delete the video")
5253
os.remove(upload_path)
5354
delete_upload_queue(upload_path)
55+
return True
5456
else:
5557
upload_log.error("Fail to append, the files will be reserved.")
5658
update_upload_queue_lock(upload_path, 0)
@@ -92,19 +94,22 @@ def read_append_and_delete_lines():
9294
if upload_queue:
9395
video_path = upload_queue['video_path']
9496
time.sleep(3) # avoid JIT read error
95-
query = generate_title(video_path)
96-
if query is None: # JIT read error or MOOV crash error or interrupted error
97-
if not os.path.exists(video_path):
98-
# Interrupted error, the file has been uploaded. But record is not deleted.
99-
delete_upload_queue(video_path) # Directly delete the record
100-
continue
101-
else:
102-
# JIT read error or MOOV crash error
103-
upload_log.error(f"Error occurred in ffprobe: {video_path}")
104-
update_upload_queue_lock(video_path, 1) # Lock first, wait for the lock execute round
105-
continue
106-
upload_log.info(f"deal with {video_path}")
107-
video_gate(video_path)
97+
if video_path.endswith('.flv'):
98+
video_gate(video_path)
99+
else:
100+
query = generate_title(video_path)
101+
if query is None: # JIT read error or MOOV crash error or interrupted error
102+
if not os.path.exists(video_path):
103+
# Interrupted error, the file has been uploaded. But record is not deleted.
104+
delete_upload_queue(video_path) # Directly delete the record
105+
continue
106+
else:
107+
# JIT read error or MOOV crash error
108+
upload_log.error(f"Error occurred in ffprobe: {video_path}")
109+
update_upload_queue_lock(video_path, 1) # Lock first, wait for the lock execute round
110+
continue
111+
upload_log.info(f"deal with {video_path}")
112+
video_gate(video_path)
108113
elif lock_queue:
109114
# check the lock video
110115
video_path = lock_queue['video_path']

0 commit comments

Comments
 (0)