Skip to content

feat(scripts): find and remove papers whose file is gone - #572

Open
Dx-Alz-xD wants to merge 2 commits into
CodeChefVIT:stagingfrom
Dx-Alz-xD:krish/remove-ghost-papers
Open

Dx-Alz-xD wants to merge 2 commits into
CodeChefVIT:stagingfrom
Dx-Alz-xD:krish/remove-ghost-papers

Conversation

@Dx-Alz-xD

Copy link
Copy Markdown
Contributor

closes #545

adds scripts/remove-ghost-papers.mjs for removing the records whose pdf no longer exists in the bucket

usage, with MONGODB_URI in .env:

  • npm run ghost-papers lists the ghost records and deletes nothing
  • npm run ghost-papers -- --delete removes them

how it decides

  • sends a HEAD request to file_url for every record in papers and admins (the upload queue), same as the viewer check from fix: Resolve bulk downloads, PostHog crash, and missing file UX #544
  • only a 404 counts as missing. 403, 5xx, timeouts, unreachable hosts and records without a file_url are listed as "could not be checked" and never deleted

safety

  • dry run unless --delete is passed
  • before deleting it writes the full records to ghost-papers-.json (gitignored) so they can be restored
  • deletes by _id and file_url together, so a record that was re-uploaded in the meantime is kept
  • refuses to delete when more than half the files are missing, since a moved or renamed bucket also answers 404

tested against an in-memory mongodb and a local file server: the dry run changed nothing, --delete removed exactly the 404 records from both collections and kept the 403/500/unreachable/no-url ones, the backup had the full records, a second run found nothing, and the more-than-half guard refused with all files missing. also checked that storage.googleapis.com answers 404 for a missing object in a public bucket

notes

  • --env-file needs node 20.6 or newer
  • only file_url is checked, a missing thumbnail alone doesn't count

adds scripts/remove-ghost-papers.mjs, run with npm run ghost-papers. it
sends a HEAD request to the file_url of every record in papers and admins,
the same check the viewer does, and lists the ones that answer 404.
anything else (403, 5xx, timeouts, no url) is left alone

it is a dry run unless --delete is passed. with --delete it saves the full
records to ghost-papers-<time>.json first, then deletes them, and only if
their file_url has not changed since the check. it refuses to delete when
more than half the files are missing, since a moved or renamed bucket
also answers 404

refs CodeChefVIT#545
@Rithish-2914

Copy link
Copy Markdown
Contributor

Hi @Dx-Alz-xD , ran a dry run on a staging copy (1,913 papers), found 37 real 404s, nice! Two things: the papers collection name is hardcoded (this DB uses paper), so a --collection option would help; and one record has a malformed URL (a Cloudinary link appended to the GCS path) that 404s even though the Cloudinary file exists, so it would get deleted wrongly. Maybe list malformed URLs separately instead of treating them as missing? Thanks!

the script looked in papers, but mongoose treats "paper" as uncountable,
so the Paper model's collection is paper. it now checks paper and admins
by default, and --collection <name>, which can be given more than once,
picks other collections. it stops and lists the database's collections
when one it was given does not exist, instead of checking an empty one

a file_url with a second link inside it, like a cloudinary url appended
to the gcs path, answers 404 even when the file exists. those are now
listed on their own as malformed and are never deleted
@Dx-Alz-xD

Copy link
Copy Markdown
Contributor Author

Hi @Dx-Alz-xD , ran a dry run on a staging copy (1,913 papers), found 37 real 404s, nice! Two things: the papers collection name is hardcoded (this DB uses paper), so a --collection option would help; and one record has a malformed URL (a Cloudinary link appended to the GCS path) that 404s even though the Cloudinary file exists, so it would get deleted wrongly. Maybe list malformed URLs separately instead of treating them as missing? Thanks!

hi thanks for running it on staging! both are in 93cc0dc

the default was wrong from the start: mongoose treats "paper" as uncountable, so the Paper model's collection is paper, not papers. it now checks paper and admins by default. --collection , which can be given more than once, picks others. if a collection it was given doesn't exist, it stops and lists the ones that do, instead of quietly checking an empty one
a file_url with a second link inside it, plain or url-encoded, is now listed on its own as malformed and is never deleted, so that cloudinary one stays
tested against a local mongo with real 404s, nested urls and a missing collection

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove "ghost" papers from MongoDB where GCS file is deleted

2 participants