|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +"""Compatibility override for the history-export enumeration activity. |
| 5 | +
|
| 6 | +The core durabletask ``list_terminal_instances`` activity enumerates terminal |
| 7 | +instances via the ``ListInstanceIds`` gRPC call. The Azure Functions Durable |
| 8 | +extension's gRPC endpoint does not implement that method (it returns |
| 9 | +``UNIMPLEMENTED``), so this module provides a drop-in replacement that |
| 10 | +enumerates via ``QueryInstances`` (:meth:`get_all_orchestration_states`) |
| 11 | +instead -- a method the extension does implement. |
| 12 | +
|
| 13 | +> [!NOTE] |
| 14 | +> This shim exists only because the Durable Functions host extension does not |
| 15 | +> yet implement ``ListInstanceIds``. Once it does, delete this module and have |
| 16 | +> :meth:`DFApp.configure_history_export` register the core |
| 17 | +> ``durabletask.extensions.history_export.activities.list_terminal_instances`` |
| 18 | +> activity directly. |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +from collections.abc import Mapping |
| 24 | +from typing import Any, Optional |
| 25 | + |
| 26 | +from durabletask import task |
| 27 | +from durabletask.client import OrchestrationQuery, OrchestrationStatus |
| 28 | +from durabletask.internal.helpers import ensure_aware |
| 29 | + |
| 30 | +from durabletask.extensions.history_export._internal import dt_from_iso |
| 31 | +from durabletask.extensions.history_export.activities import _require_context |
| 32 | + |
| 33 | +# The activity registers under the same name the export orchestrator calls, so |
| 34 | +# it transparently replaces the core activity. |
| 35 | +LIST_TERMINAL_INSTANCES_ACTIVITY = "list_terminal_instances" |
| 36 | + |
| 37 | + |
| 38 | +def list_terminal_instances( |
| 39 | + _: task.ActivityContext, input: Mapping[str, Any]) -> dict[str, Any]: |
| 40 | + """Enumerate terminal instances via ``QueryInstances``. |
| 41 | +
|
| 42 | + Drop-in replacement for the core ``list_terminal_instances`` activity that |
| 43 | + avoids the unimplemented ``ListInstanceIds`` call. ``QueryInstances`` filters |
| 44 | + by *created* time whereas the export filters by *completed* time, so the |
| 45 | + completed-time window is applied client-side against each instance's last |
| 46 | + update time. Every match is returned in a single page |
| 47 | + (``continuation_token`` is always ``None``) because |
| 48 | + ``get_all_orchestration_states`` paginates internally. |
| 49 | + """ |
| 50 | + ctx = _require_context() |
| 51 | + |
| 52 | + # A continuation token means the first call already returned everything; |
| 53 | + # there is no second page under this enumeration strategy. |
| 54 | + if input.get("continuation_token"): |
| 55 | + return {"instance_ids": [], "continuation_token": None} |
| 56 | + |
| 57 | + raw_statuses = input.get("runtime_status") |
| 58 | + runtime_status_names: Optional[list[str]] = ( |
| 59 | + list(raw_statuses) if raw_statuses is not None else None |
| 60 | + ) |
| 61 | + completed_time_from = dt_from_iso(input.get("completed_time_from")) |
| 62 | + completed_time_to = dt_from_iso(input.get("completed_time_to")) |
| 63 | + if completed_time_from is None: |
| 64 | + raise ValueError("list_terminal_instances requires 'completed_time_from'") |
| 65 | + |
| 66 | + runtime_status: Optional[list[OrchestrationStatus]] = None |
| 67 | + if runtime_status_names is not None: |
| 68 | + runtime_status = [OrchestrationStatus[name] for name in runtime_status_names] |
| 69 | + |
| 70 | + states = ctx.client.get_all_orchestration_states( |
| 71 | + OrchestrationQuery(runtime_status=runtime_status)) |
| 72 | + |
| 73 | + instance_ids: list[str] = [] |
| 74 | + for state in states: |
| 75 | + completed_at = ensure_aware(state.last_updated_at) |
| 76 | + if completed_at is not None: |
| 77 | + if completed_at < completed_time_from: |
| 78 | + continue |
| 79 | + if completed_time_to is not None and completed_at > completed_time_to: |
| 80 | + continue |
| 81 | + instance_ids.append(state.instance_id) |
| 82 | + |
| 83 | + return {"instance_ids": instance_ids, "continuation_token": None} |
0 commit comments