Skip to content

Commit

Permalink
Merge pull request #90 from druids/FixGetECSTaskRunResult
Browse files Browse the repository at this point in the history
Fixed ECS task run result
  • Loading branch information
radimsuckr authored Feb 16, 2022
2 parents 3652cac + ac58185 commit 93f9280
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions developers_chamber/ecs_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
from datetime import datetime
from multiprocessing.pool import ThreadPool

import boto3
Expand Down Expand Up @@ -463,39 +464,48 @@ def run_task_and_wait_for_success(cluster, task_definition, command, name, succe
task_id = task.split('/')[-1]

LOGGER.info("Running task: '{}'".format(task))

wait_for_task_to_stop(cluster=cluster, task=task, timeout=timeout, region=region, ecs_client=ecs_client)

response = ecs_client.describe_tasks(cluster=cluster, tasks=[task])

UNDEFINED = 'UNDEFINED'

task_stop_code = response['tasks'][0].get('stopCode', UNDEFINED)
LOGGER.info("Task stop code: '{}'".format(task_stop_code))

container_response = {}
for _container_response in response['tasks'][0]['containers']:
if _container_response['name'] == name:
container_response = _container_response

if task_stop_code != 'EssentialContainerExited':
contianer_stop_reason = response['tasks'][0]['containers'][0].get('reason', UNDEFINED)
contianer_stop_reason = container_response.get('reason', UNDEFINED)
LOGGER.info("Container stop reason: '{}'".format(contianer_stop_reason))

if contianer_stop_reason != UNDEFINED:
raise ClickException(contianer_stop_reason)

task_stop_reason = response['tasks'][0].get('stoppedReason', UNDEFINED)
LOGGER.info("Task stop reason: '{}'".format(task_stop_reason))

if task_stop_reason != UNDEFINED:
raise ClickException(task_stop_reason)

raise ClickException(response['tasks'][0])

try:
LOGGER.info('Task output:')
for event in get_log_events(log_group=task_definition, log_stream='ecs/{}/{}'.format(name, task_id),
region=region):
LOGGER.info('[task/{}] {}'.format(task_id, event['message'].rstrip()))
LOGGER.info(
2 * ' ' + '[task/{} - {}] {}'.format(
task_id,
datetime.fromtimestamp(event['timestamp'] // 1000),
event['message'].rstrip()
)
)
except Exception as ex:
LOGGER.info(ex)

exit_code = response['tasks'][0]['containers'][0].get('exitCode', UNDEFINED)
exit_code = container_response.get('exitCode', UNDEFINED)
LOGGER.info("Container exit code: '{}'".format(exit_code))

if exit_code == UNDEFINED:
Expand Down

0 comments on commit 93f9280

Please sign in to comment.