Created
February 11, 2022 16:41
-
-
Save felipevolpatto/a403956d1de7d6e7857b1488be0aadc1 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 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