embedded bots: Log warning when bot quit()s.

External bots may call bot_handler.quit() when
they wish to terminate, e.g. due to a misconfiguration.
Currently, embedded bots ignore calls to quit(), even
though they signal a problem. This commit does the first
step in handling quit() calls by logging a warning.
This commit is contained in:
Robert Hönig 2018-02-08 15:51:38 +01:00 committed by Tim Abbott
parent 6638c12aef
commit a19a69bfe3
3 changed files with 32 additions and 12 deletions

View File

@ -57,6 +57,9 @@ class StateHandler:
def contains(self, key: Text) -> bool:
return is_key_in_bot_storage(self.user_profile, key)
class EmbeddedBotQuitException(Exception):
pass
class EmbeddedBotHandler:
def __init__(self, user_profile: UserProfile) -> None:
# Only expose a subset of our UserProfile's functionality
@ -113,3 +116,6 @@ class EmbeddedBotHandler:
if optional:
return dict()
raise
def quit(self, message: str= "") -> None:
raise EmbeddedBotQuitException(message)

View File

@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-
from unittest import mock
from mock import patch
from typing import Any, Dict, Tuple, Text, Optional
from zerver.lib.bot_lib import EmbeddedBotQuitException
from zerver.lib.test_classes import ZulipTestCase
from zerver.models import UserProfile, Recipient, get_display_recipient
@ -47,6 +49,15 @@ class TestEmbeddedBotMessaging(ZulipTestCase):
last_message = self.get_last_message()
self.assertEqual(last_message.content, "foo")
def test_embedded_bot_quit_exception(self) -> None:
with patch('zulip_bots.bots.helloworld.helloworld.HelloWorldHandler.handle_message',
side_effect=EmbeddedBotQuitException("I'm quitting!")):
with patch('logging.warning') as mock_logging:
self.send_stream_message(self.user_profile.email, "Denmark",
content="@**{}** foo".format(self.bot_profile.full_name),
topic_name="bar")
mock_logging.assert_called_once_with("I'm quitting!")
class TestEmbeddedBotFailures(ZulipTestCase):
@mock.patch("logging.error")
def test_invalid_embedded_bot_service(self, logging_error_mock: mock.Mock) -> None:

View File

@ -43,7 +43,7 @@ from zerver.lib.outgoing_webhook import do_rest_call, get_outgoing_webhook_servi
from zerver.models import get_bot_services
from zulip import Client
from zulip_bots.lib import extract_query_without_mention
from zerver.lib.bot_lib import EmbeddedBotHandler, get_bot_handler
from zerver.lib.bot_lib import EmbeddedBotHandler, get_bot_handler, EmbeddedBotQuitException
import os
import sys
@ -529,6 +529,7 @@ class EmbeddedBotWorker(QueueProcessingWorker):
logging.error("Error: User %s has bot with invalid embedded bot service %s" % (
user_profile_id, service.name))
continue
try:
if hasattr(bot_handler, 'initialize'):
bot_handler.initialize(self.get_bot_api_client(user_profile))
if event['trigger'] == 'mention':
@ -542,6 +543,8 @@ class EmbeddedBotWorker(QueueProcessingWorker):
message=message,
bot_handler=self.get_bot_api_client(user_profile)
)
except EmbeddedBotQuitException as e:
logging.warning(str(e))
@assign_queue('deferred_work')
class DeferredWorker(QueueProcessingWorker):