Skip to content

[Backend] Automatic Workspace Cleanup via TTL #333

Description

@arpit2006

Description

Each scan creates a temporary workspace under WORKSPACE_ROOT to store uploaded files, extracted archives, and scan artifacts. Currently, these workspaces are only removed when a client explicitly calls DELETE /jobs/{job_id}. If jobs are not manually deleted, temporary directories and their corresponding database records accumulate over time, consuming disk space and leaving stale metadata behind.

Implement an automatic cleanup mechanism that periodically removes expired job workspaces and their associated database records based on a configurable time-to-live (TTL).

Acceptance Criteria

  • Add a background cleanup task using FastAPI lifespan or APScheduler.
  • Execute the cleanup task every hour.
  • Delete workspaces older than JOB_TTL_HOURS (default: 24 hours).
  • Make the TTL configurable via the JOB_TTL_HOURS environment variable.
  • Remove the corresponding records from the SQLite database, including:
    • jobs
    • related findings
    • any other associated job data
  • Log cleanup activity at INFO level, including the number of deleted jobs.
  • Add unit/integration tests that mock the filesystem and verify:
    • expired workspaces are deleted,
    • recent workspaces are preserved,
    • related database records are removed correctly.
  • Document the JOB_TTL_HOURS environment variable in the README.md under a new Configuration section.

Why This Matters

Automatically cleaning up expired workspaces prevents unnecessary disk usage, keeps the database free of stale records, and improves the stability of long-running PatchPilot deployments without requiring manual maintenance.

Difficulty: Medium

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions