def _process_notification(self, users, scheduler): for username in users: logger.info(f"Sending due deletion {scheduler} to user", username=obfuscate_email(username)) NotifyService().request_to_notify(template_name=scheduler, email=username) logger.info(f"Due deletion {scheduler} sent to user", username=obfuscate_email(username)) self._update_user(username, scheduler)
def _get_database(): try: engine = _get_engine() logger.info("Connected to database!") except IOError: logger.exception("Failed to get database connection!") raise IOError return engine
def _update_user(self, user, scheduler_column): logger.info('Updating user data with notification sent date', notification=scheduler_column) form_data = {scheduler_column: datetime.utcnow()} try: requests.patch(f'{self.patch_url}/{user}', data=form_data, headers=self.headers) except requests.exceptions.HTTPError as error: logger.exception("Unable to update user notification date", username=obfuscate_email(user), error=error) raise error logger.info('user data with notification sent date updated', notification=scheduler_column)
def _send_message(self, email, template_id, personalisation): """ Send message to gov.uk notify via pubsub topic :param email: str email address of recipient :param template_id: the template id on gov.uk notify to be used """ if not cfg.Config.SEND_EMAIL_TO_GOV_NOTIFY: logger.info("Notification not sent. Notify is disabled.") return notification = { 'notify': { 'email_address': email, 'template_id': template_id, 'personalisation': {} } } if personalisation: notification['notify']['personalisation'] = personalisation notification_str = json.dumps(notification) if self.publisher is None: self.publisher = pubsub_v1.PublisherClient() topic_path = self.publisher.topic_path(self.project_id, self.topic_id) logger.info('Publishing notification message to pub-sub topic', pubsub_topic=self.topic_id) future = self.publisher.publish(topic_path, data=notification_str.encode()) # It's okay for us to catch a broad Exception here because the documentation for future.result() says it # throws either a TimeoutError or an Exception. try: msg_id = future.result() logger.info('Notification message published to pub-sub.', msg_id=msg_id, pubsub_topic=self.topic_id) except TimeoutError as e: logger.exception(e) raise NotifyError('There was a problem sending a notification via pub-sub topic to GOV.UK Notify.' 'Communication to pub-sub topic timed-out', pubsub_topic=self.topic_id, error=e) except Exception as e: logger.exception(e) raise NotifyError('There was a problem sending a notification via pub-sub topic to GOV.UK Notify. ' 'Communication to pub-sub topic raised an exception.', pubsub_topic=self.topic_id, error=e)
def process_third_notification(self): users = self._get_eligible_users(self.third_notification_url) if users: self._process_notification(users, 'third_notification') else: logger.info("No user eligible for third notification")
def process_second_notification(self): users = self._get_eligible_users(self.second_notification_url) if users: self._process_notification(users, 'second_notification') else: logger.info("No user eligible for second notification")
def process_first_notification(self): users = self._get_eligible_users(self.first_notification_url) if users: self._process_notification(users, 'first_notification') else: logger.info("No user eligible for first notification")
def request_to_notify(self, template_name, email): logger.info("Request to notify ", template_name=template_name) template_id = self._get_template_id(template_name) first_name = self._get_user_first_name(email) self._send_message(email, template_id, {'FIRST_NAME': first_name})
from ras_rm_auth_scheduler_service.logger import logger from ras_rm_auth_scheduler_service.process_due_deletion_notification_job import ( ProcessNotificationJob, ) if __name__ == "__main__": logger.info("Scheduler processing Accounts not accessed in the last 35 months ") ProcessNotificationJob().process_third_notification() logger.info("Scheduler finished processing Accounts not accessed in last 35 months") logger.info("Scheduler processing Accounts not accessed in the last 30 months ") ProcessNotificationJob().process_second_notification() logger.info("Scheduler finished processing Accounts not accessed in last 30 months") logger.info("Scheduler processing Accounts not accessed in the last 24 months ") ProcessNotificationJob().process_first_notification() logger.info("Scheduler finished processing Accounts not accessed in last 24 months")