-
Notifications
You must be signed in to change notification settings - Fork 14.2k
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
Various fixes on ECS run task operator #31838
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a3afef6
ECS Run Task op should not try to get logs or check the status if not…
vandonr-amz f96c0fa
fix system tests
vandonr-amz a899019
forgot the trigger
vandonr-amz 176cc6a
single line comment & static check fix
vandonr-amz 3347548
fix tests
vandonr-amz 7ea0126
Merge remote-tracking branch 'origin/main' into vandonr/tests
vandonr-amz e1c9268
fix doc
vandonr-amz 9c2a3b5
dynamic log group
vandonr-amz ce01b0c
Merge remote-tracking branch 'origin/main' into vandonr/tests
vandonr-amz 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 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 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 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 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 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 |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
from airflow import DAG | ||
from airflow.decorators import task | ||
from airflow.models.baseoperator import chain | ||
from airflow.providers.amazon.aws.hooks.ecs import EcsClusterStates, EcsTaskStates | ||
from airflow.providers.amazon.aws.hooks.ecs import EcsClusterStates | ||
from airflow.providers.amazon.aws.operators.ecs import ( | ||
EcsCreateClusterOperator, | ||
EcsDeleteClusterOperator, | ||
|
@@ -34,7 +34,6 @@ | |
from airflow.providers.amazon.aws.sensors.ecs import ( | ||
EcsClusterStateSensor, | ||
EcsTaskDefinitionStateSensor, | ||
EcsTaskStateSensor, | ||
) | ||
from airflow.utils.trigger_rule import TriggerRule | ||
from tests.system.providers.amazon.aws.utils import ENV_ID_KEY, SystemTestContextBuilder | ||
|
@@ -67,6 +66,15 @@ def get_region(): | |
return boto3.session.Session().region_name | ||
|
||
|
||
@task(trigger_rule=TriggerRule.ALL_DONE) | ||
def clean_logs(group_name: str): | ||
client = boto3.client("logs") | ||
# A bit brutal to delete the whole group, I know, | ||
# but we don't have the access to the arn of the task which is used in the stream name | ||
# and also those logs just contain "hello world", which is not very interesting. | ||
client.delete_log_group(logGroupName=group_name) | ||
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. this is going to fail if the group does not exist, so in a way it makes sure the log configuration stays correct. |
||
|
||
|
||
with DAG( | ||
dag_id=DAG_ID, | ||
schedule="@once", | ||
|
@@ -85,6 +93,7 @@ def get_region(): | |
asg_name = f"{env_id}-asg" | ||
|
||
aws_region = get_region() | ||
log_group_name = "/ecs/hello-world" | ||
vandonr-amz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# [START howto_operator_ecs_create_cluster] | ||
create_cluster = EcsCreateClusterOperator( | ||
|
@@ -114,7 +123,16 @@ def get_region(): | |
"workingDirectory": "/usr/bin", | ||
"entryPoint": ["sh", "-c"], | ||
"command": ["ls"], | ||
} | ||
"logConfiguration": { | ||
"logDriver": "awslogs", | ||
"options": { | ||
"awslogs-group": log_group_name, | ||
"awslogs-region": aws_region, | ||
"awslogs-create-group": "true", | ||
"awslogs-stream-prefix": "ecs", | ||
}, | ||
}, | ||
}, | ||
], | ||
register_task_kwargs={ | ||
"cpu": "256", | ||
|
@@ -140,38 +158,19 @@ def get_region(): | |
"containerOverrides": [ | ||
{ | ||
"name": container_name, | ||
"command": ["echo", "hello", "world"], | ||
"command": ["echo hello world"], | ||
}, | ||
], | ||
}, | ||
network_configuration={"awsvpcConfiguration": {"subnets": existing_cluster_subnets}}, | ||
# [START howto_awslogs_ecs] | ||
awslogs_group="/ecs/hello-world", | ||
awslogs_group=log_group_name, | ||
awslogs_region=aws_region, | ||
awslogs_stream_prefix="ecs/hello-world-container", | ||
awslogs_stream_prefix=f"ecs/{container_name}", | ||
# [END howto_awslogs_ecs] | ||
# You must set `reattach=True` in order to get ecs_task_arn if you plan to use a Sensor. | ||
reattach=True, | ||
) | ||
# [END howto_operator_ecs_run_task] | ||
|
||
# EcsRunTaskOperator waits by default, setting as False to test the Sensor below. | ||
run_task.wait_for_completion = False | ||
|
||
# [START howto_sensor_ecs_task_state] | ||
# By default, EcsTaskStateSensor waits until the task has started, but the | ||
# demo task runs so fast that the sensor misses it. This sensor instead | ||
# demonstrates how to wait until the ECS Task has completed by providing | ||
# the target_state and failure_states parameters. | ||
await_task_finish = EcsTaskStateSensor( | ||
task_id="await_task_finish", | ||
cluster=existing_cluster_name, | ||
task=run_task.output["ecs_task_arn"], | ||
target_state=EcsTaskStates.STOPPED, | ||
failure_states={EcsTaskStates.NONE}, | ||
) | ||
# [END howto_sensor_ecs_task_state] | ||
|
||
# [START howto_operator_ecs_deregister_task_definition] | ||
deregister_task = EcsDeregisterTaskDefinitionOperator( | ||
task_id="deregister_task", | ||
|
@@ -209,10 +208,10 @@ def get_region(): | |
register_task, | ||
await_task_definition, | ||
run_task, | ||
await_task_finish, | ||
deregister_task, | ||
delete_cluster, | ||
await_delete_cluster, | ||
clean_logs(log_group_name), | ||
) | ||
|
||
from tests.system.utils.watcher import watcher | ||
|
This file contains 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
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.
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.
Whatever logs users were getting for the short period of time without a wait_for_completion they will no longer get. So we're calling it a bug fix with no deprecation?
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.
Yeah, idk, we may want to keep the existing behavior, but what I don't like about it is that it made the operator slower just for the sake of maybe getting a couple of logs...
Since we were starting the thread, which slept for 30 seconds (or configured value) before checking if it was stopped, this operator would take 30 seconds to return no matter what, when the job was done in a second and a half.
It's like "don't wait for completion but still wait a bit"
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.
I'd agree with Raph, I dont think this is a desired behavior but more an forgotten edge case. I would call it a bug fix
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.
Ack, I'll call that quorum then, let's call it a bug fix 👍