2016-06-03 14:54:46 +01:00
|
|
|
from flask import current_app
|
|
|
|
|
|
2016-09-20 17:24:28 +01:00
|
|
|
from app import notify_celery
|
|
|
|
|
from app.dao.notifications_dao import update_notification_status_by_id
|
2016-08-05 10:44:43 +01:00
|
|
|
from app.statsd_decorators import statsd
|
2016-06-29 11:50:54 +01:00
|
|
|
|
2016-09-20 17:24:28 +01:00
|
|
|
from app.delivery import send_to_providers
|
|
|
|
|
|
2016-06-13 16:40:46 +01:00
|
|
|
|
2016-06-17 16:32:56 +01:00
|
|
|
def retry_iteration_to_delay(retry=0):
|
2016-06-13 16:40:46 +01:00
|
|
|
"""
|
2016-09-20 17:24:28 +01:00
|
|
|
:param retry times we have performed a retry
|
2016-06-13 16:40:46 +01:00
|
|
|
Given current retry calculate some delay before retrying
|
2016-06-17 16:32:56 +01:00
|
|
|
0: 10 seconds
|
|
|
|
|
1: 60 seconds (1 minutes)
|
|
|
|
|
2: 300 seconds (5 minutes)
|
|
|
|
|
3: 3600 seconds (60 minutes)
|
|
|
|
|
4: 14400 seconds (4 hours)
|
2016-06-13 16:40:46 +01:00
|
|
|
:param retry (zero indexed):
|
2016-06-17 16:32:56 +01:00
|
|
|
:return length to retry in seconds, default 10 seconds
|
2016-06-13 16:40:46 +01:00
|
|
|
"""
|
2016-06-17 16:32:56 +01:00
|
|
|
|
|
|
|
|
delays = {
|
|
|
|
|
0: 10,
|
|
|
|
|
1: 60,
|
|
|
|
|
2: 300,
|
|
|
|
|
3: 3600,
|
|
|
|
|
4: 14400
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return delays.get(retry, 10)
|
2016-06-03 14:54:46 +01:00
|
|
|
|
2016-06-07 12:53:31 +01:00
|
|
|
|
2016-06-03 14:54:46 +01:00
|
|
|
@notify_celery.task(bind=True, name="send-sms-to-provider", max_retries=5, default_retry_delay=5)
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2016-07-01 14:14:28 +01:00
|
|
|
def send_sms_to_provider(self, service_id, notification_id):
|
2016-09-20 17:24:28 +01:00
|
|
|
try:
|
|
|
|
|
send_to_providers.send_sms_to_provider(notification_id)
|
|
|
|
|
except Exception as e:
|
2016-06-03 14:54:46 +01:00
|
|
|
try:
|
2016-09-20 17:24:28 +01:00
|
|
|
current_app.logger.error(
|
|
|
|
|
"RETRY: SMS notification {} failed".format(notification_id)
|
|
|
|
|
)
|
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
|
self.retry(queue="retry", countdown=retry_iteration_to_delay(self.request.retries))
|
|
|
|
|
except self.MaxRetriesExceededError:
|
|
|
|
|
current_app.logger.error(
|
|
|
|
|
"RETRY FAILED: task send_sms_to_provider failed for notification {}".format(notification_id),
|
|
|
|
|
e
|
|
|
|
|
)
|
|
|
|
|
update_notification_status_by_id(notification_id, 'technical-failure')
|
2016-07-01 14:14:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(bind=True, name="send-email-to-provider", max_retries=5, default_retry_delay=5)
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2016-07-04 17:29:41 +01:00
|
|
|
def send_email_to_provider(self, service_id, notification_id):
|
2016-09-20 17:24:28 +01:00
|
|
|
try:
|
|
|
|
|
send_to_providers.send_email_response(notification_id)
|
|
|
|
|
except Exception as e:
|
2016-07-01 14:14:28 +01:00
|
|
|
try:
|
2016-09-20 17:24:28 +01:00
|
|
|
current_app.logger.error(
|
|
|
|
|
"RETRY: Email notification {} failed".format(notification_id)
|
2016-07-05 16:21:31 +01:00
|
|
|
)
|
2016-09-20 17:24:28 +01:00
|
|
|
current_app.logger.exception(e)
|
|
|
|
|
self.retry(queue="retry", countdown=retry_iteration_to_delay(self.request.retries))
|
|
|
|
|
except self.MaxRetriesExceededError:
|
|
|
|
|
current_app.logger.error(
|
|
|
|
|
"RETRY FAILED: task send_email_to_provider failed for notification {}".format(notification_id),
|
|
|
|
|
e
|
2016-07-01 14:14:28 +01:00
|
|
|
)
|
2016-09-20 17:24:28 +01:00
|
|
|
update_notification_status_by_id(notification_id, 'technical-failure')
|