2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2013-01-11 21:16:42 +01:00
|
|
|
from optparse import make_option
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
import simplejson
|
|
|
|
import pika
|
2013-03-06 21:04:53 +01:00
|
|
|
from zephyr.lib.actions import process_user_activity_event, \
|
|
|
|
process_user_presence_event, process_update_message_flags
|
2013-01-18 19:01:20 +01:00
|
|
|
from zephyr.lib.queue import SimpleQueueClient
|
2013-02-15 17:03:28 +01:00
|
|
|
import sys
|
|
|
|
import signal
|
2013-01-11 21:16:42 +01:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
option_list = BaseCommand.option_list
|
2013-02-15 17:41:33 +01:00
|
|
|
help = "Process UserActivity log messages."
|
2013-01-11 21:16:42 +01:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2013-02-11 21:47:45 +01:00
|
|
|
activity_queue = SimpleQueueClient()
|
2013-01-11 21:16:42 +01:00
|
|
|
|
2013-02-11 21:47:45 +01:00
|
|
|
def callback_activity(ch, method, properties, event):
|
|
|
|
print " [x] Received activity %r" % (event,)
|
2013-02-15 17:41:33 +01:00
|
|
|
msg_type = event['type']
|
|
|
|
if msg_type == 'user_activity':
|
|
|
|
process_user_activity_event(event)
|
|
|
|
elif msg_type == 'user_presence':
|
|
|
|
process_user_presence_event(event)
|
2013-03-06 21:04:53 +01:00
|
|
|
elif msg_type == 'update_message':
|
|
|
|
process_update_message_flags(event)
|
2013-02-15 17:41:33 +01:00
|
|
|
else:
|
|
|
|
print("[*] Unknown message type: %s" (msg_type,))
|
2013-02-11 21:47:45 +01:00
|
|
|
|
2013-02-15 17:03:28 +01:00
|
|
|
def signal_handler(signal, frame):
|
|
|
|
print("[*] Closing and disconnecting from queues")
|
|
|
|
activity_queue.stop_consuming()
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
signal.signal(signal.SIGTERM, signal_handler)
|
|
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
|
2013-01-11 21:16:42 +01:00
|
|
|
print ' [*] Waiting for messages. To exit press CTRL+C'
|
2013-02-11 21:47:45 +01:00
|
|
|
activity_queue.register_json_consumer('user_activity', callback_activity)
|
2013-01-11 21:16:42 +01:00
|
|
|
activity_queue.start_consuming()
|