|
2 | 2 | import redis |
3 | 3 |
|
4 | 4 | from dataclasses import dataclass |
5 | | -from typing import Optional, Any |
| 5 | +from typing import Optional, Any, List |
6 | 6 | from threading import Lock |
7 | 7 |
|
8 | 8 | from .clock import now_ms |
9 | 9 | from .ids import new_ulid |
10 | | -from .types import ReservePaused, ReserveJob, ReserveResult, AckFailResult |
| 10 | +from .types import ReservePaused, ReserveJob, ReserveResult, AckFailResult, BatchRemoveResult, BatchRetryFailedResult |
11 | 11 | from .transport import RedisLike |
12 | 12 | from .scripts import OmniqScripts |
13 | | -from .helper import queue_base, queue_anchor |
| 13 | +from .helper import queue_base, queue_anchor, check_completion_anchor |
14 | 14 |
|
15 | 15 | @dataclass |
16 | 16 | class OmniqOps: |
@@ -300,6 +300,195 @@ def job_timeout_ms(self, *, queue: str, job_id: str, default_ms: int = 60_000) - |
300 | 300 | except Exception: |
301 | 301 | n = 0 |
302 | 302 | return n if n > 0 else int(default_ms) |
| 303 | + |
| 304 | + def retry_failed(self, *, queue: str, job_id: str, now_ms_override: int = 0) -> None: |
| 305 | + anchor = queue_anchor(queue) |
| 306 | + nms = now_ms_override or now_ms() |
| 307 | + |
| 308 | + res = self._evalsha_with_noscript_fallback( |
| 309 | + self.scripts.retry_failed.sha, |
| 310 | + self.scripts.retry_failed.src, |
| 311 | + 1, |
| 312 | + anchor, |
| 313 | + job_id, |
| 314 | + str(int(nms)), |
| 315 | + ) |
| 316 | + |
| 317 | + if not isinstance(res, list) or len(res) < 1: |
| 318 | + raise RuntimeError(f"Unexpected RETRY_FAILED response: {res}") |
| 319 | + |
| 320 | + if res[0] == "OK": |
| 321 | + return |
| 322 | + |
| 323 | + if res[0] == "ERR": |
| 324 | + reason = str(res[1]) if len(res) > 1 else "UNKNOWN" |
| 325 | + raise RuntimeError(f"RETRY_FAILED failed: {reason}") |
| 326 | + |
| 327 | + raise RuntimeError(f"Unexpected RETRY_FAILED response: {res}") |
| 328 | + |
| 329 | + def retry_failed_batch( |
| 330 | + self, |
| 331 | + *, |
| 332 | + queue: str, |
| 333 | + job_ids: List[str], |
| 334 | + now_ms_override: int = 0, |
| 335 | + ) -> BatchRetryFailedResult: |
| 336 | + if len(job_ids) > 100: |
| 337 | + raise ValueError("retry_failed_batch max is 100 job_ids per call") |
| 338 | + |
| 339 | + anchor = queue_anchor(queue) |
| 340 | + nms = now_ms_override or now_ms() |
| 341 | + |
| 342 | + argv: list[str] = [str(int(nms)), str(len(job_ids))] |
| 343 | + argv.extend([str(j) for j in job_ids]) |
| 344 | + |
| 345 | + res = self._evalsha_with_noscript_fallback( |
| 346 | + self.scripts.retry_failed_batch.sha, |
| 347 | + self.scripts.retry_failed_batch.src, |
| 348 | + 1, |
| 349 | + anchor, |
| 350 | + *argv, |
| 351 | + ) |
| 352 | + |
| 353 | + if not isinstance(res, list): |
| 354 | + raise RuntimeError(f"Unexpected RETRY_FAILED_BATCH response: {res}") |
| 355 | + |
| 356 | + if len(res) >= 2 and str(res[0]) == "ERR": |
| 357 | + reason = str(res[1]) |
| 358 | + extra = str(res[2]) if len(res) > 2 else "" |
| 359 | + raise RuntimeError(f"RETRY_FAILED_BATCH failed: {reason} {extra}".strip()) |
| 360 | + |
| 361 | + out: BatchRetryFailedResult = [] |
| 362 | + i = 0 |
| 363 | + while i < len(res): |
| 364 | + job_id = str(res[i] or "") |
| 365 | + status = str(res[i + 1] or "") |
| 366 | + reason: Optional[str] = None |
| 367 | + if status == "ERR": |
| 368 | + reason = str(res[i + 2] or "UNKNOWN") |
| 369 | + i += 3 |
| 370 | + else: |
| 371 | + i += 2 |
| 372 | + out.append((job_id, status, reason)) |
| 373 | + return out |
| 374 | + |
| 375 | + def remove_job(self, *, queue: str, job_id: str, lane: str) -> str: |
| 376 | + anchor = queue_anchor(queue) |
| 377 | + |
| 378 | + res = self._evalsha_with_noscript_fallback( |
| 379 | + self.scripts.remove_job.sha, |
| 380 | + self.scripts.remove_job.src, |
| 381 | + 1, |
| 382 | + anchor, |
| 383 | + job_id, |
| 384 | + lane, |
| 385 | + ) |
| 386 | + |
| 387 | + if not isinstance(res, list) or len(res) < 1: |
| 388 | + raise RuntimeError(f"Unexpected REMOVE_JOB response: {res}") |
| 389 | + |
| 390 | + if res[0] == "OK": |
| 391 | + return str(res[0] or "") |
| 392 | + |
| 393 | + if res[0] == "ERR": |
| 394 | + reason = str(res[1]) if len(res) > 1 else "UNKNOWN" |
| 395 | + raise RuntimeError(f"REMOVE_JOB failed: {reason}") |
| 396 | + |
| 397 | + raise RuntimeError(f"Unexpected REMOVE_JOB response: {res}") |
| 398 | + |
| 399 | + def remove_jobs_batch( |
| 400 | + self, |
| 401 | + *, |
| 402 | + queue: str, |
| 403 | + lane: str, |
| 404 | + job_ids: List[str], |
| 405 | + ) -> BatchRemoveResult: |
| 406 | + if len(job_ids) > 100: |
| 407 | + raise ValueError("remove_jobs_batch max is 100 job_ids per call") |
| 408 | + |
| 409 | + anchor = queue_anchor(queue) |
| 410 | + |
| 411 | + argv: list[str] = [str(lane), str(len(job_ids))] |
| 412 | + argv.extend([str(j) for j in job_ids]) |
| 413 | + |
| 414 | + res = self._evalsha_with_noscript_fallback( |
| 415 | + self.scripts.remove_jobs_batch.sha, |
| 416 | + self.scripts.remove_jobs_batch.src, |
| 417 | + 1, |
| 418 | + anchor, |
| 419 | + *argv, |
| 420 | + ) |
| 421 | + |
| 422 | + if not isinstance(res, list): |
| 423 | + raise RuntimeError(f"Unexpected REMOVE_JOBS_BATCH response: {res}") |
| 424 | + |
| 425 | + if len(res) >= 2 and str(res[0]) == "ERR": |
| 426 | + reason = str(res[1]) |
| 427 | + extra = str(res[2]) if len(res) > 2 else "" |
| 428 | + raise RuntimeError(f"REMOVE_JOBS_BATCH failed: {reason} {extra}".strip()) |
| 429 | + |
| 430 | + out: BatchRemoveResult = [] |
| 431 | + i = 0 |
| 432 | + while i < len(res): |
| 433 | + job_id = str(res[i] or "") |
| 434 | + status = str(res[i + 1] or "") |
| 435 | + reason: Optional[str] = None |
| 436 | + if status == "ERR": |
| 437 | + reason = str(res[i + 2] or "UNKNOWN") |
| 438 | + i += 3 |
| 439 | + else: |
| 440 | + i += 2 |
| 441 | + out.append((job_id, status, reason)) |
| 442 | + return out |
| 443 | + |
| 444 | + def check_completion_init_job_counter(self, *, key: str, expected: int) -> None: |
| 445 | + anchor = check_completion_anchor(key) |
| 446 | + |
| 447 | + res = self._evalsha_with_noscript_fallback( |
| 448 | + self.scripts.check_completion_init.sha, |
| 449 | + self.scripts.check_completion_init.src, |
| 450 | + 1, |
| 451 | + anchor, |
| 452 | + str(int(expected)), |
| 453 | + ) |
| 454 | + |
| 455 | + if not isinstance(res, list) or len(res) < 1: |
| 456 | + raise RuntimeError(f"Unexpected CHECK_COMPLETION_INIT response: {res}") |
| 457 | + |
| 458 | + if res[0] == "OK": |
| 459 | + return |
| 460 | + |
| 461 | + if res[0] == "ERR": |
| 462 | + reason = str(res[1]) if len(res) > 1 else "UNKNOWN" |
| 463 | + raise RuntimeError(f"CHECK_COMPLETION_INIT failed: {reason}") |
| 464 | + |
| 465 | + raise RuntimeError(f"Unexpected CHECK_COMPLETION_INIT response: {res}") |
| 466 | + |
| 467 | + def check_completion_job_decrement(self, *, key: str, child_id: str) -> int: |
| 468 | + anchor = check_completion_anchor(key) |
| 469 | + |
| 470 | + res = self._evalsha_with_noscript_fallback( |
| 471 | + self.scripts.check_completion_decrement.sha, |
| 472 | + self.scripts.check_completion_decrement.src, |
| 473 | + 1, |
| 474 | + anchor, |
| 475 | + str(child_id), |
| 476 | + ) |
| 477 | + |
| 478 | + if not isinstance(res, list) or len(res) < 2: |
| 479 | + raise RuntimeError(f"Unexpected CHECK_COMPLETION_DECREMENT response: {res}") |
| 480 | + |
| 481 | + if res[0] == "OK": |
| 482 | + try: |
| 483 | + return int(res[1]) |
| 484 | + except Exception: |
| 485 | + raise RuntimeError(f"Unexpected CHECK_COMPLETION_DECREMENT remaining: {res}") |
| 486 | + |
| 487 | + if res[0] == "ERR": |
| 488 | + reason = str(res[1]) if len(res) > 1 else "UNKNOWN" |
| 489 | + raise RuntimeError(f"CHECK_COMPLETION_DECREMENT failed: {reason}") |
| 490 | + |
| 491 | + raise RuntimeError(f"Unexpected CHECK_COMPLETION_DECREMENT response: {res}") |
303 | 492 |
|
304 | 493 | @staticmethod |
305 | 494 | def paused_backoff_s(poll_interval_s: float) -> float: |
|
0 commit comments