-
Notifications
You must be signed in to change notification settings - Fork 16.4k
add deferrable mode to RedshiftDataOperator #36586
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
Merged
eladkal
merged 11 commits into
apache:main
from
astronomer:add-deferrable-mode-to-RedshiftDataOperator
Jan 18, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0b3d281
feat(providers/amazon): add deferrable mode to RedshiftDataOperator
Lee-W f01a621
test(providers/amazon): add test case to RedshiftDataHook async methods
Lee-W 0f74f09
test(providers/amazon): add test case to RedshiftDataOperator when de…
Lee-W 6a2ae24
refactor(providers/amazon): extract comment operator initialization a…
Lee-W 39c5981
refactor(providers/amaozn): rename region as region_name
Lee-W 6a149ba
feat(providers/amazon): add verify and botocore_config as suggested
Lee-W a4eb56e
refactor(providers/amazon): use async_conn from aws hook and add miss…
Lee-W 2ff2f2c
feat(providers/amazon): make RedshiftDataTrigger.hook a cached_property
Lee-W bdec226
refactor(providers/amaozn): unify how async and sync version of check…
Lee-W 14049d6
style(providers/amazon): fix mypy failure
Lee-W d0a7d1c
fix(providers/amazon): fix async_conn call
Lee-W File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from functools import cached_property | ||
| from typing import Any, AsyncIterator | ||
|
|
||
| from airflow.providers.amazon.aws.hooks.redshift_data import ( | ||
| ABORTED_STATE, | ||
| FAILED_STATE, | ||
| RedshiftDataHook, | ||
| RedshiftDataQueryAbortedError, | ||
| RedshiftDataQueryFailedError, | ||
| ) | ||
| from airflow.triggers.base import BaseTrigger, TriggerEvent | ||
|
|
||
|
|
||
| class RedshiftDataTrigger(BaseTrigger): | ||
| """ | ||
| RedshiftDataTrigger is fired as deferred class with params to run the task in triggerer. | ||
|
|
||
| :param statement_id: the UUID of the statement | ||
| :param task_id: task ID of the Dag | ||
| :param poll_interval: polling period in seconds to check for the status | ||
| :param aws_conn_id: AWS connection ID for redshift | ||
| :param region_name: aws region to use | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| statement_id: str, | ||
| task_id: str, | ||
| poll_interval: int, | ||
| aws_conn_id: str | None = "aws_default", | ||
| region_name: str | None = None, | ||
| verify: bool | str | None = None, | ||
| botocore_config: dict | None = None, | ||
| ): | ||
| super().__init__() | ||
| self.statement_id = statement_id | ||
| self.task_id = task_id | ||
| self.poll_interval = poll_interval | ||
|
|
||
| self.aws_conn_id = aws_conn_id | ||
| self.region_name = region_name | ||
| self.verify = verify | ||
| self.botocore_config = botocore_config | ||
|
|
||
| def serialize(self) -> tuple[str, dict[str, Any]]: | ||
| """Serializes RedshiftDataTrigger arguments and classpath.""" | ||
| return ( | ||
| "airflow.providers.amazon.aws.triggers.redshift_data.RedshiftDataTrigger", | ||
| { | ||
| "statement_id": self.statement_id, | ||
| "task_id": self.task_id, | ||
| "aws_conn_id": self.aws_conn_id, | ||
| "poll_interval": self.poll_interval, | ||
| "region_name": self.region_name, | ||
| "verify": self.verify, | ||
| "botocore_config": self.botocore_config, | ||
| }, | ||
| ) | ||
|
|
||
| @cached_property | ||
| def hook(self) -> RedshiftDataHook: | ||
| return RedshiftDataHook( | ||
| aws_conn_id=self.aws_conn_id, | ||
| region_name=self.region_name, | ||
| verify=self.verify, | ||
| config=self.botocore_config, | ||
| ) | ||
|
|
||
| async def run(self) -> AsyncIterator[TriggerEvent]: | ||
| try: | ||
| while await self.hook.is_still_running(self.statement_id): | ||
| await asyncio.sleep(self.poll_interval) | ||
|
|
||
| is_finished = await self.hook.check_query_is_finished_async(self.statement_id) | ||
| if is_finished: | ||
| response = {"status": "success", "statement_id": self.statement_id} | ||
| else: | ||
| response = { | ||
| "status": "error", | ||
| "statement_id": self.statement_id, | ||
| "message": f"{self.task_id} failed", | ||
| } | ||
| yield TriggerEvent(response) | ||
| except (RedshiftDataQueryFailedError, RedshiftDataQueryAbortedError) as error: | ||
| response = { | ||
| "status": "error", | ||
| "statement_id": self.statement_id, | ||
| "message": str(error), | ||
| "type": FAILED_STATE if isinstance(error, RedshiftDataQueryFailedError) else ABORTED_STATE, | ||
| } | ||
| yield TriggerEvent(response) | ||
| except Exception as error: | ||
| yield TriggerEvent({"status": "error", "statement_id": self.statement_id, "message": str(error)}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.