Przeglądaj źródła

modified video api and celery worker

tomoya 2 lat temu
rodzic
commit
b938c18caf

+ 4 - 0
backend/app/app/api/api_v1/endpoints/videos.py

@@ -14,6 +14,8 @@ from app.core.celery_app import celery_app
 from app.core.config import settings
 from app.core.config import settings
 from pathlib import Path
 from pathlib import Path
 
 
+from app.core.celery_app import celery_app
+
 ZIP_STORAGE = Path("/app").joinpath(settings.BACKEND_ZIP_STORAGE)
 ZIP_STORAGE = Path("/app").joinpath(settings.BACKEND_ZIP_STORAGE)
 VIDEO_STORAGE = Path("/app").joinpath(settings.BACKEND_VIDEO_STORAGE)
 VIDEO_STORAGE = Path("/app").joinpath(settings.BACKEND_VIDEO_STORAGE)
 
 
@@ -64,6 +66,8 @@ def upload_plot(
         return {"error": str(e)}
         return {"error": str(e)}
     finally:
     finally:
         upload_file.file.close()
         upload_file.file.close()
+
+    celery_app.send_task("app.worker.test_celery", args=[video.id, video.stored_file_name, current_user.id])
     return video
     return video
 
 
 @router.get("/{id}")
 @router.get("/{id}")

+ 1 - 1
backend/app/app/core/celery_app.py

@@ -1,6 +1,6 @@
 from celery import Celery
 from celery import Celery
 
 
-celery_app = Celery("worker", broker="redis://172.104.93.163:16379/video/")
+celery_app = Celery("worker", broker="redis://172.104.93.163:16379/0")
 
 
 
 
 
 

+ 9 - 32
backend/app/app/worker.py

@@ -1,44 +1,30 @@
 from raven import Client
 from raven import Client
-
+import os
 from app.core.celery_app import celery_app
 from app.core.celery_app import celery_app
 from app.core.config import settings
 from app.core.config import settings
 import requests
 import requests
 from pathlib import Path
 from pathlib import Path
 from urllib.parse import urlparse, urljoin
 from urllib.parse import urlparse, urljoin
 
 
-client_sentry = Client(settings.SENTRY_DSN)
+#client_sentry = Client(settings.SENTRY_DSN)
 
 
-download_to_local_url = urljoin(settings.SERVER_ADDRESS, settings.API_V1_STR, "/videos/worker")
-upload_to_server_url = urljoin(settings.SERVER_ADDRESS, settings.API_V1_STR, "/videos/worker")
+download_to_local_url = urljoin(settings.SERVER_HOST, settings.API_V1_STR, "/videos/worker")
+upload_to_server_url = urljoin(settings.SERVER_HOST, settings.API_V1_STR, "/videos/worker")
 
 
-ZIP_STORAGE = Path("/app").joinpath(settings.CELERY_ZIP_STORAGE)
-VIDEO_STORAGE = Path("/app").joinpath(settings.CELERY_VIDEO_STORAGE)
+ZIP_STORAGE = Path(settings.CELERY_ZIP_STORAGE) 
+VIDEO_STORAGE = Path(settings.CELERY_VIDEO_STORAGE)
 
 
 
 
 @celery_app.task(acks_late=True)
 @celery_app.task(acks_late=True)
 def test_celery(word: str) -> str:
 def test_celery(word: str) -> str:
     return f"test task return {word}"
     return f"test task return {word}"
 
 
-@celery_app.task(acks_late=True)
-def download_to_local(id:int, file_name:str) -> str:
-
-    zip_file = Path(file_name+"/.zip")
-    r = requests.get(download_to_local_url, stream=True)
-
-    with open(str(VIDEO_STORAGE/zip_file), 'wb') as f:
-        r.raise_for_status()
-        for chunk in r.iter_content(chunk_size=1024):
-            f.write(chunk)
-    return "complete"
-
 
 
 @celery_app.task(acks_late=True)
 @celery_app.task(acks_late=True)
-def make_video(id:int, file_name) -> str:
+def make_video(video_id:int, zip_filename:str, user_id:int) -> str:
     # download 
     # download 
-    zip_file = Path(file_name+"/.zip")
     r = requests.get(download_to_local_url, stream=True)
     r = requests.get(download_to_local_url, stream=True)
-
-    with open(str(VIDEO_STORAGE/zip_file), 'wb') as f:
+    with open(str(VIDEO_STORAGE/zip_filename), 'wb') as f:
         r.raise_for_status()
         r.raise_for_status()
         for chunk in r.iter_content(chunk_size=1024):
         for chunk in r.iter_content(chunk_size=1024):
             f.write(chunk)
             f.write(chunk)
@@ -46,16 +32,7 @@ def make_video(id:int, file_name) -> str:
     # make video
     # make video
 
 
 
 
-    video_file = Path(file_name+".mp4")
-    r = requests.post(urljoin(upload_to_server_url,str(id)))
-
-
+    
     return "complete"
     return "complete"
 
 
 
 
-@celery_app.task(acks_late=True)
-def upload_to_server(id:int, file_name:str) -> str:
-
-    video_file = Path(file_name+".mp4")
-    r = requests.post(urljoin(upload_to_server_url,str(id)))
-    return "complete"