def check_alerts_for_query(self, query_id): from redash.wsgi import app logger.debug("Checking query %d for alerts", query_id) query = models.Query.get_by_id(query_id) for alert in query.alerts: alert.query = query new_state = alert.evaluate() if new_state != alert.state: logger.info("Alert %d new state: %s", alert.id, new_state) old_state = alert.state alert.update_instance(state=new_state) if old_state == models.Alert.UNKNOWN_STATE and new_state == models.Alert.OK_STATE: logger.debug( "Skipping notification (previous state was unknown and now it's ok)." ) continue # message = Message recipients = [s.email for s in alert.subscribers()] logger.debug("Notifying: %s", recipients) html = """ Check <a href="{host}/alerts/{alert_id}">alert</a> / check <a href="{host}/queries/{query_id}">query</a>. """.format(host=settings.HOST, alert_id=alert.id, query_id=query.id) with app.app_context(): message = Message(recipients=recipients, subject="[{1}] {0}".format( alert.name, new_state.upper()), html=html) mail.send(message)
def check_alerts_for_query(self, query_id): from redash.wsgi import app logger.debug("Checking query %d for alerts", query_id) query = models.Query.get_by_id(query_id) for alert in query.alerts: alert.query = query new_state = alert.evaluate() if new_state != alert.state: logger.info("Alert %d new state: %s", alert.id, new_state) old_state = alert.state alert.update_instance(state=new_state) if old_state == models.Alert.UNKNOWN_STATE and new_state == models.Alert.OK_STATE: logger.debug("Skipping notification (previous state was unknown and now it's ok).") continue # message = Message recipients = [s.email for s in alert.subscribers()] logger.debug("Notifying: %s", recipients) html = """ Check <a href="{host}/alerts/{alert_id}">alert</a> / check <a href="{host}/queries/{query_id}">query</a>. """.format(host=settings.HOST, alert_id=alert.id, query_id=query.id) with app.app_context(): message = Message(recipients=recipients, subject="[{1}] {0}".format(alert.name, new_state.upper()), html=html) mail.send(message)
def send_report(email, fn): """ Builds a message within the application's context, attaches all the XLSX reports references in fns and sends them to the specified email. :param email: Destination email address. :param fns: Filenames of reports to attach. """ # This is imported here, because it will be executed on the context # of the celery task. If imported at the beginning of the file, it # causes a circular dependency which results in the whole # application crashing, # PLEASE DO NOT FIX IT :) from redash.wsgi import app with app.app_context(): message = Message( recipients=[email], subject="Mansion Global Analytics requested reports", html='Hi,<br><br>Please find attached the requested reports.' ) # Attach all reports to message: with open(fn, 'r') as flo: message.attach( 'report.xlsx', 'application/zip', flo.read() ) # Send the e-mail mail.send(message)
def notify_mail(alert, html, new_state, app): recipients = [s.email for s in alert.subscribers()] logger.debug("Notifying: %s", recipients) try: with app.app_context(): message = Message( recipients=recipients, subject="[{1}] {0}".format(alert.name, new_state.upper()), html=html ) mail.send(message) except: logger.exception("mail send ERROR.")
def notify_mail(alert, html, new_state, app): recipients = [s.email for s in alert.subscribers()] logger.debug("Notifying: %s", recipients) try: with app.app_context(): message = Message(recipients=recipients, subject="[{1}] {0}".format(alert.name.encode('utf-8', 'ignore'), new_state.upper()), html=html) mail.send(message) except Exception: logger.exception("mail send ERROR.")
def send_mail(to, subject, html, text): from redash.wsgi import app try: with app.app_context(): message = Message(recipients=to, subject=subject, html=html, body=text) mail.send(message) except Exception: logger.exception('Failed sending message: %s', message.subject)
def init_celery_flask_app(**kwargs): app = create_app() app.app_context().push()
def __call__(self, *args, **kwargs): from redash.wsgi import app with app.app_context(): return TaskBase.__call__(self, *args, **kwargs)