embedded bots: Add functional state handler.

This replaces the former non-functional StateHandler
stub with a dictionary-like state object. Accessing it will
will read and store strings in the BotUserStateData model.

Each bot has a limited state size. To enforce this limit while
keeping data updates efficient, StateHandler caches the expensive
query for getting a bot's total state size. Assignments to a key
then only need to fetch that entry's previous size, if any, and
compare it to the new entry's size.
This commit is contained in:
derAnfaenger
2017-10-12 16:34:05 +02:00
committed by Tim Abbott
parent a2b7929b22
commit c8a2702b9a
2 changed files with 35 additions and 9 deletions

View File

@@ -9,7 +9,6 @@ from functools import wraps
import smtplib
import socket
from zulip_bots.lib import ExternalBotHandler, StateHandler
from django.conf import settings
from django.db import connection
from django.core.handlers.wsgi import WSGIRequest
@@ -506,11 +505,6 @@ class EmbeddedBotWorker(QueueProcessingWorker):
# type: (UserProfile) -> EmbeddedBotHandler
return EmbeddedBotHandler(user_profile)
# TODO: Handle stateful bots properly
def get_state_handler(self):
# type: () -> StateHandler
return StateHandler()
def consume(self, event):
# type: (Mapping[str, Any]) -> None
user_profile_id = event['user_profile_id']
@@ -528,4 +522,4 @@ class EmbeddedBotWorker(QueueProcessingWorker):
bot_handler.handle_message(
message=message,
bot_handler=self.get_bot_api_client(user_profile),
state_handler=self.get_state_handler())
state_handler=None)