Skip to content

Commit

Permalink
utils: Cancel the conversion tasks if the are older then 24 hours
Browse files Browse the repository at this point in the history
Signed-off-by: Sayan Chowdhury <sayan.chowdhury2012@gmail.com>
  • Loading branch information
sayanchowdhury committed Jul 13, 2018
1 parent 9f0e446 commit 460be30
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions fedimg/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import fedimg.uploader
from fedimg.config import PROCESS_COUNT, STATUS_FILTER
from fedimg.utils import get_rawxz_urls, get_value_from_dict
from fedimg.utils import cancel_stale_conversion_tasks

_log = logging.getLogger(__name__)

Expand Down Expand Up @@ -80,6 +81,8 @@ def consume(self, msg):
_log.debug('%s is not valid status' % msg_info['status'])
return

cancel_stale_conversion_tasks()

location = msg_info['location']
compose_id = msg_info['compose_id']
try:
Expand Down
37 changes: 37 additions & 0 deletions fedimg/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Utility functions for fedimg.
"""

import datetime
import functools
import logging
import os
Expand Down Expand Up @@ -205,3 +206,39 @@ def get_image_name_from_ami_name_for_fedmsg(image_name):
image_name = name_vt_region.rsplit('-', 4)[:-4][0]

return image_name


def cancel_stale_conversion_tasks():
_log.info("Preparing to cancel conversion tasks")
output, error, retcode = external_run_command([
'euca-describe-conversion-tasks --region us-east-1 | grep active',
])

if retcode != 0:
return False

tasks = output.split('\n')[1:-1]

for task in tasks:
expiration_datetime = datetime.datetime.strptime(
task.split()[5],
'%Y-%m-%dT%H:%M:%Sz'
)
import_vol_task_id = task.split()[3]

start_datetime = expiration_datetime - datetime.timedelta(days=7)
now_datetime = datetime.datetime.now()

delta_time = now_datetime - start_datetime

if delta_time.total_seconds() >= 86400:
_log.info('This is a stale task. Canceling %r'% import_vol_task_id)
output, error, retcode = external_run_command([
'euca-cancel-conversion-task --region us-east-1',
import_vol_task_id
])

if retcode != 0:
_log.info('Could not cancel the task %r' % import_vol_task_id)

return True

0 comments on commit 460be30

Please sign in to comment.