|
24 | 24 | import math |
25 | 25 | import random |
26 | 26 | import uuid |
27 | | -from collections.abc import Callable, Iterable, Mapping |
| 27 | +from collections.abc import Callable, Iterable, Mapping, Sequence |
28 | 28 | from dataclasses import dataclass, field |
29 | 29 | from datetime import datetime, timezone |
30 | 30 | from typing import Any |
@@ -685,6 +685,177 @@ def to_server_command( |
685 | 685 | ) |
686 | 686 |
|
687 | 687 |
|
| 688 | +def commands_to_server_commands( |
| 689 | + commands: Sequence[Command], |
| 690 | + task_queue: str, |
| 691 | + *, |
| 692 | + payload_codec: str = serializer.AVRO_CODEC, |
| 693 | + size_warning: serializer.PayloadSizeWarningConfig | None = serializer.DEFAULT_PAYLOAD_SIZE_WARNING, |
| 694 | + warning_context: PayloadWarningContext = None, |
| 695 | +) -> list[dict[str, Any]]: |
| 696 | + """Convert workflow commands to the server wire shape with batched payload encoding.""" |
| 697 | + server_commands: list[dict[str, Any]] = [] |
| 698 | + envelope_jobs: list[tuple[int, str, Any, dict[str, str]]] = [] |
| 699 | + encode_jobs: list[tuple[int, str, Any, dict[str, str]]] = [] |
| 700 | + |
| 701 | + for command in commands: |
| 702 | + if isinstance(command, ScheduleActivity): |
| 703 | + queue = command.queue or task_queue |
| 704 | + server_command: dict[str, Any] = { |
| 705 | + "type": "schedule_activity", |
| 706 | + "activity_type": command.activity_type, |
| 707 | + "queue": queue, |
| 708 | + } |
| 709 | + envelope_jobs.append(( |
| 710 | + len(server_commands), |
| 711 | + "arguments", |
| 712 | + command.arguments, |
| 713 | + _payload_warning_context( |
| 714 | + warning_context, |
| 715 | + kind="activity_input", |
| 716 | + task_queue=queue, |
| 717 | + activity_name=command.activity_type, |
| 718 | + ), |
| 719 | + )) |
| 720 | + if command.retry_policy is not None: |
| 721 | + server_command["retry_policy"] = ( |
| 722 | + command.retry_policy.to_dict() |
| 723 | + if isinstance(command.retry_policy, ActivityRetryPolicy) |
| 724 | + else dict(command.retry_policy) |
| 725 | + ) |
| 726 | + if command.start_to_close_timeout is not None: |
| 727 | + server_command["start_to_close_timeout"] = command.start_to_close_timeout |
| 728 | + if command.schedule_to_start_timeout is not None: |
| 729 | + server_command["schedule_to_start_timeout"] = command.schedule_to_start_timeout |
| 730 | + if command.schedule_to_close_timeout is not None: |
| 731 | + server_command["schedule_to_close_timeout"] = command.schedule_to_close_timeout |
| 732 | + if command.heartbeat_timeout is not None: |
| 733 | + server_command["heartbeat_timeout"] = command.heartbeat_timeout |
| 734 | + server_commands.append(server_command) |
| 735 | + continue |
| 736 | + |
| 737 | + if isinstance(command, CompleteWorkflow): |
| 738 | + server_commands.append({"type": "complete_workflow"}) |
| 739 | + envelope_jobs.append(( |
| 740 | + len(server_commands) - 1, |
| 741 | + "result", |
| 742 | + command.result, |
| 743 | + _payload_warning_context( |
| 744 | + warning_context, |
| 745 | + kind="workflow_result", |
| 746 | + task_queue=task_queue, |
| 747 | + ), |
| 748 | + )) |
| 749 | + continue |
| 750 | + |
| 751 | + if isinstance(command, CompleteUpdate): |
| 752 | + server_commands.append({"type": "complete_update", "update_id": command.update_id}) |
| 753 | + envelope_jobs.append(( |
| 754 | + len(server_commands) - 1, |
| 755 | + "result", |
| 756 | + command.result, |
| 757 | + _payload_warning_context( |
| 758 | + warning_context, |
| 759 | + kind="update_result", |
| 760 | + task_queue=task_queue, |
| 761 | + ), |
| 762 | + )) |
| 763 | + continue |
| 764 | + |
| 765 | + if isinstance(command, ContinueAsNew): |
| 766 | + queue = command.task_queue or task_queue |
| 767 | + server_command = {"type": "continue_as_new", "queue": queue} |
| 768 | + if command.workflow_type is not None: |
| 769 | + server_command["workflow_type"] = command.workflow_type |
| 770 | + server_commands.append(server_command) |
| 771 | + envelope_jobs.append(( |
| 772 | + len(server_commands) - 1, |
| 773 | + "arguments", |
| 774 | + command.arguments, |
| 775 | + _payload_warning_context( |
| 776 | + warning_context, |
| 777 | + kind="continue_as_new_input", |
| 778 | + task_queue=queue, |
| 779 | + ), |
| 780 | + )) |
| 781 | + continue |
| 782 | + |
| 783 | + if isinstance(command, RecordSideEffect): |
| 784 | + server_commands.append({"type": "record_side_effect"}) |
| 785 | + encode_jobs.append(( |
| 786 | + len(server_commands) - 1, |
| 787 | + "result", |
| 788 | + command.result, |
| 789 | + _payload_warning_context( |
| 790 | + warning_context, |
| 791 | + kind="side_effect_result", |
| 792 | + task_queue=task_queue, |
| 793 | + ), |
| 794 | + )) |
| 795 | + continue |
| 796 | + |
| 797 | + if isinstance(command, StartChildWorkflow): |
| 798 | + queue = command.task_queue or task_queue |
| 799 | + server_command = { |
| 800 | + "type": "start_child_workflow", |
| 801 | + "workflow_type": command.workflow_type, |
| 802 | + "queue": queue, |
| 803 | + } |
| 804 | + envelope_jobs.append(( |
| 805 | + len(server_commands), |
| 806 | + "arguments", |
| 807 | + command.arguments, |
| 808 | + _payload_warning_context( |
| 809 | + warning_context, |
| 810 | + kind="child_workflow_input", |
| 811 | + task_queue=queue, |
| 812 | + ), |
| 813 | + )) |
| 814 | + if command.parent_close_policy is not None: |
| 815 | + server_command["parent_close_policy"] = command.parent_close_policy |
| 816 | + if command.retry_policy is not None: |
| 817 | + server_command["retry_policy"] = ( |
| 818 | + command.retry_policy.to_dict() |
| 819 | + if isinstance(command.retry_policy, ActivityRetryPolicy) |
| 820 | + else dict(command.retry_policy) |
| 821 | + ) |
| 822 | + if command.execution_timeout_seconds is not None: |
| 823 | + server_command["execution_timeout_seconds"] = command.execution_timeout_seconds |
| 824 | + if command.run_timeout_seconds is not None: |
| 825 | + server_command["run_timeout_seconds"] = command.run_timeout_seconds |
| 826 | + server_commands.append(server_command) |
| 827 | + continue |
| 828 | + |
| 829 | + server_commands.append(command.to_server_command( |
| 830 | + task_queue, |
| 831 | + payload_codec=payload_codec, |
| 832 | + size_warning=size_warning, |
| 833 | + warning_context=warning_context, |
| 834 | + )) |
| 835 | + |
| 836 | + if envelope_jobs: |
| 837 | + envelopes = serializer.envelope_many( |
| 838 | + [value for _, _, value, _ in envelope_jobs], |
| 839 | + codec=payload_codec, |
| 840 | + size_warning=size_warning, |
| 841 | + warning_context=[context for _, _, _, context in envelope_jobs], |
| 842 | + ) |
| 843 | + for (index, key, _, _), envelope_value in zip(envelope_jobs, envelopes, strict=True): |
| 844 | + server_commands[index][key] = envelope_value |
| 845 | + |
| 846 | + if encode_jobs: |
| 847 | + blobs = serializer.encode_many( |
| 848 | + [value for _, _, value, _ in encode_jobs], |
| 849 | + codec=payload_codec, |
| 850 | + size_warning=size_warning, |
| 851 | + warning_context=[context for _, _, _, context in encode_jobs], |
| 852 | + ) |
| 853 | + for (index, key, _, _), blob in zip(encode_jobs, blobs, strict=True): |
| 854 | + server_commands[index][key] = blob |
| 855 | + |
| 856 | + return server_commands |
| 857 | + |
| 858 | + |
688 | 859 | # ── Context passed to the workflow's run() ─────────────────────────── |
689 | 860 |
|
690 | 861 | _REPLAY_LOGGER = logging.getLogger("durable_workflow.workflow.replay") |
|
0 commit comments