-
-
Notifications
You must be signed in to change notification settings - Fork 669
feat: stop PictoPy's own file writes from triggering a folder resync #1482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rohan-pandeyy
wants to merge
3
commits into
AOSSIE-Org:main
Choose a base branch
from
rohan-pandeyy:fix/watcher-self-write-echo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| """ | ||
| A short-lived record of files PictoPy modified itself. | ||
|
|
||
| The sync microservice watches every registered folder and treats any file change | ||
| as a user edit, so a write of our own comes straight back as a full folder | ||
| resync. Recording what we wrote lets the watcher tell the two apart. | ||
|
|
||
| The path format here is a contract with `sync-microservice/app/database/ | ||
| self_writes.py`, which reads this table: both sides key on | ||
| `os.path.normcase(os.path.abspath(path))`. | ||
| """ | ||
|
|
||
| import os | ||
| import sqlite3 | ||
| import time | ||
| from typing import List, Set, Tuple | ||
|
|
||
| from app.config.settings import DATABASE_PATH | ||
| from app.logging.setup_logging import get_logger | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
| # An entry the watcher never claims is one it missed -- it was stopped, or the | ||
| # change was coalesced away. Expiring them keeps the table bounded and limits | ||
| # how long a stale row can mask a real edit to the same path. | ||
| SELF_WRITE_TTL_SECONDS = 3600 | ||
|
|
||
| # (path, file_size, file_mtime) as observed on disk right now. | ||
| ObservedFile = Tuple[str, int, int] | ||
|
|
||
|
|
||
| def self_write_key(path: str) -> str: | ||
| """Normalise a path to the form both services store and look up by.""" | ||
| return os.path.normcase(os.path.abspath(path)) | ||
|
|
||
|
|
||
| def _connect() -> sqlite3.Connection: | ||
| conn = sqlite3.connect(DATABASE_PATH) | ||
| # This table stands alone, keyed by path, so nothing here depends on it | ||
| # today. Set anyway to match every other _connect() in this package. | ||
| conn.execute("PRAGMA foreign_keys = ON") | ||
| return conn | ||
|
|
||
|
|
||
| def db_create_self_writes_table() -> None: | ||
| conn = _connect() | ||
| cursor = conn.cursor() | ||
| try: | ||
| cursor.execute( | ||
| """ | ||
| CREATE TABLE IF NOT EXISTS self_writes ( | ||
| path TEXT PRIMARY KEY, | ||
| file_size INTEGER NOT NULL, | ||
| file_mtime INTEGER NOT NULL, | ||
| -- Epoch seconds rather than CURRENT_TIMESTAMP: this column only | ||
| -- exists to be subtracted from, and text timestamps make that | ||
| -- arithmetic a timezone question. | ||
| written_at INTEGER NOT NULL | ||
| ) | ||
| """ | ||
| ) | ||
| conn.commit() | ||
| finally: | ||
| conn.close() | ||
|
|
||
|
|
||
| def db_record_self_write(path: str, file_size: int, file_mtime: int) -> bool: | ||
| """ | ||
| Note that PictoPy is about to leave a file in this exact state. | ||
|
|
||
| Callers record before the bytes land, because the watcher can fire the | ||
| instant they do. | ||
| """ | ||
| conn = _connect() | ||
| cursor = conn.cursor() | ||
| now = int(time.time()) | ||
|
|
||
| try: | ||
| cursor.execute( | ||
| """ | ||
| INSERT INTO self_writes (path, file_size, file_mtime, written_at) | ||
| VALUES (?, ?, ?, ?) | ||
| ON CONFLICT(path) DO UPDATE SET | ||
| file_size=excluded.file_size, | ||
| file_mtime=excluded.file_mtime, | ||
| written_at=excluded.written_at | ||
| """, | ||
| (self_write_key(path), file_size, file_mtime, now), | ||
| ) | ||
| # Pruning here avoids needing a scheduler for a table this small. | ||
| cursor.execute( | ||
| "DELETE FROM self_writes WHERE written_at < ?", | ||
| (now - SELF_WRITE_TTL_SECONDS,), | ||
| ) | ||
| conn.commit() | ||
| return True | ||
| except sqlite3.Error as e: | ||
| logger.error(f"Error recording self write for {path}: {e}") | ||
| conn.rollback() | ||
| return False | ||
| finally: | ||
| conn.close() | ||
|
|
||
|
|
||
| def db_take_matching_self_writes(observed: List[ObservedFile]) -> Set[str]: | ||
| """ | ||
| Return the observed paths that match a recorded write, and forget them. | ||
|
|
||
| Claiming an entry as it matches means a second event for the same write is | ||
| treated as a real change. That is the safe direction to be wrong in: it | ||
| costs one redundant rescan, where holding the entry could swallow a genuine | ||
| edit that happened to land in the same second at the same size. | ||
| """ | ||
| if not observed: | ||
| return set() | ||
|
|
||
| conn = _connect() | ||
| cursor = conn.cursor() | ||
| matched: Set[str] = set() | ||
|
|
||
| try: | ||
| by_key = {self_write_key(path): path for path, _, _ in observed} | ||
| placeholders = ",".join("?" for _ in observed) | ||
| cursor.execute( | ||
| f""" | ||
| SELECT path, file_size, file_mtime | ||
| FROM self_writes | ||
| WHERE path IN ({placeholders}) | ||
| """, | ||
| list(by_key), | ||
| ) | ||
| recorded = {row[0]: (row[1], row[2]) for row in cursor.fetchall()} | ||
|
|
||
| claimed = [] | ||
| for path, size, mtime in observed: | ||
| key = self_write_key(path) | ||
| if recorded.get(key) == (size, mtime): | ||
| matched.add(path) | ||
| claimed.append(key) | ||
|
|
||
| if claimed: | ||
| cursor.executemany( | ||
| "DELETE FROM self_writes WHERE path = ?", | ||
| [(key,) for key in claimed], | ||
| ) | ||
| conn.commit() | ||
|
|
||
| return matched | ||
| except sqlite3.Error as e: | ||
| # Failing open means a redundant rescan, never a missed change. | ||
| logger.error(f"Error matching self writes: {e}") | ||
| return set() | ||
| finally: | ||
| conn.close() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| """ | ||
| Replacing a file in a watched folder without the change reading as a user edit. | ||
|
|
||
| Anything that rewrites a file inside a registered folder should go through here | ||
| rather than opening the path directly. | ||
| """ | ||
|
|
||
| import os | ||
| import tempfile | ||
| from typing import Optional | ||
|
|
||
| from app.database.self_writes import db_record_self_write | ||
| from app.logging.setup_logging import get_logger | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
| # The temp file lands in the watched folder too, so the watcher recognises and | ||
| # ignores it by name. Mirrored in sync-microservice/app/utils/watcher.py. | ||
| SELF_WRITE_TEMP_PREFIX = ".pictopy-write-" | ||
|
|
||
|
|
||
| def _discard(temp_path: Optional[str]) -> None: | ||
| """Best-effort cleanup; a leftover temp is noise, not a failure worth raising.""" | ||
| if not temp_path: | ||
| return | ||
| try: | ||
| os.unlink(temp_path) | ||
| except OSError: | ||
| logger.warning(f"Could not remove temporary file {temp_path}") | ||
|
|
||
|
|
||
| def self_write_util_replace(path: str, data: bytes) -> bool: | ||
| """ | ||
| Atomically replace a file's contents and record the write for the watcher. | ||
|
|
||
| Writes to a sibling temp file, records the size and mtime that file already | ||
| has, then renames it into place. The rename carries both across unchanged, | ||
| so the ledger row is in the database before the new bytes are visible at the | ||
| watched path -- which matters, because the watcher can fire the moment they | ||
| are. | ||
|
|
||
| Returns False if the file could not be replaced, leaving the original as is. | ||
| """ | ||
| target = os.path.abspath(path) | ||
| directory = os.path.dirname(target) | ||
| temp_path: Optional[str] = None | ||
|
|
||
| try: | ||
| # Same directory, so the rename stays on one filesystem and stays atomic. | ||
| handle_fd, temp_path = tempfile.mkstemp( | ||
| dir=directory, prefix=SELF_WRITE_TEMP_PREFIX, suffix=".tmp" | ||
| ) | ||
| with os.fdopen(handle_fd, "wb") as handle: | ||
| handle.write(data) | ||
| handle.flush() | ||
| # Without this the rename can land before the bytes do, which on a | ||
| # crash leaves a photo that is neither the old one nor the new one. | ||
| os.fsync(handle.fileno()) | ||
|
|
||
| stats = os.stat(temp_path) | ||
| db_record_self_write(target, stats.st_size, int(stats.st_mtime)) | ||
|
|
||
| os.replace(temp_path, target) | ||
| return True | ||
| except OSError as e: | ||
| # A locked or read-only file is ordinary on Windows; the caller retries | ||
| # on a later pass rather than treating it as a failed run. | ||
| logger.error(f"Could not replace {path}: {e}") | ||
| _discard(temp_path) | ||
| return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.