feat(nextcloud): hard-delete SourceRoot + reliable delete sync

Two related fixes for the Nextcloud library lifecycle.

1. DELETE /api/v1/nextcloud/source-roots/{id} now actually deletes
   the SourceRoot, every Folder under it, and every Photo in those
   folders (Nextcloud files untouched). Was a soft-deactivate
   (is_active=false) that left the rows around forever, so re-adding
   the same path resurrected ghosts and prune-missing reported zero.
   Returns {deleted_photos, deleted_folders}; the Settings UI toasts
   the count and invalidates photos/folders/stats so cached lists
   don't show ghosts. photo_tags and heap_photos already cascade via
   ON DELETE CASCADE; FolderShare uses a stringly-typed folder_id
   with no FK so cleaned up explicitly.

2. The watcher (watch_folders task) was getting killed every five
   minutes by the global task_soft_time_limit=300 in app/tasks/celery.py
   despite passing soft_time_limit=None on the decorator (None falls
   back to the worker default in this Celery version). Override with
   soft_time_limit=0, time_limit=0 (= unlimited) so the watch loop
   actually stays alive. The 'Soft time limit (300s) exceeded' /
   'Worker exited prematurely' lines should stop in worker-watcher
   logs.

3. Added discard_missing_photos() in services/cleanup.py — a soft
   variant of prune_missing_photos that walks every present source
   root, checks os.path.exists for each non-discarded Photo, and
   flips is_discarded=true on the missing ones (UPDATE not DELETE).
   Wired as discard_missing_photos_beat in tasks/scan.py and
   scheduled every 30 min via celery beat. Beat runs in-process on
   worker-watcher (--beat flag in compose) — there's only ever one
   watcher and we don't need a separate container.

Hard delete remains manual via prune-missing for users who want to
review before committing. The beat catch-up only soft-discards (file
gone -> mule-image trash, restorable).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Claudio
2026-05-10 22:19:58 +02:00
parent 99d504842e
commit 09a00f7419
7 changed files with 174 additions and 17 deletions

View File

@@ -1401,9 +1401,18 @@ function NextcloudIntegrationCard() {
const removeRoot = useMutation({
mutationFn: async (id: string) => nextcloudApi.deleteSourceRoot(id),
onSuccess: () => {
toast.success('Nextcloud library removed')
onSuccess: ({ deleted_photos, deleted_folders }) => {
const photos = `${deleted_photos} ${deleted_photos === 1 ? 'photo' : 'photos'}`
const folders = `${deleted_folders} ${deleted_folders === 1 ? 'folder' : 'folders'}`
toast.success(`Removed library — ${photos}, ${folders}. Files in Nextcloud are untouched.`)
queryClient.invalidateQueries({ queryKey: NEXTCLOUD_ROOTS_KEY })
// The list of photos/folders/heaps the rest of the app caches is
// now stale — invalidate everything photo-shaped so the user
// doesn't see ghosts of the removed library until next refresh.
queryClient.invalidateQueries({ queryKey: ['photos'] })
queryClient.invalidateQueries({ queryKey: ['folders'] })
queryClient.invalidateQueries({ queryKey: ['folders', 'tree'] })
queryClient.invalidateQueries({ queryKey: ['library', 'stats'] })
},
onError: (e) => {
const message = e instanceof Error ? e.message : String(e)

View File

@@ -1139,8 +1139,14 @@ export const nextcloud = {
return response.data
},
deleteSourceRoot: async (id: string): Promise<void> => {
await api.delete(`/nextcloud/source-roots/${id}`)
deleteSourceRoot: async (
id: string,
): Promise<{ deleted_photos: number; deleted_folders: number }> => {
const response = await api.delete<{
deleted_photos: number
deleted_folders: number
}>(`/nextcloud/source-roots/${id}`)
return response.data
},
}