Beispiel #1
0
    def handle(self, *args, **options):

        if options.get("json"):
            import json
            click.echo(json.dumps(get_statistics()))
            return

        if options.get("yaml"):
            try:
                import yaml
            except ImportError:
                click.echo("Aborting. LibYAML is not installed.")
                return
            # Disable YAML alias
            yaml.Dumper.ignore_aliases = lambda *args: True
            click.echo(yaml.dump(get_statistics(), default_flow_style=False))
            return

        self.interval = options.get("interval")

        # Arbitrary
        self.table_width = 78

        # Do not continously poll
        if not self.interval:
            self._print_stats_dashboard(get_statistics())
            return

        # Abuse clicks to 'live' render CLI dashboard TODO: Use curses instead
        try:
            while True:
                self._print_stats_dashboard(get_statistics())
                time.sleep(self.interval)
        except KeyboardInterrupt:
            pass
Beispiel #2
0
    def handle(self, *args, **options):

        if options.get("json"):
            import json
            click.echo(json.dumps(get_statistics()))
            return

        if options.get("yaml"):
            try:
                import yaml
            except ImportError:
                click.echo("Aborting. LibYAML is not installed.")
                return
            # Disable YAML alias
            yaml.Dumper.ignore_aliases = lambda *args: True
            click.echo(yaml.dump(get_statistics(), default_flow_style=False))
            return

        self.interval = options.get("interval")

        # Arbitrary
        self.table_width = 78

        # Do not continously poll
        if not self.interval:
            self._print_stats_dashboard(get_statistics())
            return

        # Abuse clicks to 'live' render CLI dashboard TODO: Use curses instead
        try:
            while True:
                self._print_stats_dashboard(get_statistics())
                time.sleep(self.interval)
        except KeyboardInterrupt:
            pass
def metric_rq():
    """Return stats about RQ Worker in Prometheus Metric format.

    Return:
        Iterator[GaugeMetricFamily]
            netbox_queue_number_jobs: Nbr Job per RQ queue and status
            netbox_queue_number_workers: Nbr worker per queue
    """
    queue_stats = get_statistics()

    job = GaugeMetricFamily("netbox_queue_number_jobs",
                            "Number of Job per RQ queue and status",
                            labels=["name", "status"])
    worker = GaugeMetricFamily("netbox_queue_number_workers",
                               "Number of worker per queue",
                               labels=["name"])

    if "queues" in queue_stats:
        for queue in queue_stats["queues"]:
            for status in [
                    "finished", "started", "deferred", "failed", "scheduled"
            ]:
                if f"{status}_jobs" not in queue.keys():
                    continue
                job.add_metric([queue["name"], status],
                               queue[f"{status}_jobs"])

            if "workers" in queue.keys():
                worker.add_metric([queue["name"]], queue["workers"])

    yield job
    yield worker
Beispiel #4
0
    def test_get_statistics(self):
        """get_statistics() returns the right number of workers"""
        queues = [{
            'connection_config': {
                'DB': 0,
                'HOST': 'localhost',
                'PORT': 6379,
            },
            'name': 'async'
        }]

        with patch('django_rq.utils.QUEUES_LIST',
                   new_callable=PropertyMock(return_value=queues)):
            worker = get_worker('async', name=uuid4().hex)
            worker.register_birth()
            statistics = get_statistics()
            data = statistics['queues'][0]
            self.assertEqual(data['name'], 'async')
            self.assertEqual(data['workers'], 1)
            worker.register_death()
Beispiel #5
0
    def __init__(self, queue):

        self.stats = get_statistics()
        self.queue_name = queue