2014-02-25 19:13:31 +00:00
|
|
|
import beanstalkc
|
|
|
|
import yaml
|
|
|
|
import logging
|
2014-04-28 19:35:35 +00:00
|
|
|
import sys
|
|
|
|
from collections import OrderedDict
|
2014-02-25 19:13:31 +00:00
|
|
|
|
|
|
|
from .config import config
|
2014-04-28 19:35:35 +00:00
|
|
|
from . import report
|
2014-02-25 19:13:31 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2014-03-25 20:27:54 +00:00
|
|
|
|
2014-04-15 00:28:54 +00:00
|
|
|
def connect():
|
|
|
|
host = config.queue_host
|
|
|
|
port = config.queue_port
|
|
|
|
if host is None or port is None:
|
2014-02-25 19:13:31 +00:00
|
|
|
raise RuntimeError(
|
|
|
|
'Beanstalk queue information not found in {conf_path}'.format(
|
|
|
|
conf_path=config.teuthology_yaml))
|
2014-04-15 00:28:54 +00:00
|
|
|
return beanstalkc.Connection(host=host, port=port)
|
|
|
|
|
2014-02-25 19:13:31 +00:00
|
|
|
|
2014-04-15 00:28:54 +00:00
|
|
|
def watch_tube(connection, tube_name):
|
|
|
|
connection.watch(tube_name)
|
|
|
|
connection.ignore('default')
|
2014-03-25 20:27:54 +00:00
|
|
|
|
2014-04-15 00:28:54 +00:00
|
|
|
|
2014-04-28 19:35:35 +00:00
|
|
|
def walk_jobs(connection, tube_name, callback, pattern=None):
|
|
|
|
"""
|
|
|
|
def callback(jobs_dict)
|
|
|
|
"""
|
2014-04-15 00:28:54 +00:00
|
|
|
log.info("Checking Beanstalk Queue...")
|
2014-04-28 19:35:35 +00:00
|
|
|
job_count = connection.stats_tube(tube_name)['current-jobs-ready']
|
2014-03-25 20:27:54 +00:00
|
|
|
if job_count == 0:
|
|
|
|
log.info('No jobs in Beanstalk Queue')
|
|
|
|
return
|
2014-04-28 19:35:35 +00:00
|
|
|
|
2014-04-29 16:30:50 +00:00
|
|
|
# Try to figure out a sane timeout based on how many jobs are in the queue
|
|
|
|
timeout = job_count / 2000.0 * 60
|
2014-04-28 19:35:35 +00:00
|
|
|
matching_jobs = OrderedDict()
|
|
|
|
for i in range(1, job_count + 1):
|
|
|
|
sys.stderr.write("{i}/{count}\r".format(i=i, count=job_count))
|
|
|
|
sys.stderr.flush()
|
2014-04-29 16:30:50 +00:00
|
|
|
job = connection.reserve(timeout=timeout)
|
2014-04-28 19:35:35 +00:00
|
|
|
if job is None or job.body is None:
|
|
|
|
continue
|
|
|
|
job_config = yaml.safe_load(job.body)
|
|
|
|
job_name = job_config['name']
|
|
|
|
job_id = job.stats()['id']
|
|
|
|
if pattern is not None and pattern not in job_name:
|
|
|
|
continue
|
|
|
|
matching_jobs[job_id] = job
|
|
|
|
sys.stderr.write('\n')
|
|
|
|
sys.stderr.flush()
|
|
|
|
callback(matching_jobs)
|
|
|
|
|
|
|
|
|
|
|
|
def _print_matching_jobs(show_desc=False):
|
|
|
|
def print_matching_jobs(jobs_dict):
|
|
|
|
i = 0
|
|
|
|
job_count = len(jobs_dict)
|
|
|
|
for job_id, job in jobs_dict.iteritems():
|
|
|
|
i += 1
|
2014-03-25 20:27:54 +00:00
|
|
|
job_config = yaml.safe_load(job.body)
|
|
|
|
job_name = job_config['name']
|
2014-04-28 19:35:35 +00:00
|
|
|
job_desc = job_config['description']
|
2014-03-25 20:27:54 +00:00
|
|
|
job_id = job.stats()['id']
|
2014-04-28 19:35:35 +00:00
|
|
|
print 'Job: {i}/{count} {job_name}/{job_id}'.format(
|
|
|
|
i=i,
|
|
|
|
count=job_count,
|
|
|
|
job_id=job_id,
|
|
|
|
job_name=job_name,
|
|
|
|
)
|
|
|
|
if job_desc and show_desc:
|
|
|
|
for desc in job_desc.split():
|
|
|
|
print '\t {desc}'.format(desc=desc)
|
|
|
|
return print_matching_jobs
|
|
|
|
|
|
|
|
|
|
|
|
def delete_matching_jobs(jobs_dict):
|
|
|
|
for job_id, job in jobs_dict.iteritems():
|
|
|
|
job_config = yaml.safe_load(job.body)
|
|
|
|
job_name = job_config['name']
|
|
|
|
job_id = job.stats()['id']
|
|
|
|
print 'Deleting {job_id}/{job_name}'.format(
|
|
|
|
job_id=job_id,
|
|
|
|
job_name=job_name,
|
|
|
|
)
|
|
|
|
job.delete()
|
|
|
|
report.try_delete_jobs(job_name, job_id)
|
|
|
|
|
|
|
|
|
|
|
|
def print_matching_runs(jobs_dict):
|
|
|
|
runs = set()
|
|
|
|
for job_id, job in jobs_dict.iteritems():
|
|
|
|
job_config = yaml.safe_load(job.body)
|
|
|
|
runs.add(job_config['name'])
|
|
|
|
for run in runs:
|
|
|
|
print run
|
2014-03-25 20:27:54 +00:00
|
|
|
|
2014-02-25 19:13:31 +00:00
|
|
|
|
|
|
|
def main(args):
|
2014-03-25 20:27:54 +00:00
|
|
|
machine_type = args['--machine_type']
|
2014-02-25 19:13:31 +00:00
|
|
|
delete = args['--delete']
|
2014-04-29 16:26:39 +00:00
|
|
|
runs = args['--runs']
|
2014-03-25 20:38:34 +00:00
|
|
|
show_desc = args['--description']
|
2014-03-25 20:40:56 +00:00
|
|
|
try:
|
2014-04-15 00:28:54 +00:00
|
|
|
connection = connect()
|
|
|
|
watch_tube(connection, machine_type)
|
2014-04-28 19:35:35 +00:00
|
|
|
if delete:
|
|
|
|
walk_jobs(connection, machine_type,
|
|
|
|
delete_matching_jobs)
|
2014-04-29 16:26:39 +00:00
|
|
|
elif runs:
|
|
|
|
walk_jobs(connection, machine_type,
|
|
|
|
print_matching_runs)
|
2014-04-28 19:35:35 +00:00
|
|
|
else:
|
|
|
|
walk_jobs(connection, machine_type,
|
|
|
|
_print_matching_jobs(show_desc))
|
2014-03-25 20:40:56 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
log.info("Interrupted.")
|
|
|
|
finally:
|
2014-04-15 00:28:54 +00:00
|
|
|
connection.close()
|