Repository: Cloud-CV/EvalAI

Issue:
No auto scale on intializing/stopping: Auto scaling should not work when instance is initializing or stopping.



Discussion:

Resolving patch:
diff --git a/apps/challenges/aws_utils.py b/apps/challenges/aws_utils.py
--- a/apps/challenges/aws_utils.py
+++ b/apps/challenges/aws_utils.py
@@ -471,10 +471,10 @@ def service_manager(
 
 def stop_ec2_instance(challenge):
     """
-    Stop the EC2 instance associated with a challenge.
+    Stop the EC2 instance associated with a challenge if status checks are ready.
 
     Args:
-        challenge (Challenge): The challenge for which the EC2 instnace needs to be stopped.
+        challenge (Challenge): The challenge for which the EC2 instance needs to be stopped.
 
     Returns:
         dict: A dictionary containing the status and message of the stop operation.
@@ -482,43 +482,41 @@ def stop_ec2_instance(challenge):
     target_instance_id = challenge.ec2_instance_id
 
     ec2 = get_boto3_client("ec2", aws_keys)
-    response = ec2.describe_instances(InstanceIds=[target_instance_id])
-
-    instances = [
-        instance
-        for reservation in response["Reservations"]
-        for instance in reservation["Instances"]
-    ]
-
-    if instances:
-        instance = instances[0]
-        instance_id = instance["InstanceId"]
-        if instance["State"]["Name"] == "running":
-            try:
-                response = ec2.stop_instances(InstanceIds=[instance_id])
-                message = "Instance for challenge {} successfully stopped.".format(
-                    challenge.pk
-                )
-                return {
-                    "response": response,
-                    "message": message,
-                }
-            except ClientError as e:
-                logger.exception(e)
+    status_response = ec2.describe_instance_status(InstanceIds=[target_instance_id])
+
+    if status_response["InstanceStatuses"]:
+        instance_status = status_response["InstanceStatuses"][0]
+        system_status = instance_status["SystemStatus"]["Status"]
+        instance_status_check = instance_status["InstanceStatus"]["Status"]
+
+        if system_status == "ok" and instance_status_check == "ok":
+            instance_state = instance_status["InstanceState"]["Name"]
+
+            if instance_state == "running":
+                try:
+                    response = ec2.stop_instances(InstanceIds=[target_instance_id])
+                    message = "Instance for challenge {} successfully stopped.".format(challenge.pk)
+                    return {
+                        "response": response,
+                        "message": message,
+                    }
+                except ClientError as e:
+                    logger.exception(e)
+                    return {
+                        "error": e.response,
+                    }
+            else:
+                message = "Instance for challenge {} is not running. Please ensure the instance is running.".format(challenge.pk)
                 return {
-                    "error": e.response,
+                    "error": message,
                 }
         else:
-            message = "Instance for challenge {} is not running. Please ensure the instance is running.".format(
-                challenge.pk
-            )
+            message = "Instance status checks are not ready for challenge {}. Please wait for the status checks to pass.".format(challenge.pk)
             return {
                 "error": message,
             }
     else:
-        message = "Instance for challenge {} not found. Please ensure the instance exists.".format(
-            challenge.pk
-        )
+        message = "Instance for challenge {} not found. Please ensure the instance exists.".format(challenge.pk)
         return {
             "error": message,
         }


Test patch: