|
17 | 17 | # - name: name of the integration alias |
18 | 18 | # - obj: object it points to |
19 | 19 | # |
20 | | -ALIASES_DESC = [("ArgoEvent", ".plugins.argo.argo_events.ArgoEvent")] |
| 20 | +ALIASES_DESC = [ |
| 21 | + ("ArgoEvent", ".plugins.argo.argo_events.ArgoEvent"), |
| 22 | + ("SFNEvent", ".plugins.aws.step_functions.sfn_event.SFNEvent"), |
| 23 | + ("AirflowEvent", ".plugins.airflow.airflow_event.AirflowEvent"), |
| 24 | +] |
21 | 25 |
|
22 | 26 | # Aliases can be enabled or disabled through configuration or extensions: |
23 | 27 | # - ENABLED_INTEGRATION_ALIAS: list of alias names to enable. |
|
27 | 31 | # ENABLED_INTEGRATION_ALIAS from the concatenation of the names in |
28 | 32 | # ALIASES_DESC (concatenation of the names here as well as in the extensions). |
29 | 33 |
|
| 34 | + |
| 35 | +def _resolve_event_providers(): |
| 36 | + """Lazily resolve all registered event provider classes from the plugin system.""" |
| 37 | + from metaflow.plugins import EVENT_PROVIDERS |
| 38 | + |
| 39 | + return EVENT_PROVIDERS |
| 40 | + |
| 41 | + |
| 42 | +def send_event(name, payload=None, backend=None, **kwargs): |
| 43 | + """Send a trigger event to wake a deployed @trigger flow. |
| 44 | +
|
| 45 | + Discovers event providers via the plugin system. Each provider class |
| 46 | + exposes ``TYPE`` (e.g. ``"argo-workflows"``) and ``is_configured()`` |
| 47 | + which checks whether the required environment variables are set. |
| 48 | +
|
| 49 | + When *backend* is ``None``, the first provider whose ``is_configured()`` |
| 50 | + returns ``True`` is used. When *backend* is given, it is matched against |
| 51 | + the provider's ``TYPE`` (dashes are normalised to underscores). |
| 52 | +
|
| 53 | + New backends are added by registering an ``EVENT_PROVIDERS_DESC`` entry |
| 54 | + in ``metaflow/plugins/__init__.py`` — no changes to this function needed. |
| 55 | +
|
| 56 | + Parameters |
| 57 | + ---------- |
| 58 | + name : str |
| 59 | + Event name (must match the @trigger event name on the deployed flow). |
| 60 | + payload : dict, optional |
| 61 | + Key-value pairs delivered with the event, used to set parameters. |
| 62 | + backend : str, optional |
| 63 | + Override auto-detection. Matched against provider TYPE |
| 64 | + (e.g. ``"argo-workflows"``, ``"step-functions"``, ``"airflow"``). |
| 65 | + Underscores are accepted as well (``"argo_workflows"``). |
| 66 | + **kwargs |
| 67 | + Passed through to the provider class constructor. |
| 68 | +
|
| 69 | + Returns |
| 70 | + ------- |
| 71 | + str or None |
| 72 | + Event ID if published successfully. |
| 73 | + """ |
| 74 | + providers = _resolve_event_providers() |
| 75 | + |
| 76 | + if backend is not None: |
| 77 | + # Normalise so "argo_workflows" matches "argo-workflows" |
| 78 | + norm = backend.replace("_", "-") |
| 79 | + for provider_class in providers: |
| 80 | + if provider_class.TYPE == norm: |
| 81 | + return provider_class(name, payload=payload, **kwargs).publish() |
| 82 | + available = [p.TYPE for p in providers] |
| 83 | + raise ValueError( |
| 84 | + "Unknown event backend %r. Available: %s" % (backend, available) |
| 85 | + ) |
| 86 | + |
| 87 | + # Auto-detect: pick the first configured provider |
| 88 | + for provider_class in providers: |
| 89 | + if hasattr(provider_class, "is_configured") and provider_class.is_configured(): |
| 90 | + return provider_class(name, payload=payload, **kwargs).publish() |
| 91 | + |
| 92 | + available = [p.TYPE for p in providers] |
| 93 | + raise RuntimeError( |
| 94 | + "Cannot auto-detect event backend — no provider is configured. " |
| 95 | + "Set the 'backend' parameter explicitly or configure one of: %s" % available |
| 96 | + ) |
| 97 | + |
| 98 | + |
30 | 99 | # Keep this line and make sure ALIASES_DESC is above this line. |
31 | 100 | process_integration_aliases(globals()) |
0 commit comments