diff --git a/README.md b/README.md index a6f8299..6a3ab65 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Store all the logs for the current job in order to be able to debug and keep tra - [x] Be able to move and rename files based on a regex - [x] Read qbittorrent credentials from .env file - [x] Implement API endpoint using FastAPI -- [ ] Run organization ob on a separate thread +- [x] Run organization job on a separate thread - [ ] Deduplicate files using Czkawka - [ ] Add unit tests - [ ] Add logging diff --git a/src/main.py b/src/main.py index 1f90ad0..b1a18db 100644 --- a/src/main.py +++ b/src/main.py @@ -1,10 +1,11 @@ import os -from fastapi import FastAPI +from fastapi import BackgroundTasks, FastAPI from dotenv import load_dotenv from qbittorrent_api import get_qbittorrent_files_downloading from filemoving import group_files_by_prefix import uuid +import time load_dotenv() @@ -19,11 +20,17 @@ if not qbit_url or not qbit_user or not qbit_password or not target_dir: app = FastAPI() @app.get("/") -async def root(): +def root(background_tasks: BackgroundTasks): job_id = str(uuid.uuid4()) + background_tasks.add_task(launch_job, job_id) + return job_id - # TODO Launch the job in the background +def launch_job(job_id): + # WARNING: Temporary code for testing purposes + time.sleep(5) + + with open("jobs.txt", "a") as f: + f.write(f"{job_id}\n") + # downloading = get_qbittorrent_files_downloading(qbit_url, qbit_user, qbit_password) # group_files_by_prefix(target_dir, downloading) - - return job_id