MCPcopy Create free account
hub / github.com/Roy3838/Observer / update_json

Function update_json

api/r2_store.py:166–194  ·  view source on GitHub ↗

Read-modify-write a JSON object under an ETag precondition. `mutator` receives the current record and mutates it in place. It is called again on each retry with freshly read data, so any guard it enforces (seat limits, duplicate members) is re-checked against the winning version ra

(key: str, mutator: Callable[[dict], Any], retries: int = 5)

Source from the content-addressed store, hash-verified

164
165
166async def update_json(key: str, mutator: Callable[[dict], Any], retries: int = 5) -> dict:
167 """
168 Read-modify-write a JSON object under an ETag precondition.
169
170 `mutator` receives the current record and mutates it in place. It is called
171 again on each retry with freshly read data, so any guard it enforces (seat
172 limits, duplicate members) is re-checked against the winning version rather
173 than the stale one we lost the race to.
174
175 Raises FileNotFoundError if the key does not exist, PreconditionFailed if
176 every attempt lost the race.
177 """
178 for attempt in range(retries):
179 record, etag = await get_json(key)
180 if record is None:
181 raise FileNotFoundError(key)
182
183 mutator(record)
184
185 try:
186 await put_json(key, record, if_match=etag)
187 return record
188 except PreconditionFailed:
189 if attempt == retries - 1:
190 raise
191 logger.warning(f"Lost write race on {key}, retrying ({attempt + 1}/{retries})")
192 await asyncio.sleep(0.1 * (attempt + 1))
193
194 raise PreconditionFailed(key)
195
196
197def org_key(org_id: str) -> str:

Callers

nothing calls this directly

Calls 4

get_jsonFunction · 0.85
mutatorFunction · 0.85
put_jsonFunction · 0.85
PreconditionFailedClass · 0.85

Tested by

no test coverage detected