mirror of https://github.com/zulip/zulip.git
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:
parent
6638c12aef
commit
a19a69bfe3
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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,19 +529,22 @@ class EmbeddedBotWorker(QueueProcessingWorker):
|
|||
logging.error("Error: User %s has bot with invalid embedded bot service %s" % (
|
||||
user_profile_id, service.name))
|
||||
continue
|
||||
if hasattr(bot_handler, 'initialize'):
|
||||
bot_handler.initialize(self.get_bot_api_client(user_profile))
|
||||
if event['trigger'] == 'mention':
|
||||
message['content'] = extract_query_without_mention(
|
||||
try:
|
||||
if hasattr(bot_handler, 'initialize'):
|
||||
bot_handler.initialize(self.get_bot_api_client(user_profile))
|
||||
if event['trigger'] == 'mention':
|
||||
message['content'] = extract_query_without_mention(
|
||||
message=message,
|
||||
client=self.get_bot_api_client(user_profile),
|
||||
)
|
||||
if message['content'] is None:
|
||||
return
|
||||
bot_handler.handle_message(
|
||||
message=message,
|
||||
client=self.get_bot_api_client(user_profile),
|
||||
bot_handler=self.get_bot_api_client(user_profile)
|
||||
)
|
||||
if message['content'] is None:
|
||||
return
|
||||
bot_handler.handle_message(
|
||||
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):
|
||||
|
|
Loading…
Reference in New Issue