Exemplo n.º 1
0
def test_queue_all():
    q1 = Queue('a')
    q2 = Queue('b')
    q3 = Queue('c')
    q2.enqueue_call()
    q3.enqueue_call()
    assert [x.name for x in Queue.all()] == ['b', 'c']
    q2.delete()
    q3.empty()
    q1.enqueue_call()
    assert [x.name for x in Queue.all()] == ['a', 'c']
Exemplo n.º 2
0
def test_empty(cli_run, stub):
    queues = [QueueFactory() for i in range(5)]
    for q in queues:
        q.enqueue_call(stub)

    with pytest.raises(click.UsageError):
        cli_run('empty')

    assert all(q.count() == 1 for q in queues)

    cli_run('empty', queues[0].name, queues[1].name)
    assert queues[0].count() == 0
    assert queues[1].count() == 0
    assert queues[2].count() == 1

    assert set(queues) == set(Queue.all())
    cli_run('empty', '--delete', queues[1].name, queues[2].name)
    assert set(queues) - {queues[1], queues[2]} == set(Queue.all())

    cli_run('empty', '--all')
    assert all(q.count() == 0 for q in queues)
Exemplo n.º 3
0
def empty(all, delete, queues, **options):
    """Empty selected queues"""

    if all:
        queues = Queue.all()
    elif queues:
        queues = [Queue(q) for q in queues]
    else:
        raise click.UsageError("No queues specified")

    if not queues:
        click.echo('Nothing to do')
        sys.exit(0)

    for queue in queues:
        if delete:
            queue.delete()
        else:
            queue.empty()
        click.echo(f'Queue f{queue.name} emptied')
Exemplo n.º 4
0
def show_workers(queues, by_queue):
    def state_symbol(state):
        return {
            WorkerState.BUSY: red('busy'),
            WorkerState.IDLE: green('idle')
        }.get(state, state)

    workers = Worker.all()
    all_queues = set(Queue.all()) | {q for w in workers for q in w.queues}
    if queues:
        workers = [w for w in workers if any(set(w.queues) & set(queues))]
    else:
        queues = all_queues

    if not by_queue:
        click.secho("Workers", bold=True)
    else:
        click.secho("Workers per Queue", bold=True)
    print_separator()
    if not by_queue:
        for worker in sorted(workers, key=attrgetter('description')):
            worker_queues = ', '.join(q.name for q in worker.queues)
            click.echo(
                f'{worker.description} {state_symbol(worker.state)}: {worker_queues}'
            )
    else:
        queue_map = {q: [] for q in all_queues}
        for w in workers:
            for q in w.queues:
                queue_map[q].append(w)
        max_qname = max(len(q.name) for q in queues)
        for queue in sorted(queues, key=attrgetter('name')):
            q_workers = queue_map[queue]
            workers_str = ", ".join(
                sorted(f'{w.description} {state_symbol(w.state)}'
                       for w in q_workers))
            if not workers_str:
                workers_str = '–'
            click.echo(f'{queue.name:>{max_qname}}: {workers_str}')

    click.echo(f'{len(workers)} worker(s)')
Exemplo n.º 5
0
def show_queues(queues):
    if not queues:
        queues = Queue.all()

    counts = {q: q.count() for q in queues}

    chart_width = 20
    chart_max = max((1, *counts.values()))
    # Round up to the next fixed marker or the next power of two times 1000
    chart_max = next(
        (x for x in [20, 50, 100, 200, 400, 600, 800, 1000] if x >= chart_max),
        2**math.ceil(math.log2(chart_max / 1000)) * 1000)

    click.secho("Queues", bold=True)
    print_separator()
    for q in sorted(queues, key=attrgetter('name')):
        count = counts[q]
        chart = green('|' + '█' * int(count / chart_max * chart_width))
        click.echo(f'{q.name:<12} {chart} {count}')

    click.echo(f'{len(queues)} queue(s), {sum(counts.values())} task(s) total')