Exemplo n.º 1
0
def _record_audit(update_results, producer_task, start_time):
    """
    Record an audit log for the get_company_updates task which expresses the number
    of companies successfully updates, failures, ids of companies updated, celery
    task info and start/end times.
    """
    audit = {
        'success_count': 0,
        'failure_count': 0,
        'updated_company_ids': [],
        'producer_task_id': producer_task.request.id,
        'start_time': start_time.isoformat(),
        'end_time': now().isoformat(),
    }
    for result in update_results:
        if result.successful():
            audit['success_count'] += 1
            audit['updated_company_ids'].append(result.result)
        else:
            audit['failure_count'] += 1
    log_to_sentry('get_company_updates task completed.', extra=audit)
    success_count, failure_count = audit['success_count'], audit[
        'failure_count']
    realtime_message = (f'{producer_task.name} updated: {success_count}; '
                        f'failed to update: {failure_count}')
    send_realtime_message(realtime_message)
Exemplo n.º 2
0
 def test_message_method_called(self, monkeypatch):
     """
     Test that the Slack client 'send' method is called appropriately.
     """
     mock_web_client = mock.Mock()
     monkeypatch.setattr(
         'datahub.core.realtime_messaging.WebClient',
         mock_web_client,
     )
     send_realtime_message('Hello!')
     mock_web_client().chat_postMessage.assert_called_once_with(
         channel='MESSAGE_CHANNEL',
         text='Hello!',
     )
Exemplo n.º 3
0
 def test_slack_client_initiated(self, monkeypatch):
     """
     Test that the Slack client gets passed the token and timeout from the config.
     """
     mock_web_client = mock.Mock()
     monkeypatch.setattr(
         'datahub.core.realtime_messaging.WebClient',
         mock_web_client,
     )
     send_realtime_message('some message')
     mock_web_client.assert_called_once_with(
         timeout=11,
         token='API_TOKEN',
     )
Exemplo n.º 4
0
 def test_absent_message_channel_aborts_message(self, caplog, monkeypatch):
     """
     Test that an absence of a message channel aborts the send process gracefully.
     """
     caplog.set_level('INFO')
     mock_web_client = mock.Mock()
     monkeypatch.setattr(
         'datahub.core.realtime_messaging.WebClient',
         mock_web_client,
     )
     send_realtime_message('some message')
     expected_message = 'Setup was incomplete. Message not attempted.'
     assert expected_message in caplog.text
     mock_web_client.assert_not_called()
Exemplo n.º 5
0
 def test_disabled_facility_aborts_message(self, caplog, monkeypatch):
     """
     Test that an disabling the facility setting aborts the send process gracefully.
     """
     caplog.set_level('INFO')
     mock_web_client = mock.Mock()
     monkeypatch.setattr(
         'datahub.core.realtime_messaging.WebClient',
         mock_web_client,
     )
     send_realtime_message('some message')
     expected_message = 'Setup was incomplete. Message not attempted.'
     assert expected_message in caplog.text
     mock_web_client().assert_not_called()
Exemplo n.º 6
0
def automatic_contact_archive(self, limit=1000, simulate=False):
    """
    Archive inactive contacts.
    """
    with advisory_lock('automatic_contact_archive', wait=False) as acquired:

        if not acquired:
            logger.info('Another instance of this task is already running.')
            return

        archive_count = _automatic_contact_archive(limit=limit,
                                                   simulate=simulate)
        realtime_message = f'{self.name} archived: {archive_count}'
        send_realtime_message(realtime_message)
Exemplo n.º 7
0
def automatic_company_archive(self, limit=1000, simulate=True):
    """
    Archive inactive companies.
    """
    if not is_feature_flag_active(AUTOMATIC_COMPANY_ARCHIVE_FEATURE_FLAG):
        logger.info(
            f'Feature flag "{AUTOMATIC_COMPANY_ARCHIVE_FEATURE_FLAG}" is not active, exiting.',
        )
        return

    with advisory_lock('automatic_company_archive', wait=False) as acquired:

        if not acquired:
            logger.info('Another instance of this task is already running.')
            return

        archive_count = _automatic_company_archive(limit, simulate)
        send_realtime_message(f'{self.name} archived: {archive_count}')
Exemplo n.º 8
0
 def test_slack_error_handled(
     self,
     caplog,
     monkeypatch,
 ):
     """
     Test that Slack client errors are caught and reported.
     """
     caplog.set_level('ERROR')
     mock_web_client = mock.Mock()
     monkeypatch.setattr(
         'datahub.core.realtime_messaging.WebClient',
         mock_web_client,
     )
     mock_web_client().chat_postMessage.side_effect = SlackClientError
     send_realtime_message('some message')
     expected_message = 'Slack post failed.'
     assert expected_message in caplog.text