task_util.py 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. import logging
  2. import os
  3. import time
  4. from app.util.file_util import copy_directory, delete_directory
  5. logger = logging.getLogger(__name__)
  6. BASE_DIR = "data/data"
  7. def create_task_workspace(task_id: str, template_path: str) -> str:
  8. workspace = os.path.join(BASE_DIR, task_id)
  9. logger.info("[%s] Creating workspace: %s (from template: %s)", task_id, workspace, template_path)
  10. t0 = time.time()
  11. copy_directory(template_path, workspace)
  12. elapsed = time.time() - t0
  13. logger.info("[%s] Workspace ready in %.2fs", task_id, elapsed)
  14. return workspace
  15. def cleanup_workspace(workspace: str, task_id: str):
  16. if not workspace or not workspace.strip():
  17. return
  18. try:
  19. if os.path.isdir(workspace):
  20. logger.info("[%s] Cleaning up workspace: %s", task_id, workspace)
  21. delete_directory(workspace)
  22. logger.info("[%s] Workspace cleaned up", task_id)
  23. except Exception as e:
  24. logger.error("[%s] Error cleaning up workspace %s: %s", task_id, workspace, e)