Commit 09d8c48b authored by Leonidas Poulopoulos's avatar Leonidas Poulopoulos

Resolve an old issue where the platform would start on 2nd refresh

parent adc07310
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
from gevent import monkey from gevent import monkey
monkey.patch_all() monkey.patch_all()
from gevent.pool import Pool from gevent.pool import Pool
import gevent
import json import json
import uuid import uuid
...@@ -31,17 +32,20 @@ from django.conf import settings ...@@ -31,17 +32,20 @@ from django.conf import settings
#from django.views.decorators.csrf import csrf_exempt #from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.conf import settings
import beanstalkc import beanstalkc
import logging import logging
import os
FORMAT = '%(asctime)s %(levelname)s: %(message)s' LOG_FILENAME = os.path.join(settings.LOG_FILE_LOCATION, 'poller.log')
logging.basicConfig(format=FORMAT) formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s')
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(LOG_FILENAME)
handler.setFormatter(formatter)
logger.addHandler(handler)
def create_message(message, user, time): def create_message(message, user, time):
data = {'id': str(uuid.uuid4()), 'body': message, 'user':user, 'time':time} data = {'id': str(uuid.uuid4()), 'body': message, 'user':user, 'time':time}
...@@ -56,6 +60,12 @@ def json_response(value, **kwargs): ...@@ -56,6 +60,12 @@ def json_response(value, **kwargs):
class Msgs(object): class Msgs(object):
cache_size = 500 cache_size = 500
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(Msgs, cls).__new__(cls, *args, **kwargs)
return cls._instance
def __init__(self): def __init__(self):
logger.info("initializing") logger.info("initializing")
self.user = None self.user = None
...@@ -73,7 +83,7 @@ class Msgs(object): ...@@ -73,7 +83,7 @@ class Msgs(object):
def message_existing(self, request): def message_existing(self, request):
if request.is_ajax(): if request.is_ajax():
try: try:
user = request.user.get_profile().peer.domain_name user = request.user.get_profile().peer.peer_tag
except: except:
user = None user = None
return False return False
...@@ -94,6 +104,7 @@ class Msgs(object): ...@@ -94,6 +104,7 @@ class Msgs(object):
if mesg: if mesg:
message = mesg['message'] message = mesg['message']
user = mesg['username'] user = mesg['username']
logger.info("from %s" %user)
now = datetime.datetime.now() now = datetime.datetime.now()
msg = create_message(message, user, now.strftime("%Y-%m-%d %H:%M:%S")) msg = create_message(message, user, now.strftime("%Y-%m-%d %H:%M:%S"))
try: try:
...@@ -119,7 +130,7 @@ class Msgs(object): ...@@ -119,7 +130,7 @@ class Msgs(object):
if request.is_ajax(): if request.is_ajax():
cursor = {} cursor = {}
try: try:
user = request.user.get_profile().peer.domain_name user = request.user.get_profile().peer.peer_tag
except: except:
user = None user = None
return False return False
...@@ -145,29 +156,28 @@ class Msgs(object): ...@@ -145,29 +156,28 @@ class Msgs(object):
self.user_cursor[user] = self.user_cache[user][-1]['id'] self.user_cursor[user] = self.user_cache[user][-1]['id']
return HttpResponseRedirect(reverse('group-routes')) return HttpResponseRedirect(reverse('group-routes'))
def monitor_polls(self, polls=None): def monitor_polls(self):
b = beanstalkc.Connection() b = beanstalkc.Connection()
b.watch(settings.POLLS_TUBE) b.watch(settings.POLLS_TUBE)
while True: while True:
job = b.reserve() job = b.reserve()
msg = json.loads(job.body) msg = json.loads(job.body)
job.bury() job.bury()
logger.info("Got New message")
self.message_new(msg) self.message_new(msg)
def start_polling(self): def start_polling(self):
logger.info("Start Polling") logger.info("Start Polling")
p = Pool(10) gevent.spawn(self.monitor_polls)
while True:
p.spawn(self.monitor_polls)
msgs = Msgs() msgs = Msgs()
main = msgs.main main = msgs.main
message_new = msgs.message_new
message_updates = msgs.message_updates message_updates = msgs.message_updates
message_existing = msgs.message_existing message_existing = msgs.message_existing
poll = msgs.start_polling poll = msgs.start_polling
poll() poll()
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