diff --git a/codewiki/src/fe/background_worker.py b/codewiki/src/fe/background_worker.py index 007ef88c..a9fe7729 100644 --- a/codewiki/src/fe/background_worker.py +++ b/codewiki/src/fe/background_worker.py @@ -9,6 +9,7 @@ import threading import subprocess import asyncio +import logging from datetime import datetime from pathlib import Path from queue import Queue @@ -23,6 +24,8 @@ from .config import WebAppConfig from codewiki.src.utils import file_manager +logger = logging.getLogger(__name__) + class BackgroundWorker: """Background worker for processing documentation generation jobs.""" @@ -41,7 +44,7 @@ def start(self): self.running = True thread = threading.Thread(target=self._worker_loop, daemon=True) thread.start() - print("Background worker started") + logger.info("Background worker started") def stop(self): """Stop the background worker.""" @@ -84,9 +87,9 @@ def load_job_statuses(self): progress=job_data.get('progress', ''), docs_path=job_data.get('docs_path') ) - print(f"Loaded {len([j for j in self.job_status.values() if j.status == 'completed'])} completed jobs from disk") + logger.info(f"Loaded {len([j for j in self.job_status.values() if j.status == 'completed'])} completed jobs from disk") except Exception as e: - print(f"Error loading job statuses: {e}") + logger.error(f"Error loading job statuses: {e}") def _reconstruct_jobs_from_cache(self): """Reconstruct job statuses from cache entries for backward compatibility.""" @@ -114,14 +117,14 @@ def _reconstruct_jobs_from_cache(self): ) reconstructed_count += 1 except Exception as e: - print(f"Failed to reconstruct job for {cache_entry.repo_url}: {e}") + logger.error(f"Failed to reconstruct job for {cache_entry.repo_url}: {e}") if reconstructed_count > 0: - print(f"Reconstructed {reconstructed_count} job statuses from cache") + logger.info(f"Reconstructed {reconstructed_count} job statuses from cache") self.save_job_statuses() except Exception as e: - print(f"Error reconstructing jobs from cache: {e}") + logger.error(f"Error reconstructing jobs from cache: {e}") def save_job_statuses(self): """Save job statuses to disk.""" @@ -145,7 +148,7 @@ def save_job_statuses(self): file_manager.save_json(data, self.jobs_file) except Exception as e: - print(f"Error saving job statuses: {e}") + logger.error(f"Error saving job statuses: {e}") def _worker_loop(self): """Main worker loop.""" @@ -157,7 +160,7 @@ def _worker_loop(self): else: time.sleep(1) except Exception as e: - print(f"Worker error: {e}") + logger.error(f"Worker error: {e}") time.sleep(1) def _process_job(self, job_id: str): @@ -187,7 +190,7 @@ def _process_job(self, job_id: str): # Save job status to disk self.save_job_statuses() - print(f"Job {job_id}: Using cached documentation") + logger.info(f"Job {job_id}: Using cached documentation") return # Clone repository @@ -236,7 +239,7 @@ def _process_job(self, job_id: str): # Save job status to disk self.save_job_statuses() - print(f"Job {job_id}: Documentation generated successfully") + logger.info(f"Job {job_id}: Documentation generated successfully") except Exception as e: # Update job status with error @@ -245,7 +248,7 @@ def _process_job(self, job_id: str): job.error_message = str(e) job.progress = f"Failed: {str(e)}" - print(f"Job {job_id}: Failed with error: {e}") + logger.error(f"Job {job_id}: Failed with error: {e}") finally: # Cleanup temporary repository @@ -253,4 +256,4 @@ def _process_job(self, job_id: str): try: subprocess.run(['rm', '-rf', temp_repo_dir], check=True) except Exception as e: - print(f"Failed to cleanup temp directory: {e}") \ No newline at end of file + logger.error(f"Failed to cleanup temp directory: {e}")