2014-01-28 16:59:59 +01:00
|
|
|
import logging
|
|
|
|
from zerver.middleware import async_request_restart
|
2016-04-07 21:51:29 +02:00
|
|
|
from typing import Any
|
2014-01-28 16:59:59 +01:00
|
|
|
|
2014-01-27 23:53:13 +01:00
|
|
|
current_handler_id = 0
|
2016-01-25 23:42:16 +01:00
|
|
|
handlers = {} # type: Dict[int, Any] # TODO: Should be AsyncDjangoHandler but we don't important runtornado.py.
|
2014-01-27 23:53:13 +01:00
|
|
|
|
|
|
|
def get_handler_by_id(handler_id):
|
2016-04-04 10:48:52 +02:00
|
|
|
# type: (int) -> Any # TODO: should be AsyncDjangoHandler, see above
|
2014-01-27 23:53:13 +01:00
|
|
|
return handlers[handler_id]
|
|
|
|
|
|
|
|
def allocate_handler_id(handler):
|
2016-04-04 10:48:52 +02:00
|
|
|
# type: (Any) -> int # TODO: should be AsyncDjangoHandler, see above
|
2014-01-27 23:53:13 +01:00
|
|
|
global current_handler_id
|
|
|
|
handlers[current_handler_id] = handler
|
2014-01-28 17:10:10 +01:00
|
|
|
handler.handler_id = current_handler_id
|
2014-01-27 23:53:13 +01:00
|
|
|
current_handler_id += 1
|
2014-01-28 17:10:10 +01:00
|
|
|
return handler.handler_id
|
2014-01-28 16:59:59 +01:00
|
|
|
|
2016-02-13 06:34:14 +01:00
|
|
|
def clear_handler_by_id(handler_id):
|
2016-04-04 10:48:52 +02:00
|
|
|
# type: (int) -> None
|
2016-02-13 06:34:14 +01:00
|
|
|
del handlers[handler_id]
|
|
|
|
|
2016-03-21 00:31:18 +01:00
|
|
|
def handler_stats_string():
|
2016-04-04 10:48:52 +02:00
|
|
|
# type: () -> str
|
2016-03-21 00:31:18 +01:00
|
|
|
return "%s handlers, latest ID %s" % (len(handlers), current_handler_id)
|
|
|
|
|
2014-01-28 16:59:59 +01:00
|
|
|
def finish_handler(handler_id, event_queue_id, contents, apply_markdown):
|
2016-07-03 18:23:17 +02:00
|
|
|
# type: (int, str, List[Dict[str, Any]], bool) -> None
|
2014-01-28 16:59:59 +01:00
|
|
|
err_msg = "Got error finishing handler for queue %s" % (event_queue_id,)
|
|
|
|
try:
|
|
|
|
# We call async_request_restart here in case we are
|
|
|
|
# being finished without any events (because another
|
|
|
|
# get_events request has supplanted this request)
|
|
|
|
handler = get_handler_by_id(handler_id)
|
|
|
|
request = handler._request
|
|
|
|
async_request_restart(request)
|
2016-10-19 22:14:52 +02:00
|
|
|
if len(contents) != 1:
|
|
|
|
request._log_data['extra'] = "[%s/1]" % (event_queue_id,)
|
|
|
|
else:
|
|
|
|
request._log_data['extra'] = "[%s/1/%s]" % (event_queue_id, contents[0]["type"])
|
|
|
|
|
2014-01-28 16:59:59 +01:00
|
|
|
handler.zulip_finish(dict(result='success', msg='',
|
|
|
|
events=contents,
|
|
|
|
queue_id=event_queue_id),
|
|
|
|
request, apply_markdown=apply_markdown)
|
|
|
|
except IOError as e:
|
2016-07-13 17:09:49 +02:00
|
|
|
if str(e) != 'Stream is closed':
|
2014-01-28 16:59:59 +01:00
|
|
|
logging.exception(err_msg)
|
|
|
|
except AssertionError as e:
|
2016-07-13 17:09:49 +02:00
|
|
|
if str(e) != 'Request closed':
|
2014-01-28 16:59:59 +01:00
|
|
|
logging.exception(err_msg)
|
|
|
|
except Exception:
|
|
|
|
logging.exception(err_msg)
|