ceph/teuthology/beanstalk.py
Zack Cerza 1449e753ab Use shared methods to connect to beanstalkd
Signed-off-by: Zack Cerza <zack.cerza@inktank.com>
2014-04-17 12:27:41 -05:00

78 lines
2.4 KiB
Python

import beanstalkc
import yaml
import logging
from .config import config
log = logging.getLogger(__name__)
def connect():
host = config.queue_host
port = config.queue_port
if host is None or port is None:
raise RuntimeError(
'Beanstalk queue information not found in {conf_path}'.format(
conf_path=config.teuthology_yaml))
return beanstalkc.Connection(host=host, port=port)
def watch_tube(connection, tube_name):
connection.watch(tube_name)
connection.ignore('default')
def walk_jobs(connection, machine_type, show_desc=False, delete=None):
log.info("Checking Beanstalk Queue...")
job_count = connection.stats_tube(machine_type)['current-jobs-ready']
if job_count == 0:
log.info('No jobs in Beanstalk Queue')
return
x = 1
while x < job_count:
x += 1
job = connection.reserve(timeout=20)
if job is not None and job.body is not None:
job_config = yaml.safe_load(job.body)
job_name = job_config['name']
job_id = job.stats()['id']
job_description = job_config['description']
if delete:
if delete in job_name:
m = 'Deleting {job_id}/{job_name}'.format(
job_id=job_id,
job_name=job_name,
)
print m
job.delete()
else:
m = "Searching queue... Checked {x}/{count} Jobs\r".format(
x=x, count=job_count)
print m,
else:
m = 'Job: {x}/{count} {job_name}/{job_id}'.format(
x=x,
count=job_count,
job_id=job_id,
job_name=job_name,
)
print m
if job_description and show_desc:
for desc in job_description.split():
print '\t {desc}'.format(desc=desc)
log.info("Finished checking Beanstalk Queue.")
def main(args):
machine_type = args['--machine_type']
delete = args['--delete']
show_desc = args['--description']
try:
connection = connect()
watch_tube(connection, machine_type)
walk_jobs(connection, machine_type, show_desc=show_desc, delete=delete)
except KeyboardInterrupt:
log.info("Interrupted.")
finally:
connection.close()