Created
January 30, 2022 17:47
-
-
Save moyix/a234bd85c14517e18b4fc4df0dc417f3 to your computer and use it in GitHub Desktop.
Script that kills any Github Actions Runner worker that has been running longer than 5 minutes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import psutil | |
import docker | |
from datetime import timedelta, datetime | |
import time | |
client = docker.from_env() | |
while True: | |
time.sleep(10) | |
try: | |
c = client.containers.get('ephemeral-github-actions-runner.service') | |
except docker.errors.NotFound as e: | |
continue | |
try: | |
for UID, PID, PPID, C, STIME, TTY, TIME, CMD in c.top()['Processes']: | |
if 'Runner.Worker' in CMD: | |
p = psutil.Process(int(PID)) | |
elapsed = datetime.now() - datetime.fromtimestamp(p.create_time()) | |
if elapsed > timedelta(minutes=5): | |
print(f"[{datetime.now()}] Found runner job older than 5 minutes ({elapsed}): {UID} {PID} {PPID} {C} {STIME} {TTY} {TIME} {CMD}") | |
print("Killing...") | |
p.terminate() | |
except docker.errors.APIError as e: | |
continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment