dzen.nstarikov.ru/docs/superpowers/plans/2026-06-27-video-cleanup.md
2026-06-27 14:07:10 +03:00

65 lines
2.7 KiB
Markdown

# Video Cleanup Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Delete downloaded video artifacts after confirmed upload to OK while preserving retry files for failed uploads.
**Architecture:** Add a focused cleanup helper to `app/worker.py` that validates the target directory is beneath the configured VK storage root, removes that directory, and clears persisted local paths. Invoke it only after `ok_video_id` has been assigned. Cleanup errors are logged without changing the successful upload status.
**Tech Stack:** Python 3.12, pathlib, shutil, SQLAlchemy, Celery, Docker Compose
---
### Task 1: Add Safe Artifact Cleanup
**Files:**
- Modify: `app/worker.py`
- [ ] **Step 1: Import directory removal support**
Add `import shutil` alongside the existing standard-library imports.
- [ ] **Step 2: Add the cleanup helper**
Implement a helper with this contract:
```python
def cleanup_uploaded_video_artifacts(video: Video) -> None:
video_root = (settings.storage_path / "vk").resolve()
paths = [video.local_video_path, video.local_cover_path]
directories = {Path(path).resolve().parent for path in paths if path}
for directory in directories:
if directory == video_root or video_root not in directory.parents:
raise ValueError(f"Refusing to clean path outside video storage: {directory}")
if directory.exists():
shutil.rmtree(directory)
video.local_video_path = None
video.local_cover_path = None
```
- [ ] **Step 3: Invoke cleanup after successful upload**
After assigning `video.ok_video_id` and setting `video.status = "ok_uploaded"`, call the helper inside a `try/except OSError` block. Log failures and retain the successful upload status.
- [ ] **Step 4: Compile the worker**
Run: `python3 -m compileall app/worker.py`
Expected: `Compiling 'app/worker.py'...` with exit code 0.
### Task 2: Deploy and Clean Existing Successful Downloads
**Files:**
- Deploy: `app/worker.py` to `/var/www/bots/vk2ok/app/worker.py`
- [ ] **Step 1: Rebuild services**
Run `docker compose up -d --build api worker beat` in `/var/www/bots/vk2ok`.
- [ ] **Step 2: Clean existing uploaded artifacts**
Inside the API container, query `Video` rows where `ok_video_id IS NOT NULL` and either local path is set. Apply `cleanup_uploaded_video_artifacts` and commit each successful cleanup.
- [ ] **Step 3: Verify health and storage state**
Confirm all containers are running, Celery recovery returns normally, uploaded rows have null local paths, and no open publication work remains.