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