From 11e2cd34544a8d63f01f16cc6c5d6dd7166a111b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=82=96=E5=AE=87=E8=BF=AA?= Date: Tue, 24 Jun 2025 18:54:06 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=87=E7=94=A8=E5=AE=9A=E6=97=B6=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E6=AF=8F=E5=A4=A93=20AM=20=E9=A6=99=E6=B8=AF=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E6=B8=85=E7=90=86=E8=BF=87=E6=9C=9F=E7=9A=84=E7=9B=B4?= =?UTF-8?q?=E6=92=AD=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 采用定时任务每天3 AM 香港时间清理过期的直播缓存 --------- Merge request URL: https://g-ldyi2063.coding.net/p/dev/d/modalDeploy/git/merge/4868?initial=true Co-authored-by: shuohigh@gmail.com --- src/cluster/ffmpeg_apps/schedule_cleanup.py | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/cluster/ffmpeg_apps/schedule_cleanup.py diff --git a/src/cluster/ffmpeg_apps/schedule_cleanup.py b/src/cluster/ffmpeg_apps/schedule_cleanup.py new file mode 100644 index 0000000..c361cbb --- /dev/null +++ b/src/cluster/ffmpeg_apps/schedule_cleanup.py @@ -0,0 +1,35 @@ +import shutil + +import modal + +from ..ffmpeg_app import ffmpeg_worker_image, app, config, s3_mount, local_copy_to_s3, output_path_prefix + +with ffmpeg_worker_image.imports(): + import os, time + from datetime import datetime + + hls_recording_volume = modal.Volume.from_name("stream_records", create_if_missing=True) + hls_recording_mount_point = "/mnt/stream_records" + + + # runs daily at 3 am (Hong Kong time) + @app.function( + volumes={hls_recording_mount_point: hls_recording_volume}, + schedule=modal.Cron("0 3 * * *", timezone="Asia/HongKong") + ) + def storage_cleanup(): + # todo: clean up storage + output_dir = f"{config.modal_environment}/records/hls/" + volume_output_dir = f"{hls_recording_mount_point}/{output_dir}" + + now = time.time() + cutoff = now - 24 * 60 * 60 # 24小时之前的时间戳 + old_folders = [] + for entry in os.scandir(volume_output_dir): + if entry.is_dir(): + mtime = entry.stat().st_mtime + if mtime < cutoff: + old_folders.append((entry.path, datetime.fromtimestamp(mtime))) + + for folder in old_folders: + shutil.rmtree(folder)