async def cancel_source_connection_job(
    *,
    db: AsyncSession = Depends(deps.get_db),
    source_connection_id: UUID,
    job_id: UUID,
    user: schemas.User = Depends(deps.get_user),
) -> schemas.SourceConnectionJob:
    """Cancel a running sync job for a source connection.

    This will send a cancellation signal to the Temporal workflow if enabled.
    The workflow will handle the cancellation and update the job status to CANCELLED.

    Args:
        db: The database session
        source_connection_id: The ID of the source connection
        job_id: The ID of the sync job to cancel
        user: The current user

    Returns:
        The cancelled sync job
    """
    # First verify the job exists and belongs to this source connection
    sync_job = await source_connection_service.get_source_connection_job(
        db=db, source_connection_id=source_connection_id, job_id=job_id, current_user=user
    )

    # Check if the job is in a cancellable state
    if sync_job.status not in [SyncJobStatus.CREATED, SyncJobStatus.IN_PROGRESS]:
        raise HTTPException(
            status_code=400, detail=f"Cannot cancel job in {sync_job.status} status"
        )

    # If Temporal is enabled, try to cancel the workflow
    if await temporal_service.is_temporal_enabled():
        try:
            cancelled = await temporal_service.cancel_sync_job_workflow(str(job_id))
            if cancelled:
                logger.info(f"Successfully sent cancellation signal for job {job_id}")
            else:
                logger.warning(f"No running Temporal workflow found for job {job_id}")
                # Even if no workflow found, we might want to update the status
                # if it's stuck in IN_PROGRESS
                if sync_job.status == SyncJobStatus.IN_PROGRESS:
                    await sync_job_service.update_status(
                        sync_job_id=job_id,
                        status=SyncJobStatus.CANCELLED,
                        current_user=user,
                        error="Job cancelled by user",
                        failed_at=datetime.now(),  # Using failed_at for cancelled timestamp
                    )
        except Exception as e:
            logger.error(f"Error cancelling Temporal workflow: {e}")
            raise HTTPException(status_code=500, detail="Failed to cancel workflow") from None
    else:
        # For non-Temporal jobs, directly update the status
        # (though background tasks can't really be cancelled)
        await sync_job_service.update_status(
            sync_job_id=job_id,
            status=SyncJobStatus.CANCELLED,
            current_user=user,
            error="Job cancelled by user",
            failed_at=datetime.now(),  # Using failed_at for cancelled timestamp
        )

    # Fetch the updated job
    return await source_connection_service.get_source_connection_job(
        db=db, source_connection_id=source_connection_id, job_id=job_id, current_user=user
    )
Cancel a running sync job for a source connection.

    This will send a cancellation signal to the Temporal workflow if enabled.
    The workflow will handle the cancellation and update the job status to CANCELLED.

    Args:
        db: The database session
        source_connection_id: The ID of the source connection
        job_id: The ID of the sync job to cancel
        user: The current user

    Returns:
        The cancelled sync job
    
backend/airweave/api/v1/endpoints/source_connections.py