Run background job from API endpoint

This commit is contained in:
Jose134 2024-12-08 02:55:47 +01:00
parent aeb5abad41
commit e434d28c16
2 changed files with 13 additions and 6 deletions

View File

@ -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] Be able to move and rename files based on a regex
- [x] Read qbittorrent credentials from .env file - [x] Read qbittorrent credentials from .env file
- [x] Implement API endpoint using FastAPI - [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 - [ ] Deduplicate files using Czkawka
- [ ] Add unit tests - [ ] Add unit tests
- [ ] Add logging - [ ] Add logging

View File

@ -1,10 +1,11 @@
import os import os
from fastapi import FastAPI from fastapi import BackgroundTasks, FastAPI
from dotenv import load_dotenv from dotenv import load_dotenv
from qbittorrent_api import get_qbittorrent_files_downloading from qbittorrent_api import get_qbittorrent_files_downloading
from filemoving import group_files_by_prefix from filemoving import group_files_by_prefix
import uuid import uuid
import time
load_dotenv() 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 = FastAPI()
@app.get("/") @app.get("/")
async def root(): def root(background_tasks: BackgroundTasks):
job_id = str(uuid.uuid4()) 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) # downloading = get_qbittorrent_files_downloading(qbit_url, qbit_user, qbit_password)
# group_files_by_prefix(target_dir, downloading) # group_files_by_prefix(target_dir, downloading)
return job_id