-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ZMQ queue class and task for asynchronous reporting
- Loading branch information
Showing
15 changed files
with
169 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .base import BaseQueue # NOQA | ||
from .celery import CeleryQueue # NOQA | ||
from .rq import RQQueue # NOQA | ||
from .zmq import ZMQQueue # NOQA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from __future__ import absolute_import | ||
|
||
import logging | ||
|
||
try: | ||
import zmq | ||
except ImportError: | ||
zmq = None | ||
|
||
from kaneda.exceptions import ImproperlyConfigured | ||
|
||
from .base import BaseQueue | ||
|
||
|
||
class ZMQQueue(BaseQueue): | ||
""" | ||
ZeroMQ queue | ||
:param connection_url: ZMQ connection url (tcp://127.0.0.1:5555). | ||
:param timeout: ZMQ socket timeout (milliseconds). | ||
""" | ||
settings_namespace = 'ZMQ' | ||
|
||
def __init__(self, connection_url, timeout=300): | ||
if not zmq: | ||
raise ImproperlyConfigured('You need to install pyzmq to use the ZMQ queue.') | ||
context = zmq.Context() | ||
self.socket = context.socket(zmq.PUSH) | ||
self.socket.SNDTIMEO = timeout | ||
self.socket.bind(connection_url) | ||
|
||
def report(self, name, metric, value, tags, id_): | ||
payload = locals() | ||
del payload['self'] | ||
try: | ||
return self.socket.send_json(payload) | ||
except Exception as e: | ||
logger = logging.getLogger(__name__) | ||
logger.exception(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .zmq import zmq_task # NOQA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from __future__ import absolute_import | ||
|
||
from datetime import datetime | ||
|
||
import click | ||
import zmq | ||
|
||
from kaneda.exceptions import SettingsError | ||
from kaneda.utils import get_backend, get_settings | ||
|
||
|
||
@click.command() | ||
@click.option('--connection_url', '-u', help='ZMQ connection url, e.g: tcp://127.0.0.1:5555') | ||
def zmq_task(connection_url): | ||
""" | ||
ZMQ job to report metrics to the configured backend in kanedasettings.py | ||
To run the worker execute this command: | ||
zmqworker --connection_url=<zmq_connection_url> | ||
""" | ||
if not connection_url: | ||
try: | ||
settings = get_settings() | ||
connection_url = settings.ZMQ_CONNECTION_URL | ||
except ImportError: | ||
raise SettingsError("Pass --connection_url option or define ZMQ_CONNECTION_URL on Kaneda settings file " | ||
"before use ZMQ task processor.") | ||
backend = get_backend() | ||
context = zmq.Context() | ||
socket = context.socket(zmq.PULL) | ||
socket.connect(connection_url) | ||
poller = zmq.Poller() | ||
poller.register(socket) | ||
click.secho('Running ZMQ worker - listening at {}.'.format(connection_url), fg='blue') | ||
click.secho('Using {}.'.format(backend.__class__.__name__), fg='blue') | ||
click.echo('\n') | ||
while True: | ||
events = dict(poller.poll(0)) | ||
if socket in events: | ||
payload = socket.recv_json() | ||
click.secho('[{}: Received data] {}'.format(datetime.utcnow(), payload), fg='green') | ||
backend.report(**payload) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ deps = | |
influxdb | ||
celery | ||
rq | ||
pyzmq | ||
redis | ||
six==1.10.0 | ||
|
||
|