BVB Source Codes

rq Show helpers.py Source code

Return Download rq: download helpers.py Source code - Download rq Source code - Type:.py
  1. # -*- coding: utf-8 -*-
  2. from __future__ import (absolute_import, division, print_function,
  3.                         unicode_literals)
  4.  
  5. import importlib
  6. import time
  7. from functools import partial
  8.  
  9. import click
  10. import redis
  11. from redis import StrictRedis
  12. from rq import Queue, Worker
  13. from rq.logutils import setup_loghandlers
  14. from rq.worker import WorkerStatus
  15.  
  16. red = partial(click.style, fg='red')
  17. green = partial(click.style, fg='green')
  18. yellow = partial(click.style, fg='yellow')
  19.  
  20.  
  21. def read_config_file(module):
  22.     """Reads all UPPERCASE variables defined in the given module file."""
  23.     settings = importlib.import_module(module)
  24.     return dict([(k, v)
  25.                  for k, v in settings.__dict__.items()
  26.                  if k.upper() == k])
  27.  
  28.  
  29. def get_redis_from_config(settings, connection_class=StrictRedis):
  30.     """Returns a StrictRedis instance from a dictionary of settings."""
  31.     if settings.get('REDIS_URL') is not None:
  32.         return connection_class.from_url(settings['REDIS_URL'])
  33.  
  34.     kwargs = {
  35.         'host': settings.get('REDIS_HOST', 'localhost'),
  36.         'port': settings.get('REDIS_PORT', 6379),
  37.         'db': settings.get('REDIS_DB', 0),
  38.         'password': settings.get('REDIS_PASSWORD', None),
  39.     }
  40.  
  41.     use_ssl = settings.get('REDIS_SSL', False)
  42.     if use_ssl:
  43.         # If SSL is required, we need to depend on redis-py being 2.10 at
  44.         # least
  45.         def safeint(x):
  46.             try:
  47.                 return int(x)
  48.             except ValueError:
  49.                 return 0
  50.  
  51.         version_info = tuple(safeint(x) for x in redis.__version__.split('.'))
  52.         if not version_info >= (2, 10):
  53.             raise RuntimeError('Using SSL requires a redis-py version >= 2.10')
  54.         kwargs['ssl'] = use_ssl
  55.     return connection_class(**kwargs)
  56.  
  57.  
  58. def pad(s, pad_to_length):
  59.     """Pads the given string to the given length."""
  60.     return ('%-' + '%ds' % pad_to_length) % (s,)
  61.  
  62.  
  63. def get_scale(x):
  64.     """Finds the lowest scale where x <= scale."""
  65.     scales = [20, 50, 100, 200, 400, 600, 800, 1000]
  66.     for scale in scales:
  67.         if x <= scale:
  68.             return scale
  69.     return x
  70.  
  71.  
  72. def state_symbol(state):
  73.     symbols = {
  74.         WorkerStatus.BUSY: red('busy'),
  75.         WorkerStatus.IDLE: green('idle'),
  76.         WorkerStatus.SUSPENDED: yellow('suspended'),
  77.     }
  78.     try:
  79.         return symbols[state]
  80.     except KeyError:
  81.         return state
  82.  
  83.  
  84. def show_queues(queues, raw, by_queue):
  85.     if queues:
  86.         qs = list(map(Queue, queues))
  87.     else:
  88.         qs = Queue.all()
  89.  
  90.     num_jobs = 0
  91.     termwidth, _ = click.get_terminal_size()
  92.     chartwidth = min(20, termwidth - 20)
  93.  
  94.     max_count = 0
  95.     counts = dict()
  96.     for q in qs:
  97.         count = q.count
  98.         counts[q] = count
  99.         max_count = max(max_count, count)
  100.     scale = get_scale(max_count)
  101.     ratio = chartwidth * 1.0 / scale
  102.  
  103.     for q in qs:
  104.         count = counts[q]
  105.         if not raw:
  106.             chart = green('|' + '鈻? * int(ratio * count))
  107.            line = '%-12s %s %d' % (q.name, chart, count)
  108.        else:
  109.            line = 'queue %s %d' % (q.name, count)
  110.        click.echo(line)
  111.  
  112.        num_jobs += count
  113.  
  114.    # print summary when not in raw mode
  115.    if not raw:
  116.        click.echo('%d queues, %d jobs total' % (len(qs), num_jobs))
  117.  
  118.  
  119. def show_workers(queues, raw, by_queue):
  120.    if queues:
  121.        qs = list(map(Queue, queues))
  122.  
  123.        def any_matching_queue(worker):
  124.            def queue_matches(q):
  125.                return q in qs
  126.            return any(map(queue_matches, worker.queues))
  127.  
  128.        # Filter out workers that don't match the queue filter
  129.         ws = [w for w in Worker.all() if any_matching_queue(w)]
  130.  
  131.         def filter_queues(queue_names):
  132.             return [qname for qname in queue_names if Queue(qname) in qs]
  133.  
  134.     else:
  135.         qs = Queue.all()
  136.         ws = Worker.all()
  137.         filter_queues = (lambda x: x)
  138.  
  139.     if not by_queue:
  140.         for w in ws:
  141.             worker_queues = filter_queues(w.queue_names())
  142.             if not raw:
  143.                 click.echo('%s %s: %s' % (w.name, state_symbol(w.get_state()), ', '.join(worker_queues)))
  144.             else:
  145.                 click.echo('worker %s %s %s' % (w.name, w.get_state(), ','.join(worker_queues)))
  146.     else:
  147.         # Create reverse lookup table
  148.         queues = dict([(q, []) for q in qs])
  149.         for w in ws:
  150.             for q in w.queues:
  151.                 if q not in queues:
  152.                     continue
  153.                 queues[q].append(w)
  154.  
  155.         max_qname = max(map(lambda q: len(q.name), queues.keys())) if queues else 0
  156.         for q in queues:
  157.             if queues[q]:
  158.                 queues_str = ", ".join(sorted(map(lambda w: '%s (%s)' % (w.name, state_symbol(w.get_state())), queues[q])))  # noqa
  159.             else:
  160.                 queues_str = '鈥?
  161.            click.echo('%s %s' % (pad(q.name + ':', max_qname + 1), queues_str))
  162.  
  163.    if not raw:
  164.        click.echo('%d workers, %d queues' % (len(ws), len(qs)))
  165.  
  166.  
  167. def show_both(queues, raw, by_queue):
  168.    show_queues(queues, raw, by_queue)
  169.    if not raw:
  170.        click.echo('')
  171.    show_workers(queues, raw, by_queue)
  172.    if not raw:
  173.        click.echo('')
  174.        import datetime
  175.        click.echo('Updated: %s' % datetime.datetime.now())
  176.  
  177.  
  178. def refresh(interval, func, *args):
  179.    while True:
  180.        if interval:
  181.            click.clear()
  182.        func(*args)
  183.        if interval:
  184.            time.sleep(interval)
  185.        else:
  186.            break
  187.  
  188.  
  189. def setup_loghandlers_from_args(verbose, quiet):
  190.    if verbose and quiet:
  191.        raise RuntimeError("Flags --verbose and --quiet are mutually exclusive.")
  192.  
  193.    if verbose:
  194.        level = 'DEBUG'
  195.    elif quiet:
  196.        level = 'WARNING'
  197.    else:
  198.        level = 'INFO'
  199.    setup_loghandlers(level)
  200.  
downloadhelpers.py Source code - Download rq Source code
Related Source Codes/Software:
amazon-dsstne - Deep Scalable Sparse Tensor Network Engine (DSSTNE... 2017-01-08
webpack-demos - a collection of simple demos of Webpack 2017-01-08
Squire - HTML5 rich text editor. Try the demo integration a... 2017-01-08
thor - Thor is a toolkit for building powerful command-li... 2017-01-08
glide - Package Management for Golang h... 2017-01-08
emmet-vim - emmet for vim: http://emmet.io/ ... 2017-01-08
prose - A Content Editor for GitHub. ht... 2017-01-08
sshrc - ring your .bashrc, .vimrc, etc. with you when you ... 2017-01-08
typed.js - A jQuery typing animation script. ... 2017-01-08
find - High-precision indoor positioning framework for mo... 2017-01-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top