Keyword write-back and the file claim
A Tag edit (api/views/tags.py: add, remove, rename, delete, merge) commits the links, then
api.metadata.edits.tags_changed queues one write_photo_keywords task for the names it touched. The
task runs on any worker, and api/metadata/photo_writer.py write_keywords writes, for each photo,
whether it has each of those names as a tag now.
One claim for every write to a file
ExifTool writes a file by creating <name>_exiftool_tmp next to it, writing the whole file anew there and
renaming it over the original. A second ExifTool that starts on the same file meanwhile fails with
Error creating file (checked with ExifTool 13.59). Every write to a photo's file or sidecar therefore
takes one per-photo claim, Photo.file_writer (a token) and Photo.file_writer_until, through one
conditional UPDATE (api/metadata/file_claim.py): the rating (_write_rating), the keywords, the date,
the caption, the location, the rights, a rotation, the face regions, write_photo_fields and Write
metadata to files (FIELD_WRITERS). Until #3188 ratings and keywords had a claim each, and a keyword
write that overlapped a rating, caption, location or rights write of the same file was logged and lost.
- The claim is free when it has no token or its time has passed, by the database's clock. Its lease is
ExifTool's write timeout for the target file plus two minutes (
file_write_lease), renewed before each write, so a write cannot land after the claim could have been taken over. - A write that finds the claim taken queues itself again for when the claim ends (
enqueue_task_at) with a per-photo key<kind>:<photo id>:<time>, so however many jobs find the same claim, one task waits. The time is the claim's end, but at least a second from now (retry_time): the queue runs a task once the worker's clock reaches its time, while the claim ends by the database's clock (SQLite keeps milliseconds, a PostgreSQL server's clock can lag), so a retry can run and still find its claim held. Queued again under the same key, it would collide with its own open task and be dropped, and a write without a pending marker would be lost. A field write (date, caption, location, rights, rotation, faces) queueswrite_photo_fieldsfor its field, which writes the value the photo has when it runs. - A field write whose claim was taken over by the time it releases it queues itself again too: its write may have landed after the new holder's.
- No database transaction is held across ExifTool: on SQLite it would block every writer of the app for the length of the write.
The rating keeps its counters and the keywords get their own pending marker, so the hourly sweeps can
retry them (write_pending_ratings, write_pending_keywords, scheduled by
start_job_cleanup_service). A rating job that finds the file claimed marks the rating pending before
it queues its retry: the holder may be a keyword or caption write, which does not write the rating.
The other fields have no pending marker: a write ExifTool refuses for another reason (a read-only file)
is logged, as before. Only a write that waits for an external drive leaves a row and is repeated
(Nothing is written to another drive).
Pending keywords
write_keywords first adds the names to Photo.unwritten_keywords and bumps Photo.keyword_edits, in
one transaction that locks the photo row (keyword_pending.mark_keywords_unwritten; BEGIN IMMEDIATE on
SQLite). Then it takes the file claim and writes every pending name (write_unwritten_keywords):
- read the pending names and
keyword_edits, and which of the names the photo has as tags (_split_by_tags); - read the file's keyword lists and have ExifTool write the file anew;
- read the file back and change
PhotoMetadata.keywordsonly for the names it wrote (_record_written_keywords), in one transaction fenced by the claim token. A keyword another app added meanwhile stays unknown, so the next rescan makes it a tag; UPDATE ... SET unwritten_keywords = NULL WHERE keyword_edits = <read> AND file_writer = <token>(record_keyword_write). When a tag edit came meanwhile, the holder goes back to 1, at most three times, and then queuesretry_keyword_writes. When the claim is gone, its write may have landed after the new holder's, so it marks its names unwritten again and a retry writes them.
Because the names wait in the database, a retry carries no names, and the per-photo key of
retry_keyword_writes is safe: two select-all keyword edits on the same photos queue one waiting task
per photo, and the holder of the claim writes the names of both. A write that fails counts in
keyword_write_failures, and the sweep leaves a photo alone after five failures in a row until its next
tag edit. A retry for an owner who turned Write edits to files Off ends the pending write.
Not released yet (comes after 1.7.0) An external drive that is not connected, or another drive under its letter, does
not count as a failure: the names stay pending, and the sweep leaves the photo out until the drive is back
(see Nothing is written to another drive).
Rescans
A rescan (api/metadata/photo_reader.py extract_exif_data) of a photo whose owner writes to files
takes the same claim with a short lease, FILE_READ_LEASE (one minute), and renews it before it applies
what it read. When a write holds the claim, or the lease ended before the apply, the rescan imports no
keyword as a tag and leaves PhotoMetadata.keywords as it was; the next rescan reads them again. A
rescan of a photo whose owner does not write to files takes no claim at all, so it costs no extra
UPDATE. Writes that find the claim held by a rescan wait for its short lease, not for a write timeout.
The claim columns, the pending keywords and their counters are in LIFECYCLE_FIELDS, so a full-row
Photo.save() of an instance loaded earlier never writes them back.
The models are verification/metadata/FileClaim.tla (two kinds of writers on one file),
verification/metadata/RatingWriteBack.tla and verification/organize/KeywordWriteBack.tla.