celery workers
Browse files- App/Worker.py +30 -30
- App/celery_config.py +7 -7
- Dockerfile +4 -3
- requirements.txt +1 -1
    	
        App/Worker.py
    CHANGED
    
    | @@ -10,15 +10,15 @@ from asgiref.sync import async_to_sync | |
| 10 | 
             
            import json
         | 
| 11 | 
             
            import os
         | 
| 12 |  | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 15 | 
             
            # celery.conf.update(
         | 
| 16 | 
             
            #     # Other Celery configuration settings
         | 
| 17 | 
             
            #     CELERYD_LOG_LEVEL="DEBUG",  # Set log level to DEBUG for the worker
         | 
| 18 | 
             
            # )
         | 
| 19 |  | 
| 20 |  | 
| 21 | 
            -
             | 
| 22 | 
             
            def create_json_file(assets: List[Assets], asset_dir: str):
         | 
| 23 | 
             
                for asset in assets:
         | 
| 24 | 
             
                    filename = f"{asset.type.capitalize()}Sequences.json"
         | 
| @@ -59,7 +59,7 @@ def download_with_wget(link, download_dir, filename): | |
| 59 | 
             
                subprocess.run(["aria2c", link, "-d", download_dir, "-o", filename])
         | 
| 60 |  | 
| 61 |  | 
| 62 | 
            -
             | 
| 63 | 
             
            def copy_remotion_app(src: str, dest: str):
         | 
| 64 | 
             
                shutil.copytree(src, dest)
         | 
| 65 |  | 
| @@ -68,7 +68,7 @@ def copy_remotion_app(src: str, dest: str): | |
| 68 | 
             
                # create_symlink(source_dir, target_dir=dest, symlink_name="node_module")
         | 
| 69 |  | 
| 70 |  | 
| 71 | 
            -
             | 
| 72 | 
             
            def unsilence(directory: str):
         | 
| 73 | 
             
                output_dir = os.path.join(directory, "out/video.mp4")
         | 
| 74 | 
             
                shortered_dir = os.path.join(directory, "out/temp.mp4")
         | 
| @@ -77,13 +77,13 @@ def unsilence(directory: str): | |
| 77 | 
             
                os.rename(shortered_dir, output_dir)
         | 
| 78 |  | 
| 79 |  | 
| 80 | 
            -
             | 
| 81 | 
             
            def install_dependencies(directory: str):
         | 
| 82 | 
             
                os.chdir(directory)
         | 
| 83 | 
             
                os.system("npm install")
         | 
| 84 |  | 
| 85 |  | 
| 86 | 
            -
             | 
| 87 | 
             
            def download_assets(links: List[LinkInfo], temp_dir: str):
         | 
| 88 | 
             
                public_dir = f"{temp_dir}/public"
         | 
| 89 | 
             
                for link in links:
         | 
| @@ -92,14 +92,14 @@ def download_assets(links: List[LinkInfo], temp_dir: str): | |
| 92 | 
             
                    download_with_wget(file_link, public_dir, file_name)
         | 
| 93 |  | 
| 94 |  | 
| 95 | 
            -
             | 
| 96 | 
             
            def render_video(directory: str, output_directory: str):
         | 
| 97 | 
             
                os.chdir(directory)
         | 
| 98 | 
             
                os.system(f"npm run build --output {output_directory}")
         | 
| 99 | 
             
                print("complete")
         | 
| 100 |  | 
| 101 |  | 
| 102 | 
            -
             | 
| 103 | 
             
            async def cleanup_temp_directory(
         | 
| 104 | 
             
                temp_dir: str, output_dir: str, chat_id: int = -1002069945904
         | 
| 105 | 
             
            ):
         | 
| @@ -114,7 +114,7 @@ async def cleanup_temp_directory( | |
| 114 | 
             
                    shutil.rmtree(temp_dir, ignore_errors=True)
         | 
| 115 |  | 
| 116 |  | 
| 117 | 
            -
             | 
| 118 | 
             
            async def celery_task(video_task: EditorRequest):
         | 
| 119 | 
             
                remotion_app_dir = os.path.join("/srv", "Remotion-app")
         | 
| 120 | 
             
                project_id = str(uuid.uuid4())
         | 
| @@ -123,26 +123,26 @@ async def celery_task(video_task: EditorRequest): | |
| 123 |  | 
| 124 | 
             
                assets_dir = os.path.join(temp_dir, "src/HelloWorld/Assets")
         | 
| 125 |  | 
| 126 | 
            -
                copy_remotion_app(remotion_app_dir, temp_dir),
         | 
| 127 | 
            -
                install_dependencies(temp_dir),
         | 
| 128 | 
            -
                create_constants_json_file(video_task.constants, assets_dir),
         | 
| 129 | 
            -
                create_json_file(video_task.assets, assets_dir),
         | 
| 130 | 
            -
                download_assets(video_task.links, temp_dir) if video_task.links else None,
         | 
| 131 | 
            -
                render_video(temp_dir, output_dir),
         | 
| 132 | 
            -
                unsilence(temp_dir),
         | 
| 133 | 
            -
                await cleanup_temp_directory(temp_dir, output_dir),
         | 
| 134 | 
            -
             | 
| 135 | 
            -
                 | 
| 136 | 
            -
             | 
| 137 | 
            -
             | 
| 138 | 
            -
             | 
| 139 | 
            -
             | 
| 140 | 
            -
             | 
| 141 | 
            -
             | 
| 142 | 
            -
             | 
| 143 | 
            -
                 | 
| 144 | 
            -
             | 
| 145 | 
            -
                 | 
| 146 |  | 
| 147 |  | 
| 148 | 
             
            def handle_error(task_id, err, *args, **kwargs):
         | 
|  | |
| 10 | 
             
            import json
         | 
| 11 | 
             
            import os
         | 
| 12 |  | 
| 13 | 
            +
            celery = Celery()
         | 
| 14 | 
            +
            celery.config_from_object(celery_config)
         | 
| 15 | 
             
            # celery.conf.update(
         | 
| 16 | 
             
            #     # Other Celery configuration settings
         | 
| 17 | 
             
            #     CELERYD_LOG_LEVEL="DEBUG",  # Set log level to DEBUG for the worker
         | 
| 18 | 
             
            # )
         | 
| 19 |  | 
| 20 |  | 
| 21 | 
            +
            @celery.task(name="CreateFile")
         | 
| 22 | 
             
            def create_json_file(assets: List[Assets], asset_dir: str):
         | 
| 23 | 
             
                for asset in assets:
         | 
| 24 | 
             
                    filename = f"{asset.type.capitalize()}Sequences.json"
         | 
|  | |
| 59 | 
             
                subprocess.run(["aria2c", link, "-d", download_dir, "-o", filename])
         | 
| 60 |  | 
| 61 |  | 
| 62 | 
            +
            @celery.task(name="CopyRemotion")
         | 
| 63 | 
             
            def copy_remotion_app(src: str, dest: str):
         | 
| 64 | 
             
                shutil.copytree(src, dest)
         | 
| 65 |  | 
|  | |
| 68 | 
             
                # create_symlink(source_dir, target_dir=dest, symlink_name="node_module")
         | 
| 69 |  | 
| 70 |  | 
| 71 | 
            +
            @celery.task(name="Unsilence")
         | 
| 72 | 
             
            def unsilence(directory: str):
         | 
| 73 | 
             
                output_dir = os.path.join(directory, "out/video.mp4")
         | 
| 74 | 
             
                shortered_dir = os.path.join(directory, "out/temp.mp4")
         | 
|  | |
| 77 | 
             
                os.rename(shortered_dir, output_dir)
         | 
| 78 |  | 
| 79 |  | 
| 80 | 
            +
            @celery.task(name="InstallDependency")
         | 
| 81 | 
             
            def install_dependencies(directory: str):
         | 
| 82 | 
             
                os.chdir(directory)
         | 
| 83 | 
             
                os.system("npm install")
         | 
| 84 |  | 
| 85 |  | 
| 86 | 
            +
            @celery.task(name="DownloadAssets")
         | 
| 87 | 
             
            def download_assets(links: List[LinkInfo], temp_dir: str):
         | 
| 88 | 
             
                public_dir = f"{temp_dir}/public"
         | 
| 89 | 
             
                for link in links:
         | 
|  | |
| 92 | 
             
                    download_with_wget(file_link, public_dir, file_name)
         | 
| 93 |  | 
| 94 |  | 
| 95 | 
            +
            @celery.task(name="RenderFile")
         | 
| 96 | 
             
            def render_video(directory: str, output_directory: str):
         | 
| 97 | 
             
                os.chdir(directory)
         | 
| 98 | 
             
                os.system(f"npm run build --output {output_directory}")
         | 
| 99 | 
             
                print("complete")
         | 
| 100 |  | 
| 101 |  | 
| 102 | 
            +
            @celery.task(name="send")
         | 
| 103 | 
             
            async def cleanup_temp_directory(
         | 
| 104 | 
             
                temp_dir: str, output_dir: str, chat_id: int = -1002069945904
         | 
| 105 | 
             
            ):
         | 
|  | |
| 114 | 
             
                    shutil.rmtree(temp_dir, ignore_errors=True)
         | 
| 115 |  | 
| 116 |  | 
| 117 | 
            +
            @celery.task(name="All")
         | 
| 118 | 
             
            async def celery_task(video_task: EditorRequest):
         | 
| 119 | 
             
                remotion_app_dir = os.path.join("/srv", "Remotion-app")
         | 
| 120 | 
             
                project_id = str(uuid.uuid4())
         | 
|  | |
| 123 |  | 
| 124 | 
             
                assets_dir = os.path.join(temp_dir, "src/HelloWorld/Assets")
         | 
| 125 |  | 
| 126 | 
            +
                # copy_remotion_app(remotion_app_dir, temp_dir),
         | 
| 127 | 
            +
                # install_dependencies(temp_dir),
         | 
| 128 | 
            +
                # create_constants_json_file(video_task.constants, assets_dir),
         | 
| 129 | 
            +
                # create_json_file(video_task.assets, assets_dir),
         | 
| 130 | 
            +
                # download_assets(video_task.links, temp_dir) if video_task.links else None,
         | 
| 131 | 
            +
                # render_video(temp_dir, output_dir),
         | 
| 132 | 
            +
                # unsilence(temp_dir),
         | 
| 133 | 
            +
                # await cleanup_temp_directory(temp_dir, output_dir),
         | 
| 134 | 
            +
             | 
| 135 | 
            +
                chain(
         | 
| 136 | 
            +
                    copy_remotion_app.si(remotion_app_dir, temp_dir),
         | 
| 137 | 
            +
                    install_dependencies.si(temp_dir),
         | 
| 138 | 
            +
                    create_json_file.si(video_task.assets, assets_dir),
         | 
| 139 | 
            +
                    download_assets.si(video_task.links, temp_dir) if video_task.links else None,
         | 
| 140 | 
            +
                    render_video.si(temp_dir, output_dir),
         | 
| 141 | 
            +
                    unsilence.si(temp_dir),
         | 
| 142 | 
            +
                    cleanup_temp_directory.si(temp_dir, output_dir),
         | 
| 143 | 
            +
                ).apply_async(
         | 
| 144 | 
            +
                    # link_error=handle_error
         | 
| 145 | 
            +
                )  # Link the tasks and handle errors
         | 
| 146 |  | 
| 147 |  | 
| 148 | 
             
            def handle_error(task_id, err, *args, **kwargs):
         | 
    	
        App/celery_config.py
    CHANGED
    
    | @@ -9,13 +9,13 @@ result_accept_content = ["application/json", "application/x-python-serialize"] | |
| 9 | 
             
            timezone = "Europe/Oslo"
         | 
| 10 | 
             
            enable_utc = True
         | 
| 11 |  | 
| 12 | 
            -
            broker_url = f" | 
| 13 | 
             
            result_backend = f"db+postgresql+psycopg2://postgres:[email protected]:5432/postgres"
         | 
| 14 |  | 
| 15 | 
             
            # SSL/TLS and SNI configuration
         | 
| 16 | 
            -
            broker_use_ssl = {
         | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
            }
         | 
|  | |
| 9 | 
             
            timezone = "Europe/Oslo"
         | 
| 10 | 
             
            enable_utc = True
         | 
| 11 |  | 
| 12 | 
            +
            broker_url = f"amqps://llmgzyix:WZZdL_6mmwvoawt58_gYYJV4veF8dOZm@beaver.rmq.cloudamqp.com/llmgzyix"
         | 
| 13 | 
             
            result_backend = f"db+postgresql+psycopg2://postgres:[email protected]:5432/postgres"
         | 
| 14 |  | 
| 15 | 
             
            # SSL/TLS and SNI configuration
         | 
| 16 | 
            +
            # broker_use_ssl = {
         | 
| 17 | 
            +
            #     "ssl_cert_reqs": ssl.CERT_NONE,
         | 
| 18 | 
            +
            #     "ssl_match_hostname": False,
         | 
| 19 | 
            +
            #     "ssl_check_hostname": False,
         | 
| 20 | 
            +
            #     "ssl_sni": "master.cache--j5zxzwppzvjs.addon.code.run",
         | 
| 21 | 
            +
            # }
         | 
    	
        Dockerfile
    CHANGED
    
    | @@ -9,7 +9,7 @@ RUN chmod -R 755 /srv | |
| 9 |  | 
| 10 | 
             
            # Install dependencies
         | 
| 11 | 
             
            RUN apt-get update && \
         | 
| 12 | 
            -
              apt-get install -y | 
| 13 |  | 
| 14 | 
             
            RUN apt-get install -y \
         | 
| 15 | 
             
              fonts-liberation \
         | 
| @@ -26,6 +26,7 @@ RUN apt-get install -y \ | |
| 26 | 
             
              libvulkan1 \
         | 
| 27 | 
             
              libxcomposite1 \
         | 
| 28 | 
             
              libxdamage1 \
         | 
|  | |
| 29 | 
             
              libxfixes3 \
         | 
| 30 | 
             
              libasound2 \
         | 
| 31 | 
             
              libxkbcommon0 \
         | 
| @@ -69,9 +70,9 @@ RUN pipx install unsilence | |
| 69 |  | 
| 70 |  | 
| 71 | 
             
            # Command to run the application
         | 
| 72 | 
            -
             | 
| 73 |  | 
| 74 | 
            -
            CMD python -m uvicorn App.app:app --host 0.0.0.0  --port 7860  --workers 2
         | 
| 75 |  | 
| 76 |  | 
| 77 | 
             
            EXPOSE 7860
         | 
|  | |
| 9 |  | 
| 10 | 
             
            # Install dependencies
         | 
| 11 | 
             
            RUN apt-get update && \
         | 
| 12 | 
            +
              apt-get install -y   wget ffmpeg curl aria2
         | 
| 13 |  | 
| 14 | 
             
            RUN apt-get install -y \
         | 
| 15 | 
             
              fonts-liberation \
         | 
|  | |
| 26 | 
             
              libvulkan1 \
         | 
| 27 | 
             
              libxcomposite1 \
         | 
| 28 | 
             
              libxdamage1 \
         | 
| 29 | 
            +
              mesa-vulkan-drivers\
         | 
| 30 | 
             
              libxfixes3 \
         | 
| 31 | 
             
              libasound2 \
         | 
| 32 | 
             
              libxkbcommon0 \
         | 
|  | |
| 70 |  | 
| 71 |  | 
| 72 | 
             
            # Command to run the application
         | 
| 73 | 
            +
            CMD python -m uvicorn App.app:app --host 0.0.0.0 --port 7860 &  python -m celery -A App.Worker.celery worker -c 5  --max-tasks-per-child=1  --without-heartbeat 
         | 
| 74 |  | 
| 75 | 
            +
            # CMD python -m uvicorn App.app:app --host 0.0.0.0  --port 7860  --workers 2
         | 
| 76 |  | 
| 77 |  | 
| 78 | 
             
            EXPOSE 7860
         | 
    	
        requirements.txt
    CHANGED
    
    | @@ -10,7 +10,7 @@ Werkzeug==2.2.2 | |
| 10 | 
             
            uvicorn==0.21.1
         | 
| 11 | 
             
            gunicorn
         | 
| 12 | 
             
            requests
         | 
| 13 | 
            -
            celery
         | 
| 14 | 
             
            pyrogram
         | 
| 15 | 
             
            Telethon
         | 
| 16 | 
             
            TgCrypto
         | 
|  | |
| 10 | 
             
            uvicorn==0.21.1
         | 
| 11 | 
             
            gunicorn
         | 
| 12 | 
             
            requests
         | 
| 13 | 
            +
            celery==5.3.0
         | 
| 14 | 
             
            pyrogram
         | 
| 15 | 
             
            Telethon
         | 
| 16 | 
             
            TgCrypto
         |