working
This commit is contained in:
62
FitnessSync/backend/src/services/job_manager.py
Normal file
62
FitnessSync/backend/src/services/job_manager.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import uuid
|
||||
import logging
|
||||
from typing import Dict, Optional, List
|
||||
from datetime import datetime
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class JobManager:
|
||||
_instance = None
|
||||
|
||||
def __new__(cls):
|
||||
if cls._instance is None:
|
||||
cls._instance = super(JobManager, cls).__new__(cls)
|
||||
cls._instance.active_jobs = {}
|
||||
return cls._instance
|
||||
|
||||
def create_job(self, operation: str) -> str:
|
||||
job_id = str(uuid.uuid4())
|
||||
self.active_jobs[job_id] = {
|
||||
"id": job_id,
|
||||
"operation": operation,
|
||||
"status": "running",
|
||||
"cancel_requested": False,
|
||||
"start_time": datetime.now(),
|
||||
"progress": 0,
|
||||
"message": "Starting..."
|
||||
}
|
||||
logger.info(f"Created job {job_id} for {operation}")
|
||||
return job_id
|
||||
|
||||
def get_job(self, job_id: str) -> Optional[Dict]:
|
||||
return self.active_jobs.get(job_id)
|
||||
|
||||
def get_active_jobs(self) -> List[Dict]:
|
||||
return list(self.active_jobs.values())
|
||||
|
||||
def update_job(self, job_id: str, status: str = None, progress: int = None, message: str = None):
|
||||
if job_id in self.active_jobs:
|
||||
if status:
|
||||
self.active_jobs[job_id]["status"] = status
|
||||
if progress is not None:
|
||||
self.active_jobs[job_id]["progress"] = progress
|
||||
if message:
|
||||
self.active_jobs[job_id]["message"] = message
|
||||
|
||||
def request_cancel(self, job_id: str) -> bool:
|
||||
if job_id in self.active_jobs:
|
||||
self.active_jobs[job_id]["cancel_requested"] = True
|
||||
self.active_jobs[job_id]["message"] = "Cancelling..."
|
||||
logger.info(f"Cancellation requested for job {job_id}")
|
||||
return True
|
||||
return False
|
||||
|
||||
def should_cancel(self, job_id: str) -> bool:
|
||||
job = self.active_jobs.get(job_id)
|
||||
return job and job.get("cancel_requested", False)
|
||||
|
||||
def complete_job(self, job_id: str):
|
||||
if job_id in self.active_jobs:
|
||||
del self.active_jobs[job_id]
|
||||
|
||||
job_manager = JobManager()
|
||||
Reference in New Issue
Block a user