-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
122 lines (96 loc) · 3.15 KB
/
Copy pathmain.py
File metadata and controls
122 lines (96 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import sys
import time
from src.config import Config
from src.db_manager import DBManager
from src.logger import setup_logger
from src.processor import process_file, cleanup_expired_files
from src.scanner import Scanner
try:
import fcntl
HAS_FCNTL = True
except ImportError:
HAS_FCNTL = False
LOCK_FILE = "/tmp/video_watchdog.lock"
def acquire_lock():
if not HAS_FCNTL:
print(
"Warning: fcntl module not found. File locking is only supported on Linux/Unix."
)
return None
try:
lock_fd = open(LOCK_FILE, "w")
fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
return lock_fd
except IOError:
print("另一个实例正在运行,已退出。")
sys.exit(1)
def run_task(
task_config, scanner, db_manager, logger, scan_interval, monitoring_logged
):
report = scanner.scan(task_config, db_manager, logger)
if report.expired_files:
cleanup_expired_files(report.expired_files, db_manager, logger)
task_name = task_config.name
source_dir = task_config.source_dir
if not report.entries:
if task_name not in monitoring_logged:
logger.info(
f"【{task_name}】正在以 {scan_interval} 秒的间隔持续监听 {source_dir}"
)
monitoring_logged.add(task_name)
return False
monitoring_logged.discard(task_name)
for entry in report.entries:
process_file(entry, task_config, db_manager, logger)
return True
def main():
_lock_fd = acquire_lock()
try:
config = Config()
except Exception as e:
print(f"加载配置失败,原因:\n{e}")
sys.exit(1)
global_cfg = config.global_config
log_dir = global_cfg.get("log_dir", "logs")
log_level = global_cfg.get("log_level", "INFO")
logger = setup_logger(
log_dir=log_dir,
max_log_files=global_cfg.get("max_log_files", 7),
log_level=log_level,
)
db_manager = DBManager()
scanner = Scanner()
tasks = config.tasks
logger.info("VideoWatchdog 已启动。")
scan_interval = global_cfg.get("scan_interval", 0)
monitoring_logged = set()
if scan_interval == 0:
logger.info("全局扫描间隔为 0,作为一次性任务执行。")
for task_config in tasks:
run_task(
task_config,
scanner,
db_manager,
logger,
scan_interval,
monitoring_logged,
)
logger.info("所有任务完成。")
else:
logger.info("VideoWatchdog 已进入监听模式。")
try:
while True:
for task_config in tasks:
run_task(
task_config,
scanner,
db_manager,
logger,
scan_interval,
monitoring_logged,
)
time.sleep(scan_interval)
except KeyboardInterrupt:
logger.info("VideoWatchdog 接收到退出信号,正在退出……")
if __name__ == "__main__":
main()