Skip to content

Commit

Permalink
Fixes bug caused by task ARN coming with cluster name, causing script…
Browse files Browse the repository at this point in the history
… to not find logs
  • Loading branch information
adenot committed Oct 21, 2020
1 parent e6e1558 commit 6871d37
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
13 changes: 8 additions & 5 deletions src/run-task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ TASK_ARN=$(aws ecs register-task-definition \
--output=text)

echo "---> Executing ECS Task"
echo " CLUSTER_NAME: ${CLUSTER_NAME}"
echo " APP_NAME: ${APP_NAME}"
echo " TASK_ARN: ${TASK_ARN}"

echo " CLUSTER_NAME: ${CLUSTER_NAME}"
echo " APP_NAME: ${APP_NAME}"
echo " TASK_DEFINITION_ARN: ${TASK_ARN}"
echo -n " STATUS: "
TASK_ID=$(aws ecs run-task \
--cluster $CLUSTER_NAME \
--task-definition $TASK_ARN \
Expand All @@ -41,8 +41,11 @@ sleep 5

while [ "$(aws ecs describe-tasks --tasks $TASK_ID --cluster $CLUSTER_NAME --query="tasks[0].lastStatus" --output=text)" == "PENDING" ]
do
sleep 1
echo -n "."
sleep 5
done
echo -n "RUNNING"
echo

echo "---> Task ARN $TASK_ID"

Expand Down
45 changes: 20 additions & 25 deletions src/tail-task-logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,15 @@
app_name=os.environ['APP_NAME']
task_arn=sys.argv[1]

task_id=task_arn.split(":task/",1)[1] #get the task number id
task_id=task_arn.split("/")[-1] #get the task number id (without the cluster name)
last_event = None
log_group_name='/ecs/'+cluster_name+'/'+app_name
log_stream_prefix = None

print(" Waiting for logs...")
print("======== TASK LOGS ========")

while True:
try:
response = aws_ecs.describe_tasks(
cluster=cluster_name,
tasks=[task_arn])
task_status = response['tasks'][0]['lastStatus']

if log_stream_prefix is None:
log_streams = logs.describe_log_streams(logGroupName=log_group_name, orderBy='LastEventTime', descending=True, limit=1)

Expand All @@ -33,7 +28,6 @@
'logStreamName': log_stream_prefix+'/'+task_id,
'startFromHead': True
}

else:
log_stream_events = logs.get_log_events(**extra_args)

Expand All @@ -43,22 +37,24 @@
if 'nextToken' not in extra_args or log_stream_events['nextForwardToken'] != extra_args['nextToken']:
extra_args['nextToken'] = log_stream_events['nextForwardToken']

if task_status == "STOPPED":
print("======== TASK STOPPED ========")
print("Task ID: %s" % task_id)
print("Task ARN: %s" % task_arn)
print("Service Name: %s" % app_name)
print("Cluster Name: %s" % cluster_name)
if 'startedAt' in response['tasks'][0]:
print("Started at: %s" % response['tasks'][0]['startedAt'])
print("Stopped at: %s" % response['tasks'][0]['stoppedAt'])
print("Stopped Reason: %s" % response['tasks'][0]['stoppedReason'])
if 'stopCode' in response['tasks'][0]:
print("Stop Code: %s" % response['tasks'][0]['stopCode'])
print("")
break

time.sleep(1)
response = aws_ecs.describe_tasks(
cluster=cluster_name,
tasks=[task_arn])

if response['tasks'][0]['lastStatus'] == "STOPPED":
print("======== TASK STOPPED ========")
print("Task ID: %s" % task_id)
print("Task ARN: %s" % task_arn)
print("Service Name: %s" % app_name)
print("Cluster Name: %s" % cluster_name)
if 'startedAt' in response['tasks'][0]:
print("Started at: %s" % response['tasks'][0]['startedAt'])
print("Stopped at: %s" % response['tasks'][0]['stoppedAt'])
print("Stopped Reason: %s" % response['tasks'][0]['stoppedReason'])
if 'stopCode' in response['tasks'][0]:
print("Stop Code: %s" % response['tasks'][0]['stopCode'])
print("")
break

except logs.exceptions.ResourceNotFoundException as e:
time.sleep(5)
Expand All @@ -68,4 +64,3 @@
print("Error: " + str(e))
break


0 comments on commit 6871d37

Please sign in to comment.