"""Durable shared upload queue, one commit for many fully verified samples.""" import fcntl import json import os from pathlib import Path import re import time from email.utils import parsedate_to_datetime from common import REPO,REV,atomic,sha DEFAULT_ROOT=Path('/opt/styleedit-root-20261025/batch-upload') FILES=['source.mp4','edited.mp4','depth.mp4','depth.json','filter.json'] class CommitDeferred(Exception): def __init__(self,until): self.until=until super().__init__('HF commit deferred until '+time.strftime('%Y-%m-%dT%H:%M:%SZ',time.gmtime(until))) def retry_delay(error,now): response=getattr(error,'response',None) if getattr(response,'status_code',None)!=429:return None headers=getattr(response,'headers',{}) retry=headers.get('Retry-After') or headers.get('retry-after') if retry: try:return max(1,float(retry)) except ValueError: try:return max(1,parsedate_to_datetime(retry).timestamp()-now) except (ValueError,TypeError):pass match=re.search(r'in (?:about )?(\d+)\s*(second|minute|hour)',str(error),re.I) if match:return int(match[1])*{'second':1,'minute':60,'hour':3600}[match[2].lower()] return 3600 def guarded_commit(root,action,clock=time.time): """Media and metadata publishers share a durable 60-second commit budget.""" root=Path(root);root.mkdir(parents=True,exist_ok=True) with (root/'commit.lock').open('a') as lock: try:fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB) except BlockingIOError:raise CommitDeferred(clock()+5) path=root/'commit-gate.json';state=json.loads(path.read_text()) if path.exists() else {} now=clock();until=state.get('not_before',0) if now