Simplify get_dead_runners_in_ec2 a little bit, update runners

This commit is contained in:
Mikhail f. Shiryaev 2023-06-01 22:03:42 +02:00
parent b775b5cfd6
commit 3c13eaa159
No known key found for this signature in database
GPG Key ID: 4B02ED204C7D93F4

View File

@ -99,17 +99,22 @@ def get_dead_runners_in_ec2(runners: RunnerDescriptions) -> RunnerDescriptions:
def get_lost_ec2_instances(runners: RunnerDescriptions) -> List[dict]: def get_lost_ec2_instances(runners: RunnerDescriptions) -> List[dict]:
client = boto3.client("ec2") client = boto3.client("ec2")
reservations = client.describe_instances( reservations = client.describe_instances(
Filters=[{"Name": "tag-key", "Values": ["github:runner-type"]}] Filters=[{"Name": "tag-key", "Values": ["github:runner-type"]}],
)["Reservations"] )["Reservations"]
lost_instances = [] # flatten the reservation into instances
offline_runners = [ instances = [
runner.name for runner in runners if runner.offline and not runner.busy instance
for reservation in reservations
for instance in reservation["Instances"]
] ]
# Here we refresh the runners to get the most recent state lost_instances = []
offline_runner_names = {
runner.name for runner in runners if runner.offline and not runner.busy
}
runner_names = {runner.name for runner in runners}
now = datetime.now().timestamp() now = datetime.now().timestamp()
for reservation in reservations: for instance in instances:
for instance in reservation["Instances"]:
# Do not consider instances started 20 minutes ago as problematic # Do not consider instances started 20 minutes ago as problematic
if now - instance["LaunchTime"].timestamp() < 1200: if now - instance["LaunchTime"].timestamp() < 1200:
continue continue
@ -120,21 +125,16 @@ def get_lost_ec2_instances(runners: RunnerDescriptions) -> List[dict]:
if tag["Key"] == "github:runner-type" if tag["Key"] == "github:runner-type"
][0] ][0]
# If there's no necessary labels in runner type it's fine # If there's no necessary labels in runner type it's fine
if not ( if not (UNIVERSAL_LABEL in runner_type or runner_type in RUNNER_TYPE_LABELS):
UNIVERSAL_LABEL in runner_type or runner_type in RUNNER_TYPE_LABELS
):
continue continue
if instance["InstanceId"] in offline_runners: if instance["InstanceId"] in offline_runner_names:
lost_instances.append(instance) lost_instances.append(instance)
continue continue
if instance["State"]["Name"] == "running" and ( if (
not [ instance["State"]["Name"] == "running"
runner and not instance["InstanceId"] in runner_names
for runner in runners
if runner.name == instance["InstanceId"]
]
): ):
lost_instances.append(instance) lost_instances.append(instance)