|
4 | 4 | from azure.functions._durable_functions import _deserialize_custom_object
|
5 | 5 | from datetime import datetime
|
6 | 6 | from typing import List, Optional
|
| 7 | +from ..models.actions.Action import Action |
| 8 | +from ..models.Task import Task |
7 | 9 |
|
8 | 10 |
|
9 | 11 | def should_suspend(partial_result) -> bool:
|
@@ -410,3 +412,95 @@ def should_preserve(event: HistoryEvent) -> bool:
|
410 | 412 | # We should try to refactor this logic at some point
|
411 | 413 | event = matches[0]
|
412 | 414 | return event
|
| 415 | + |
| 416 | + |
| 417 | +def get_retried_task( |
| 418 | + state: List[HistoryEvent], max_number_of_attempts: int, scheduled_type: HistoryEventType, |
| 419 | + completed_type: HistoryEventType, failed_type: HistoryEventType, action: Action) -> Task: |
| 420 | + """Determine the state of scheduling some task for execution with retry options. |
| 421 | +
|
| 422 | + Parameters |
| 423 | + ---------- |
| 424 | + state: List[HistoryEvent] |
| 425 | + The list of history events |
| 426 | + max_number_of_ints: int |
| 427 | + The maximum number of retrying attempts |
| 428 | + scheduled_type: HistoryEventType |
| 429 | + The event type corresponding to scheduling the searched-for task |
| 430 | + completed_type: HistoryEventType |
| 431 | + The event type corresponding to a completion of the searched-for task |
| 432 | + failed_type: HistoryEventType |
| 433 | + The event type coresponding to the failure of the searched-for task |
| 434 | + action: Action |
| 435 | + The action corresponding to the searched-for task |
| 436 | +
|
| 437 | + Returns |
| 438 | + ------- |
| 439 | + Task |
| 440 | + A Task encompassing the state of the scheduled work item, that is, |
| 441 | + either completed, failed, or incomplete. |
| 442 | + """ |
| 443 | + # tasks to look for in the state array |
| 444 | + scheduled_task, completed_task = None, None |
| 445 | + failed_task, scheduled_timer_task = None, None |
| 446 | + attempt = 1 |
| 447 | + |
| 448 | + # Note each case below is exclusive, and the order matters |
| 449 | + for event in state: |
| 450 | + event_type = HistoryEventType(event.event_type) |
| 451 | + |
| 452 | + # Skip processed events |
| 453 | + if event.is_processed: |
| 454 | + continue |
| 455 | + |
| 456 | + # first we find the scheduled_task |
| 457 | + elif scheduled_task is None: |
| 458 | + if event_type is scheduled_type: |
| 459 | + scheduled_task = event |
| 460 | + |
| 461 | + # if the task has a correponding completion, we process the events |
| 462 | + # and return a completed task |
| 463 | + elif event_type == completed_type and \ |
| 464 | + event.TaskScheduledId == scheduled_task.event_id: |
| 465 | + completed_task = event |
| 466 | + set_processed([scheduled_task, completed_task]) |
| 467 | + return Task( |
| 468 | + is_completed=True, |
| 469 | + is_faulted=False, |
| 470 | + action=action, |
| 471 | + result=parse_history_event(completed_task), |
| 472 | + timestamp=completed_task.timestamp, |
| 473 | + id_=completed_task.TaskScheduledId |
| 474 | + ) |
| 475 | + |
| 476 | + # if its failed, we'll have to wait for an upcoming timer scheduled |
| 477 | + elif failed_task is None: |
| 478 | + if event_type is failed_type: |
| 479 | + if event.TaskScheduledId == scheduled_task.event_id: |
| 480 | + failed_task = event |
| 481 | + |
| 482 | + # if we have a timer scheduled, we'll have to find a timer fired |
| 483 | + elif scheduled_timer_task is None: |
| 484 | + if event_type is HistoryEventType.TIMER_CREATED: |
| 485 | + scheduled_timer_task = event |
| 486 | + |
| 487 | + # if we have a timer fired, we check if we still have more attempts for retries. |
| 488 | + # If so, we retry again and clear our found events so far. |
| 489 | + # If not, we process the events and return a completed task |
| 490 | + elif event_type is HistoryEventType.TIMER_FIRED: |
| 491 | + if event.TimerId == scheduled_timer_task.event_id: |
| 492 | + set_processed([scheduled_task, completed_task, failed_task, scheduled_timer_task]) |
| 493 | + if attempt >= max_number_of_attempts: |
| 494 | + return Task( |
| 495 | + is_completed=True, |
| 496 | + is_faulted=True, |
| 497 | + action=action, |
| 498 | + timestamp=failed_task.timestamp, |
| 499 | + id_=failed_task.TaskScheduledId, |
| 500 | + exc=Exception( |
| 501 | + f"{failed_task.Reason} \n {failed_task.Details}") |
| 502 | + ) |
| 503 | + else: |
| 504 | + scheduled_task, failed_task, scheduled_timer_task = None, None, None |
| 505 | + attempt += 1 |
| 506 | + return Task(is_completed=False, is_faulted=False, action=action) |
0 commit comments