def test_connection(self, sentinel_mock, endpoint_mock):
     persistence = Persistence()
     self.assertIsNotNone(persistence.client)
     sentinel_mock.assert_called_once_with([('fake-host-01', '26379'),
                                            ('fake-host-02', '26379'),
                                            ('fake-host-03', '26379')],
                                           socket_timeout=1)
     self.assertEqual(persistence.ttl_seconds, REDIS_KEY_TTL)
    def test_notified(self, endpoint_mock):
        persistence = Persistence()
        persistence.client = FakeCache()

        task_db = build_task_database()
        task_running = build_task_running()
        persistence.set_notified(task_running)

        self.assertFalse(persistence.was_notified(task_db))
        self.assertTrue(persistence.was_notified(task_running))
def persistence_check():
    from random import randint
    from src.persistence.persist import Persistence

    key, value = 'HEALTH_CHECK', randint(0, 100000)
    try:
        persistence = Persistence()
        persistence.client.set(key, value)
        assert int(persistence.client.get(key)) == value
        persistence.client.delete(key)
    except Exception as e:
        return False, e
    else:
        return True, 'OK'
Beispiel #4
0
def main():
    basicConfig(level=INFO)
    bot = Bot()
    dbaas = DBaaS()
    persistence = Persistence()

    info('Loading oldest tasks')
    tasks = dbaas.latest_tasks()
    for task in tasks:
        persistence.set_notified(task)
    info('Loaded')

    errors_count = 0
    while True:
        tasks = []
        try:
            tasks = dbaas.latest_tasks()
        except ConnectionError as e:
            errors_count += 1
            info('Error getting info from DBaaS - {}/{}'.format(
                errors_count, MAX_DBAAS_ATTEMPS))
            debug(e)

            if errors_count == MAX_DBAAS_ATTEMPS:
                bot.send_message('I can not get task history, '
                                 'problem in DBaaS API\n{}'.format(e))
        else:
            if errors_count > 0:
                info('DBaaS API is working, resetting error counter')
            errors_count = 0

        for task in tasks:
            if not task.is_error:
                continue

            if not persistence.was_notified(task):
                info('Notifying {}'.format(task.id))
                bot.send_message(task.as_message(), task.relevance)

            persistence.set_notified(task)

        sleep(15)
Beispiel #5
0
 def __init__(self):
     self.slack_client = SlackClient(SLACK_TOKEN, SLACK_PROXIES)
     self.name = '<@{}>'.format(SLACK_BOT_ID)
     self.rtm_reconnect_url = None
     self.persistence = Persistence()
Beispiel #6
0
 def __init__(self, channel, text):
     self.channel = channel
     self.text = text
     self.persistence = Persistence()
Beispiel #7
0
class Bot(object):
    def __init__(self):
        self.slack_client = SlackClient(SLACK_TOKEN, SLACK_PROXIES)
        self.name = '<@{}>'.format(SLACK_BOT_ID)
        self.rtm_reconnect_url = None
        self.persistence = Persistence()

    @property
    def my_channels(self):
        response = self.slack_client.api_call("channels.list")
        channels = []
        for channel in response['channels']:
            if channel['is_member']:
                channels.append(channel['id'])

        return channels

    def send_message_in_channel(self, message, channel):
        self.slack_client.api_call("chat.postMessage",
                                   channel=channel,
                                   text=message,
                                   as_user=True)

    def send_message(self, message, relevance):
        all_relevances = RELEVANCE_LIST[RELEVANCE_LIST.index(relevance):]
        for relevance in all_relevances:
            channels_list = self.persistence.channels_for(relevance)
            for channel in channels_list:
                self.send_message_in_channel(message, channel)

    def receive_command(self):
        try:
            commands = self.slack_client.rtm_read()
        except Exception as e:
            if not self.rtm_reconnect_url:
                return e

            info('Trying to reconnect in {}'.format(self.rtm_reconnect_url))
            self.slack_client.server.connect_slack_websocket(
                self.rtm_reconnect_url)
            self.rtm_reconnect_url = None
            return self.receive_command()

        debug(commands)

        self.get_reconnect_url(commands)

        return commands

    def get_reconnect_url(self, commands):
        for command in commands:
            if 'type' not in command:
                continue

            if command['type'] != 'reconnect_url':
                continue

            self.rtm_reconnect_url = command['url']

    def get_direct_messages(self):
        for command in self.receive_command():
            if not ('type' in command and 'text' in command):
                debug('Content invalid in {}'.format(command))
                continue

            if command['type'] != 'message':
                continue

            if self.name not in command['text']:
                continue

            if command.get('user', '') == SLACK_BOT_ID:
                continue

            text_cleaned = command['text'].replace(self.name, '')
            text_cleaned = text_cleaned.strip()

            yield BotMessage.build(command['channel'], text_cleaned)
Beispiel #8
0
 def test_connection(self, redis_from_url):
     persistence = Persistence()
     self.assertIsNotNone(persistence.client)
     redis_from_url.assert_called_once_with(REDIS_URL_CONNECTION)
     self.assertEqual(persistence.ttl_seconds, REDIS_KEY_TTL)