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)