task_util.py 957 B

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("Creating workspace: %s (from template: %s)", workspace, template_path)
  10. t0 = time.time()
  11. copy_directory(template_path, workspace)
  12. elapsed = time.time() - t0
  13. logger.info("Workspace ready in %.2fs", 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("Cleaning up workspace: %s", workspace)
  21. delete_directory(workspace)
  22. logger.info("Workspace cleaned up")
  23. except Exception as e:
  24. logger.error("Error cleaning up workspace %s: %s", workspace, e)