| 138 | |
| 139 | |
| 140 | def claim_suggestions( |
| 141 | paths: Paths, |
| 142 | batch_size: int, |
| 143 | max_attempts: int = 3, |
| 144 | stale_after_seconds: int = DEFAULT_LOCK_STALE_SECONDS, |
| 145 | ) -> list[tuple[Path, SuggestionEnvelope]]: |
| 146 | claimed: list[tuple[Path, SuggestionEnvelope]] = [] |
| 147 | candidates = list_suggestions(paths, "pending") |
| 148 | retryable_failed = [] |
| 149 | for failed_path in list_suggestions(paths, "failed"): |
| 150 | envelope = SuggestionEnvelope.from_dict(load_json(failed_path)) |
| 151 | if envelope.attempt_count < max_attempts: |
| 152 | retryable_failed.append(failed_path) |
| 153 | retryable_processing = [] |
| 154 | for processing_path in list_stale_processing(paths, stale_after_seconds=stale_after_seconds): |
| 155 | envelope = SuggestionEnvelope.from_dict(load_json(processing_path)) |
| 156 | if envelope.attempt_count < max_attempts: |
| 157 | retryable_processing.append(processing_path) |
| 158 | for source in (candidates + retryable_failed + retryable_processing)[:batch_size]: |
| 159 | envelope = SuggestionEnvelope.from_dict(load_json(source)) |
| 160 | reason = "reclaimed_stale" if source.parent == paths.suggestions_processing_dir else "claimed" |
| 161 | transition = { |
| 162 | "at": utc_now().replace(microsecond=0).isoformat().replace("+00:00", "Z"), |
| 163 | "from": envelope.state, |
| 164 | "to": "processing", |
| 165 | "reason": reason, |
| 166 | } |
| 167 | updated = replace( |
| 168 | envelope, |
| 169 | state="processing", |
| 170 | attempt_count=envelope.attempt_count + 1, |
| 171 | failure_reason=None, |
| 172 | transition_log=[*envelope.transition_log, transition], |
| 173 | ) |
| 174 | destination = move_suggestion(paths, source, updated, "processing") |
| 175 | claimed.append((destination, updated)) |
| 176 | return claimed |
| 177 | |
| 178 | |
| 179 | def finalize_suggestion(paths: Paths, source: Path, envelope: SuggestionEnvelope, state: str, reason: str | None = None) -> Path: |