实现定时清除过期日志和录像的功能。

feature/assist-websocket
Apex Liu 2022-09-15 00:42:36 +08:00
parent 3ba80f02d2
commit a7099228e6
2 changed files with 32 additions and 3 deletions

View File

@ -1,7 +1,12 @@
# -*- coding: utf-8 -*-
import datetime
from app.const import *
from app.base.cron import tp_cron
from app.base.configs import tp_cfg
from app.base.logger import log
from app.model import ops
from app.model import record
def tp_init_jobs():
@ -10,8 +15,28 @@ def tp_init_jobs():
# 每隔一个小时清理一次过期的远程连接授权码
cron.add_job('clean_expired_ops_token', _job_clean_expired_ops_token, first_interval_seconds=10, interval_seconds=3600)
# 每50秒检查一下是否要到了每日清理系统日志和会话记录的时间了到了则执行清理操作
cron.add_job('check_and_clean_log_and_record', _job_clean_log_and_record, interval_seconds=50)
return True
def _job_clean_expired_ops_token():
log.v('clean expired ops token.\n')
ops.clean_expired_ops_token()
def _job_clean_log_and_record():
sto = tp_cfg().sys.storage
if sto.keep_log == 0 or sto.keep_record == 0:
# nothing need remove
return
now = datetime.datetime.now()
if not (now.hour == sto.cleanup_hour and now.minute == sto.cleanup_minute):
return
log.v('[cron] time to clean log and record.\n')
err, msg = record.cleanup_log_and_record()
for m in msg:
log.v('[cron] {}\n'.format(m))

View File

@ -550,7 +550,10 @@ def session_end(record_id, ret_code):
@tornado.gen.coroutine
def cleanup_storage(handler):
# storage config
return cleanup_log_and_record()
def cleanup_log_and_record():
sto = tp_cfg().sys.storage
db = get_db()
@ -562,7 +565,7 @@ def cleanup_storage(handler):
chk_time = _now - sto.keep_log * 24 * 60 * 60
if sto.keep_log > 0:
# find out all sys-log to be remove
# find out all sys-log to be removed
s.select_from('syslog', ['id'], alt_name='s')
s.where('s.log_time<{chk_time}'.format(chk_time=chk_time))
err = s.query()
@ -583,6 +586,7 @@ def cleanup_storage(handler):
else:
msg.append('{} 条系统日志已清除!'.format(removed_log))
chk_time = _now - sto.keep_record * 24 * 60 * 60
if sto.keep_record > 0:
core_cfg = tp_cfg().core
if not core_cfg.detected:
@ -594,7 +598,7 @@ def cleanup_storage(handler):
have_error = True
msg.append('清除指定会话录像失败:会话录像路径不存在({}'.format(replay_path))
else:
# find out all record to be remove
# find out all record to be removed
s.reset().select_from('record', ['id', 'protocol_type'], alt_name='r')
s.where('r.time_begin<{chk_time}'.format(chk_time=chk_time))
err = s.query()