Spaces:
Sleeping
Sleeping
Chandima Prabhath
commited on
Commit
·
f7bd5c7
1
Parent(s):
8b4f8f9
Refactor Redis connection handling and update Dockerfile for environment variable support
Browse files- .gitignore +1 -0
- Dockerfile +3 -1
- video_encoder/api/main.py +6 -4
- video_encoder/config.py +17 -0
- video_encoder/worker/tasks.py +4 -3
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.env
|
Dockerfile
CHANGED
|
@@ -8,8 +8,10 @@ WORKDIR /app
|
|
| 8 |
|
| 9 |
COPY --chown=user ./requirements.txt requirements.txt
|
| 10 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
|
|
|
|
|
|
| 11 |
|
| 12 |
EXPOSE 7860
|
| 13 |
|
| 14 |
COPY --chown=user . /app
|
| 15 |
-
CMD ["python","run.py"]
|
|
|
|
| 8 |
|
| 9 |
COPY --chown=user ./requirements.txt requirements.txt
|
| 10 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 11 |
+
COPY --chown=user .env /app/.env
|
| 12 |
+
RUN pip install python-dotenv
|
| 13 |
|
| 14 |
EXPOSE 7860
|
| 15 |
|
| 16 |
COPY --chown=user . /app
|
| 17 |
+
CMD ["python","run.py"]
|
video_encoder/api/main.py
CHANGED
|
@@ -7,9 +7,11 @@ import hmac
|
|
| 7 |
import hashlib
|
| 8 |
from datetime import datetime
|
| 9 |
from rq import Queue
|
| 10 |
-
from redis import
|
| 11 |
-
from ..config import EncodingConfig
|
| 12 |
-
from ..worker.tasks import encode_video_task
|
|
|
|
|
|
|
| 13 |
|
| 14 |
app = FastAPI(title="Video Encoding Service")
|
| 15 |
|
|
@@ -32,7 +34,7 @@ async def upload_video(file: UploadFile):
|
|
| 32 |
|
| 33 |
@app.get("/status/{job_id}")
|
| 34 |
async def get_status(job_id: str):
|
| 35 |
-
redis =
|
| 36 |
progress = redis.hget(f"job:{job_id}", "progress")
|
| 37 |
|
| 38 |
if not progress:
|
|
|
|
| 7 |
import hashlib
|
| 8 |
from datetime import datetime
|
| 9 |
from rq import Queue
|
| 10 |
+
from redis import from_url
|
| 11 |
+
from ..config import EncodingConfig, RedisConfig
|
| 12 |
+
from ..worker.tasks import encode_video_task
|
| 13 |
+
|
| 14 |
+
q = Queue(connection=RedisConfig.get_connection(), default_timeout=3600)
|
| 15 |
|
| 16 |
app = FastAPI(title="Video Encoding Service")
|
| 17 |
|
|
|
|
| 34 |
|
| 35 |
@app.get("/status/{job_id}")
|
| 36 |
async def get_status(job_id: str):
|
| 37 |
+
redis = RedisConfig.get_connection()
|
| 38 |
progress = redis.hget(f"job:{job_id}", "progress")
|
| 39 |
|
| 40 |
if not progress:
|
video_encoder/config.py
CHANGED
|
@@ -1,8 +1,25 @@
|
|
| 1 |
import os
|
| 2 |
from typing import List, Dict
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
class EncodingConfig:
|
| 5 |
TEMP_DIR = "./tmp"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
HMAC_SECRET = os.getenv("VIDEO_HMAC_SECRET", "default-secret-change-me")
|
| 7 |
|
| 8 |
RESOLUTIONS: List[Dict] = [
|
|
|
|
| 1 |
import os
|
| 2 |
from typing import List, Dict
|
| 3 |
|
| 4 |
+
import os
|
| 5 |
+
from redis import from_url
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
class EncodingConfig:
|
| 11 |
TEMP_DIR = "./tmp"
|
| 12 |
+
|
| 13 |
+
class RedisConfig:
|
| 14 |
+
URL = os.getenv("REDIS_URL")
|
| 15 |
+
CONN_POOL = None # Will be initialized on first use
|
| 16 |
+
QUEUE_NAME = "video_encoding_queue"
|
| 17 |
+
|
| 18 |
+
@classmethod
|
| 19 |
+
def get_connection(cls):
|
| 20 |
+
if not cls.CONN_POOL:
|
| 21 |
+
cls.CONN_POOL = from_url(cls.URL, max_connections=20)
|
| 22 |
+
return cls.CONN_POOL
|
| 23 |
HMAC_SECRET = os.getenv("VIDEO_HMAC_SECRET", "default-secret-change-me")
|
| 24 |
|
| 25 |
RESOLUTIONS: List[Dict] = [
|
video_encoder/worker/tasks.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
import shutil
|
| 4 |
-
from redis import
|
|
|
|
| 5 |
from rq import Queue
|
| 6 |
from pathlib import Path
|
| 7 |
from typing import Optional
|
|
@@ -11,10 +12,10 @@ from ..config import EncodingConfig
|
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
-
q = Queue(connection=
|
| 15 |
|
| 16 |
def update_job_progress(job_id: str, progress: float):
|
| 17 |
-
|
| 18 |
|
| 19 |
def encode_video_task(job_id: str, input_path: str) -> Optional[str]:
|
| 20 |
try:
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
import shutil
|
| 4 |
+
from redis import from_url
|
| 5 |
+
from ..config import RedisConfig
|
| 6 |
from rq import Queue
|
| 7 |
from pathlib import Path
|
| 8 |
from typing import Optional
|
|
|
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
+
q = Queue(connection=RedisConfig.get_connection(), default_timeout=3600)
|
| 16 |
|
| 17 |
def update_job_progress(job_id: str, progress: float):
|
| 18 |
+
RedisConfig.get_connection().hset(f"job:{job_id}", "progress", progress)
|
| 19 |
|
| 20 |
def encode_video_task(job_id: str, input_path: str) -> Optional[str]:
|
| 21 |
try:
|