Commit 99e3fb5e authored by Stevan Radakovic's avatar Stevan Radakovic Committed by Neil Williams
Browse files

Add zmq_client example script to docs.

Move notifications yaml to separate directory as well.
Update documentation.

Change-Id: I495462c2863dc01a933953b998d138aa33bf8afa
parent cd718036
......@@ -139,9 +139,29 @@ being enabled.
.. seealso:: :ref:`configuring_event_notifications`
Work is ongoing to create a simple ``lava-client`` library which can be used to
connect to the push notifications and replace the need to poll on XML-RPC. In
the meantime, if you are interested in using event notifications for a custom
:term:`frontend`, you might want to look at the code for the example website:
https://github.com/ivoire/ReactOWeb
Write your own event notification client
========================================
It is quite straight forward to get events from ``lava-publisher``.
Users can embed this example piece code in their own local client app to
listen to the job and/or device events and act according to the return data.
This script can also be used standalone from command line but is otherwise only
an example.
.. code-block:: bash
python zmq_client.py -j 357 -p tcp://127.0.0.1:5500 -t 1200
zmq_client.py script:
.. include:: examples/source/zmq_client.py
:code: python
:start-after: # START_CLIENT
:end-before: # END_CLIENT
If you are interested in using event notifications for a custom :term:`frontend`,
you might want also to look at the code for the ReactOWeb example website:
https://github.com/ivoire/ReactOWeb
# START_CLIENT
import argparse
import yaml
import logging
import re
import signal
import time
import zmq
from zmq.utils.strtypes import b, u
FINISHED_JOB_STATUS = ["Complete", "Incomplete", "Canceled"]
class JobEndTimeoutError(Exception):
""" Raise when the specified job does not finish in certain timeframe. """
class Timeout():
""" Timeout error class with ALARM signal. Accepts time in seconds. """
class TimeoutError(Exception):
pass
def __init__(self, sec):
self.sec = sec
def __enter__(self):
signal.signal(signal.SIGALRM, self.timeout_raise)
signal.alarm(self.sec)
def __exit__(self, *args):
signal.alarm(0)
def timeout_raise(self, *args):
raise Timeout.TimeoutError()
class JobListener():
def __init__(self, url):
self.context = zmq.Context.instance()
self.sock = self.context.socket(zmq.SUB)
self.sock.setsockopt(zmq.SUBSCRIBE, b"")
self.sock.connect(url)
def wait_for_job_end(self, job_id, timeout=None):
try:
with Timeout(timeout):
while True:
msg = self.sock.recv_multipart()
try:
(topic, uuid, dt, username, data) = msg[:]
except IndexError:
# Droping invalid message
continue
data = yaml.safe_load(data)
if "job" in data:
if data["job"] == job_id:
if data["status"] in FINISHED_JOB_STATUS:
return data
except Timeout.TimeoutError:
raise JobEndTimeoutError(
"JobListener timed out after %s seconds." % timeout)
def main():
# Parse the command line
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--publisher", default="tcp://127.0.0.1:5500",
help="Publisher host and port")
parser.add_argument("-j", "--job-id", type=int,
help="Job ID to wait for")
parser.add_argument("-t", "--timeout", type=int,
help="Timeout in seconds")
options = parser.parse_args()
listener = JobListener(options.publisher)
print listener.wait_for_job_end(options.job_id, options.timeout)
if __name__ == '__main__':
main()
# END_CLIENT
......@@ -49,7 +49,7 @@ must be specified in full.
Examples for user vs manual addressing:
.. include:: examples/test-jobs/notifications.yaml
.. include:: examples/source/notifications.yaml
:code: yaml
:start-after: # notify recipients block
:end-before: # notify compare block
......@@ -79,12 +79,12 @@ the notification block.
Here are some comparing setup examples from test definition excerpts:
.. include:: examples/test-jobs/notifications.yaml
.. include:: examples/source/notifications.yaml
:code: yaml
:start-after: # notify compare block
:end-before: # notify compare custom block
.. include:: examples/test-jobs/notifications.yaml
.. include:: examples/source/notifications.yaml
:code: yaml
:start-after: # notify compare custom block
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment