#!/usr/bin/env python
# Copyright (c) 2012-2013 SwiftStack, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# When you're developing things and something goes wrong, your work
# queue can wind up full of bogus jobs. Use this to just wipe them all out.
import argparse
import beanstalkc
import sys

import ssbench

if __name__ == "__main__":
    arg_parser = argparse.ArgumentParser(description='Drain the queue')
    arg_parser.add_argument('--qhost', default="localhost")
    arg_parser.add_argument('--qport', default=11300, type=int)

    args = arg_parser.parse_args(sys.argv[1:])

    beanq = beanstalkc.Connection(host=args.qhost, port=args.qport)
    beanq.watch(ssbench.STATS_TUBE)
    beanq.watch(ssbench.WORK_TUBE)

    job = beanq.reserve(timeout=1)
    while job:
        job.delete()
        print ".",
        job = beanq.reserve(timeout=1)
