Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #189 from ministryofjustice/add_simple_countdown_f…
Browse files Browse the repository at this point in the history
…or_long_waits

Add simple countdown for long waits
  • Loading branch information
filipposc5 committed Feb 26, 2016
2 parents 1ef35fe + be563ae commit e32ba74
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
8 changes: 3 additions & 5 deletions bootstrap_cfn/autoscale.py
@@ -1,7 +1,5 @@
import logging

import time

import boto.ec2.autoscale

import boto3
Expand Down Expand Up @@ -97,7 +95,7 @@ def cycle_instances(self,

# wait for the same time as the "HealthCheckGracePeriod" in the ASG
logging.getLogger("bootstrap-cfn").info("Waiting %ss - HealthCheckGracePeriod" % health_check_grace_period)
time.sleep(health_check_grace_period)
utils.sleep_countdown(health_check_grace_period)
logging.getLogger("bootstrap-cfn").info("End of waiting period")

# check if the number of healthy instances is = to the number of expected instances, where
Expand All @@ -119,7 +117,7 @@ def cycle_instances(self,
.format(current_instance_id, termination_delay))
if termination_delay:
logging.getLogger("bootstrap-cfn").info("Waiting %ss - termination_delay" % termination_delay)
time.sleep(termination_delay)
utils.sleep_countdown(termination_delay)
logging.getLogger("bootstrap-cfn").info("End of waiting period")
client.terminate_instance_in_auto_scaling_group(
InstanceId=current_instance_id,
Expand Down Expand Up @@ -175,7 +173,7 @@ def wait_for_instances(self,
.format(retry_delay, count, retry_max))
if count == retry_max:
raise AutoscalingInstanceCountError(self.group.name, expected_instance_count, instances)
time.sleep(retry_delay)
utils.sleep_countdown(retry_delay)
instances = self.get_healthy_instances()
logging.getLogger("bootstrap-cfn").info("wait_for_instances: Found {} instances, {}"
.format(len(instances), [instance.get('InstanceId') for instance in instances]))
Expand Down
17 changes: 17 additions & 0 deletions bootstrap_cfn/utils.py
@@ -1,4 +1,5 @@
import os
import sys
import time

from copy import deepcopy
Expand Down Expand Up @@ -134,3 +135,19 @@ def get_events(stack, stack_name):
next = events.next_token
time.sleep(1)
return reversed(sum(event_list, []))


def sleep_countdown(sleep_time):
"""
Simple terminal countdown of the form mm:ss
Args:
sleep_time(int): The number of seconds to countdown from.
"""
while sleep_time > 0:
mins, secs = divmod(sleep_time, 60)
timeformat = '{:02d}:{:02d}'.format(mins, secs)
sys.stdout.write("{}\r".format(timeformat))
sys.stdout.flush()
time.sleep(1)
sleep_time -= 1

0 comments on commit e32ba74

Please sign in to comment.