2014-08-06 09:43:40 +00:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
"""
|
|
|
|
The ZMQ_Feed_Q Module
|
|
|
|
=====================
|
|
|
|
|
|
|
|
This module is consuming the Redis-list created by the ZMQ_Feed_Q Module,
|
|
|
|
And save the paste on disk to allow others modules to work on them.
|
|
|
|
|
|
|
|
..todo:: Be able to choose to delete or not the saved paste after processing.
|
|
|
|
..todo:: Store the empty paste (unprocessed) somewhere in Redis.
|
|
|
|
|
|
|
|
..note:: Module ZMQ_Something_Q and ZMQ_Something are closely bound, always put
|
|
|
|
the same Subscriber name in both of them.
|
|
|
|
|
|
|
|
Requirements
|
|
|
|
------------
|
|
|
|
|
|
|
|
*Need running Redis instances.
|
|
|
|
*Need the ZMQ_Feed_Q Module running to be able to work properly.
|
|
|
|
|
|
|
|
"""
|
2014-08-14 12:11:07 +00:00
|
|
|
import base64
|
|
|
|
import os
|
|
|
|
import time
|
2014-08-06 09:43:40 +00:00
|
|
|
from pubsublogger import publisher
|
|
|
|
|
2014-08-19 17:07:07 +00:00
|
|
|
import Helper
|
2014-08-06 09:43:40 +00:00
|
|
|
|
2014-08-14 12:11:07 +00:00
|
|
|
|
2014-08-19 17:07:07 +00:00
|
|
|
if __name__ == "__main__":
|
2014-08-22 15:35:40 +00:00
|
|
|
publisher.port = 6380
|
2014-08-19 17:07:07 +00:00
|
|
|
publisher.channel = "Script"
|
2014-08-06 09:43:40 +00:00
|
|
|
|
2014-08-19 17:07:07 +00:00
|
|
|
config_section = 'Feed'
|
|
|
|
config_channel = 'topicfilter'
|
|
|
|
subscriber_name = 'feed'
|
2014-08-06 09:43:40 +00:00
|
|
|
|
2014-08-19 17:07:07 +00:00
|
|
|
h = Helper.Redis_Queues(config_section, config_channel, subscriber_name)
|
2014-08-06 09:43:40 +00:00
|
|
|
|
2014-08-14 12:11:07 +00:00
|
|
|
# Publisher
|
2014-08-19 17:07:07 +00:00
|
|
|
pub_config_section = "PubSub_Global"
|
2014-08-20 13:14:57 +00:00
|
|
|
pub_config_channel = 'channel'
|
|
|
|
h.zmq_pub(pub_config_section, pub_config_channel)
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
# LOGGING #
|
|
|
|
publisher.info("Feed Script started to receive & publish.")
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
2014-08-20 13:14:57 +00:00
|
|
|
message = h.redis_rpop()
|
2014-08-14 12:11:07 +00:00
|
|
|
# Recovering the streamed message informations.
|
|
|
|
if message is not None:
|
2014-08-06 09:43:40 +00:00
|
|
|
if len(message.split()) == 3:
|
|
|
|
topic, paste, gzip64encoded = message.split()
|
|
|
|
print paste
|
|
|
|
else:
|
2014-08-14 12:11:07 +00:00
|
|
|
# TODO Store the name of the empty paste inside a Redis-list.
|
2014-08-06 09:43:40 +00:00
|
|
|
print "Empty Paste: not processed"
|
|
|
|
publisher.debug("Empty Paste: {0} not processed".format(paste))
|
|
|
|
continue
|
|
|
|
else:
|
2014-08-19 17:07:07 +00:00
|
|
|
if h.redis_queue_shutdown():
|
2014-08-06 09:43:40 +00:00
|
|
|
print "Shutdown Flag Up: Terminating"
|
|
|
|
publisher.warning("Shutdown Flag Up: Terminating.")
|
|
|
|
break
|
|
|
|
print "Empty Queues: Waiting..."
|
|
|
|
time.sleep(10)
|
|
|
|
continue
|
2014-08-14 12:11:07 +00:00
|
|
|
# Creating the full filepath
|
2014-08-22 15:35:40 +00:00
|
|
|
filename = os.path.join(os.environ['AIL_HOME'],
|
2014-08-19 17:07:07 +00:00
|
|
|
h.config.get("Directories", "pastes"), paste)
|
|
|
|
dirname = os.path.dirname(filename)
|
|
|
|
if not os.path.exists(dirname):
|
|
|
|
os.makedirs(dirname)
|
2014-08-06 09:43:40 +00:00
|
|
|
|
2014-08-19 17:07:07 +00:00
|
|
|
with open(filename, 'wb') as f:
|
|
|
|
f.write(base64.standard_b64decode(gzip64encoded))
|
2014-08-06 09:43:40 +00:00
|
|
|
|
2014-08-20 13:14:57 +00:00
|
|
|
h.zmq_pub_send(filename)
|