This repository was archived by the owner on Jun 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheck_task.py
More file actions
51 lines (45 loc) · 1.68 KB
/
check_task.py
File metadata and controls
51 lines (45 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import datetime
import logging
import sys, os
parent_dir = os.path.abspath(os.path.dirname(__file__))
vendor_dir = os.path.join(parent_dir, 'vendor')
sys.path.append(vendor_dir)
from dateutil.parser import parse
from lib import EcsTaskManager, EcsTaskFailureError, EcsTaskExitCodeError, EcsTaskTimeoutError
from lib import validate_ecs
from lib import ecs_error_handler
# Configure logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(os.environ.get("LOG_LEVEL", "INFO"))
# ECS Task Manager
task_mgr = EcsTaskManager()
# Checks if timeout has exceeded
def check_timeout(event):
now = parse(datetime.datetime.utcnow().isoformat() + 'Z')
creation = parse(event['CreateTimestamp'])
if now > creation + datetime.timedelta(seconds=event['Timeout']):
raise EcsTaskTimeoutError(event['Tasks'], creation, event['Timeout'])
# Checks ECS task exit codes
def check_exit_codes(tasks):
non_zero = [c.get('taskArn') for t in tasks for c in t.get('containers') if c.get('exitCode') != 0]
if non_zero:
raise EcsTaskExitCodeError(tasks, non_zero)
@ecs_error_handler
def handler(event, context):
log.info('Received event %s' % str(event))
# Validate event and create task
event = validate_ecs(event)
check_timeout(event)
# Query task status
task_arns = [t.get('taskArn') for t in event['Tasks']]
result = task_mgr.describe_tasks(cluster=event['Cluster'], tasks=task_arns)
event['Tasks'] = result['tasks']
event['Failures'] = result['failures']
if event['Failures']:
raise EcsTaskFailureError(result)
# Check if task is complete
event['Status'] = task_mgr.check_status(event['Tasks'])
if event['Status'] == 'STOPPED':
check_exit_codes(event['Tasks'])
return event