Skip to content
This repository was archived by the owner on Aug 9, 2024. It is now read-only.

Commit 436e2f0

Browse files
utils: Cancel the conversion tasks if the are older then 24 hours
Signed-off-by: Sayan Chowdhury <[email protected]>
1 parent c101b69 commit 436e2f0

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

fedimg/consumers.py

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
from fedimg.config import PROCESS_COUNT, STATUS_FILTER
3939
from fedimg.utils import get_rawxz_urls, get_value_from_dict
40+
from fedimg.utils import cancel_stale_conversion_tasks
4041

4142
_log = logging.getLogger(__name__)
4243

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

84+
cancel_stale_conversion_tasks()
85+
8386
location = msg_info['location']
8487
compose_id = msg_info['compose_id']
8588
try:

fedimg/utils.py

+37
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import logging
2727
_log = logging.getLogger(__name__)
2828

29+
import datetime
2930
import functools
3031
import os
3132
import re
@@ -199,3 +200,39 @@ def get_image_name_from_ami_name_for_fedmsg(image_name):
199200
image_name = name_vt_region.rsplit('-', 4)[:-4][0]
200201

201202
return image_name
203+
204+
205+
def cancel_stale_conversion_tasks():
206+
_log.info("Preparing to cancel conversion tasks")
207+
output, error, retcode = external_run_command([
208+
'euca-describe-conversion-tasks --region us-east-1 | grep active',
209+
])
210+
211+
if retcode != 0:
212+
return False
213+
214+
tasks = output.split('\n')[1:-1]
215+
216+
for task in tasks:
217+
expiration_datetime = datetime.datetime.strptime(
218+
task.split()[5],
219+
'%Y-%m-%dT%H:%M:%Sz'
220+
)
221+
import_vol_task_id = task.split()[3]
222+
223+
start_datetime = expiration_datetime - datetime.timedelta(days=7)
224+
now_datetime = datetime.datetime.now()
225+
226+
delta_time = now_datetime - start_datetime
227+
228+
if delta_time.total_seconds() >= 86400:
229+
_log.info('This is a stale task. Canceling %r'% import_vol_task_id)
230+
output, error, retcode = external_run_command([
231+
'euca-cancel-conversion-task --region us-east-1',
232+
import_vol_task_id
233+
])
234+
235+
if retcode != 0:
236+
_log.info('Could not cancel the task %r' % import_vol_task_id)
237+
238+
return True

0 commit comments

Comments
 (0)