Created
May 24, 2017 23:38
-
-
Save gregarndt/83288a6b091719731a5934532063a0e2 to your computer and use it in GitHub Desktop.
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
import os | |
from datetime import datetime | |
import psycopg2 | |
conn = psycopg2.connect(os.environ['PGCONNECTIONSTRING']) | |
cur = conn.cursor() | |
# this should be a list that TC could be using that is not currently allocated | |
# to buildbot | |
machines = ["t-yosemite-r7-{:04d}".format(i) for i in range(300)] | |
machine_data = {} | |
def get_machine_state(worker_id): | |
machine_data = { | |
'elapsed_since_job': None, | |
'elapsed_since_job_secs': 0, | |
'elapsed_on_job': 0, | |
'slave_state': 'no jobs', | |
'starttime': None, # not sure what this is | |
'slave_class': 'test', | |
'row_class': 'no jobs', | |
} | |
# we only care about the last 2 tasks that a worker might have executed | |
cur.execute("select state,scheduled,started,resolved from tasks where worker_id = %s order by started desc limit 1", (machine,)) | |
if cur.rowcount == 0: | |
return machine_data | |
for r in cur: | |
if r[0] == 'running': | |
machine_data['row_class'] = 'working' | |
machine_data['slave_state'] = 'working' | |
machine_data['elapsed_on_job'] = datetime.now() - started | |
return machine_data | |
# if the task is not running, assumed exception or resolved | |
since = (datetime.utcnow() - r[3]).total_seconds() | |
m, s = divmod(since, 60) | |
h, m = divmod(m, 60) | |
machine_data['elapsed_since_job'] = "{%d:%02d:%02d}" % (h, m, s) | |
machine_data['elapsed_since_job_secs'] = since | |
if since/3600.0 > 5: | |
machine_data['row_class'] = machine_data['slave_state'] = 'broken' | |
return machine_data | |
try: | |
for machine in machines: | |
machine_data[machine] = get_machine_state(machine) | |
print machine_data | |
finally: | |
cur.close() | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment