| |
@@ -0,0 +1,134 @@
|
| |
+ # encoding=utf8
|
| |
+ '''
|
| |
+ This file is for development purposes only.
|
| |
+
|
| |
+ The end goal is for the streaming server to be implemented on the FMN side
|
| |
+ '''
|
| |
+ import json
|
| |
+ import logging
|
| |
+ import sys
|
| |
+ import trollius
|
| |
+ import trollius_redis
|
| |
+ import urllib2
|
| |
+ from concurrent.futures import TimeoutError
|
| |
+ from trollius import From
|
| |
+
|
| |
+ reload(sys)
|
| |
+ sys.setdefaultencoding('utf8')
|
| |
+
|
| |
+ log = logging.getLogger(__name__)
|
| |
+ SERVER = None
|
| |
+ REDIS_HOST = '0.0.0.0'
|
| |
+ REDIS_PORT = 6379
|
| |
+ REDIS_DB = 0
|
| |
+ EVENTSOURCE_PORT = 9090
|
| |
+ '''
|
| |
+ you need to
|
| |
+
|
| |
+ pip install trollius trollius_redis
|
| |
+
|
| |
+ dnf install redis httpie
|
| |
+ systemctl start redis
|
| |
+
|
| |
+
|
| |
+ usage: http get 0.0.0.0:9090/
|
| |
+
|
| |
+ '''
|
| |
+
|
| |
+
|
| |
+ def get_recent_posts():
|
| |
+ delta = "delta=86400" # one day worth of data
|
| |
+ rows_per_page = "rows_per_page=50"
|
| |
+ url = "https://apps.fedoraproject.org/datagrepper/raw" + "?" + delta + "&" + rows_per_page
|
| |
+ request = urllib2.Request(url)
|
| |
+ contents = urllib2.urlopen(request).read()
|
| |
+ json_response = json.loads(contents)
|
| |
+ return json_response['raw_messages']
|
| |
+
|
| |
+
|
| |
+ @trollius.coroutine
|
| |
+ def handle_client(client_reader, client_writer):
|
| |
+ origin = '*'
|
| |
+ if origin.endswith('/'):
|
| |
+ origin = origin[:-1]
|
| |
+
|
| |
+ client_writer.write(("HTTP/1.0 200 OK\n"
|
| |
+ "Content-Type: text/event-stream\n"
|
| |
+ "Cache: nocache\n"
|
| |
+ "Connection: keep-alive\n"
|
| |
+ "Access-Control-Allow-Origin: %s\n\n" % origin
|
| |
+ ).encode())
|
| |
+
|
| |
+ connection = yield trollius.From(trollius_redis.Connection.create(
|
| |
+ host=REDIS_HOST, port=REDIS_PORT,
|
| |
+ db=REDIS_DB))
|
| |
+
|
| |
+ try:
|
| |
+ posts = get_recent_posts()
|
| |
+
|
| |
+ # send 50 latest posts
|
| |
+ num_post_sent = 0
|
| |
+ while num_post_sent < 50:
|
| |
+ reply = posts[num_post_sent]
|
| |
+ reply = json.dumps(reply)
|
| |
+ log.info(reply)
|
| |
+ log.info("Sending post %s %s", str(num_post_sent), reply)
|
| |
+ client_writer.write(('data: %s\n\n' % reply).encode())
|
| |
+ yield trollius.From(client_writer.drain())
|
| |
+ num_post_sent += 1
|
| |
+ yield From(trollius.sleep(2))
|
| |
+
|
| |
+ except trollius.ConnectionResetError:
|
| |
+ log.exception("ERROR: ConnectionResetError in handle_client")
|
| |
+ except Exception:
|
| |
+ log.exception("ERROR: Exception in handle_client")
|
| |
+ finally:
|
| |
+ # Wathever happens, close the connection.
|
| |
+ connection.close()
|
| |
+ client_writer.close()
|
| |
+
|
| |
+
|
| |
+ def main():
|
| |
+ global SERVER
|
| |
+
|
| |
+ try:
|
| |
+ loop = trollius.get_event_loop()
|
| |
+ coro = trollius.start_server(handle_client,
|
| |
+ host=None,
|
| |
+ port=EVENTSOURCE_PORT,
|
| |
+ loop=loop)
|
| |
+ SERVER = loop.run_until_complete(coro)
|
| |
+ log.info('Serving server at {}'.format(SERVER.sockets[0].getsockname(
|
| |
+ )))
|
| |
+ loop.run_forever()
|
| |
+ except KeyboardInterrupt:
|
| |
+ pass
|
| |
+ except trollius.ConnectionResetError as err:
|
| |
+ log.exception("ERROR: ConnectionResetError in main")
|
| |
+ except Exception as err:
|
| |
+ log.exception("ERROR: Exception in main")
|
| |
+ finally:
|
| |
+ # Close the server
|
| |
+ SERVER.close()
|
| |
+ log.info("End Connection")
|
| |
+ loop.run_until_complete(SERVER.wait_closed())
|
| |
+ loop.close()
|
| |
+ log.info("End")
|
| |
+
|
| |
+
|
| |
+ if __name__ == '__main__':
|
| |
+ log = logging.getLogger("")
|
| |
+ formatter = logging.Formatter(
|
| |
+ "%(asctime)s %(levelname)s [%(module)s:%(lineno)d] %(message)s")
|
| |
+
|
| |
+ # setup console logging
|
| |
+ log.setLevel(logging.DEBUG)
|
| |
+ ch = logging.StreamHandler()
|
| |
+ ch.setLevel(logging.DEBUG)
|
| |
+
|
| |
+ aslog = logging.getLogger("asyncio")
|
| |
+ aslog.setLevel(logging.DEBUG)
|
| |
+
|
| |
+ ch.setFormatter(formatter)
|
| |
+ log.addHandler(ch)
|
| |
+ main()
|
| |
:thumbsup: