Repository navigation
Make HttpSensor defer when response_check is provided #71922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f832de8
f1c2a97
88c1913
b3981f1
4701b46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -220,6 +220,10 @@ class HttpSensorTrigger(BaseTrigger): | |
| :param extra_options: Additional kwargs to pass when creating a request. | ||
| For example, ``run(json=obj)`` is passed as ``aiohttp.ClientSession().get(json=obj)`` | ||
| :param poke_interval: Time to sleep using asyncio | ||
| :param initial_delay: Time to sleep before the first request. Used when the | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a pattern that is being used elsewhere (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could not find another trigger using the same |
||
| sensor re-defers after evaluating ``response_check`` on the worker, so | ||
| consecutive attempts keep the ``poke_interval`` pacing. | ||
| :param should_return_response: Whether to include the serialized HTTP response in the trigger event. | ||
| """ | ||
|
|
||
| def __init__( | ||
|
|
@@ -231,6 +235,8 @@ def __init__( | |
| headers: dict[str, str] | None = None, | ||
| extra_options: dict[str, Any] | None = None, | ||
| poke_interval: float = 5.0, | ||
| initial_delay: float = 0.0, | ||
| should_return_response: bool = False, | ||
| ): | ||
| super().__init__() | ||
| self.endpoint = endpoint | ||
|
|
@@ -240,6 +246,8 @@ def __init__( | |
| self.extra_options = extra_options or {} | ||
| self.http_conn_id = http_conn_id | ||
| self.poke_interval = poke_interval | ||
| self.initial_delay = initial_delay | ||
| self.should_return_response = should_return_response | ||
|
|
||
| def serialize(self) -> tuple[str, dict[str, Any]]: | ||
| """Serialize HttpTrigger arguments and classpath.""" | ||
|
|
@@ -253,23 +261,37 @@ def serialize(self) -> tuple[str, dict[str, Any]]: | |
| "extra_options": self.extra_options, | ||
| "http_conn_id": self.http_conn_id, | ||
| "poke_interval": self.poke_interval, | ||
| "initial_delay": self.initial_delay, | ||
| "should_return_response": self.should_return_response, | ||
| }, | ||
| ) | ||
|
|
||
| async def run(self) -> AsyncIterator[TriggerEvent]: | ||
| """Make a series of asynchronous http calls via an http hook.""" | ||
| hook = self._get_async_hook() | ||
| if self.initial_delay > 0: | ||
| await asyncio.sleep(self.initial_delay) | ||
| while True: | ||
| try: | ||
| async with aiohttp.ClientSession() as session: | ||
| await hook.run( | ||
| client_response = await hook.run( | ||
| session=session, | ||
| endpoint=self.endpoint, | ||
| data=self.data, | ||
| headers=self.headers, | ||
| extra_options=self.extra_options, | ||
| ) | ||
| yield TriggerEvent(True) | ||
| if self.should_return_response: | ||
| response = await HttpTrigger._convert_response(client_response) | ||
| if self.should_return_response: | ||
| yield TriggerEvent( | ||
| { | ||
| "status": "success", | ||
| "response": HttpResponseSerializer.serialize(response), | ||
| } | ||
| ) | ||
| else: | ||
| yield TriggerEvent(True) | ||
| return | ||
| except AirflowException as exc: | ||
| if str(exc).startswith("404"): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like the
self.response_checkbehvaior is being dropped? Is that an issue?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I understand it, this condition causes the behavior reported in #40209 because setting
response_checkforces the sensor onto the synchronous path. The check itself is still evaluated byself.poke(context)before deferral and byexecute_complete()after the trigger returns a response. A false result defers the task again.