2016-06-20 13:33:53 +01:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
|
|
from flask import current_app
|
|
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
|
|
2016-09-07 15:36:59 +01:00
|
|
|
from app.aws import s3
|
2016-06-20 13:33:53 +01:00
|
|
|
from app import notify_celery
|
|
|
|
|
from app.dao.invited_user_dao import delete_invitations_created_more_than_two_days_ago
|
2016-09-07 15:36:59 +01:00
|
|
|
from app.dao.jobs_dao import dao_get_scheduled_jobs, dao_update_job, dao_get_jobs_older_than
|
2016-06-20 13:33:53 +01:00
|
|
|
from app.dao.notifications_dao import delete_notifications_created_more_than_a_week_ago, get_notifications, \
|
|
|
|
|
update_notification_status_by_id
|
|
|
|
|
from app.dao.users_dao import delete_codes_older_created_more_than_a_day_ago
|
2016-08-05 10:44:43 +01:00
|
|
|
from app.statsd_decorators import statsd
|
2016-08-24 17:03:56 +01:00
|
|
|
from app.models import JOB_STATUS_PENDING
|
|
|
|
|
from app.celery.tasks import process_job
|
|
|
|
|
|
|
|
|
|
|
2016-09-07 15:36:59 +01:00
|
|
|
@notify_celery.task(name="remove_csv_files")
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def remove_csv_files():
|
|
|
|
|
jobs = dao_get_jobs_older_than(7)
|
|
|
|
|
for job in jobs:
|
|
|
|
|
s3.remove_job_from_s3(job.service_id, job.id)
|
|
|
|
|
current_app.logger.info("Job ID {} has been removed from s3.".format(job.id))
|
|
|
|
|
|
|
|
|
|
|
2016-08-24 17:03:56 +01:00
|
|
|
@notify_celery.task(name="run-scheduled-jobs")
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def run_scheduled_jobs():
|
|
|
|
|
try:
|
|
|
|
|
jobs = dao_get_scheduled_jobs()
|
|
|
|
|
for job in jobs:
|
|
|
|
|
job.job_status = JOB_STATUS_PENDING
|
|
|
|
|
dao_update_job(job)
|
|
|
|
|
process_job.apply_async([str(job.id)], queue="process-job")
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Job ID {} added to process job queue".format(job.id)
|
|
|
|
|
)
|
|
|
|
|
except SQLAlchemyError as e:
|
|
|
|
|
current_app.logger.exception("Failed to run scheduled jobs", e)
|
|
|
|
|
raise
|
2016-06-20 13:33:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete-verify-codes")
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2016-06-20 13:33:53 +01:00
|
|
|
def delete_verify_codes():
|
|
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
|
|
|
|
deleted = delete_codes_older_created_more_than_a_day_ago()
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete job started {} finished {} deleted {} verify codes".format(start, datetime.utcnow(), deleted)
|
|
|
|
|
)
|
2016-08-24 17:03:56 +01:00
|
|
|
except SQLAlchemyError as e:
|
|
|
|
|
current_app.logger.exception("Failed to delete verify codes", e)
|
2016-06-20 13:33:53 +01:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete-successful-notifications")
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2016-06-20 13:33:53 +01:00
|
|
|
def delete_successful_notifications():
|
|
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
|
|
|
|
deleted = delete_notifications_created_more_than_a_week_ago('delivered')
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete job started {} finished {} deleted {} successful notifications".format(
|
|
|
|
|
start,
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
deleted
|
|
|
|
|
)
|
|
|
|
|
)
|
2016-08-24 17:03:56 +01:00
|
|
|
except SQLAlchemyError as e:
|
|
|
|
|
current_app.logger.exception("Failed to delete successful notifications", e)
|
2016-06-20 13:33:53 +01:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete-failed-notifications")
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2016-06-20 13:33:53 +01:00
|
|
|
def delete_failed_notifications():
|
|
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
|
|
|
|
deleted = delete_notifications_created_more_than_a_week_ago('failed')
|
|
|
|
|
deleted += delete_notifications_created_more_than_a_week_ago('technical-failure')
|
|
|
|
|
deleted += delete_notifications_created_more_than_a_week_ago('temporary-failure')
|
|
|
|
|
deleted += delete_notifications_created_more_than_a_week_ago('permanent-failure')
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete job started {} finished {} deleted {} failed notifications".format(
|
|
|
|
|
start,
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
deleted
|
|
|
|
|
)
|
|
|
|
|
)
|
2016-08-24 17:03:56 +01:00
|
|
|
except SQLAlchemyError as e:
|
|
|
|
|
current_app.logger.exception("Failed to delete failed notifications", e)
|
2016-06-20 13:33:53 +01:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete-invitations")
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2016-06-20 13:33:53 +01:00
|
|
|
def delete_invitations():
|
|
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
|
|
|
|
deleted = delete_invitations_created_more_than_two_days_ago()
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete job started {} finished {} deleted {} invitations".format(start, datetime.utcnow(), deleted)
|
|
|
|
|
)
|
2016-08-24 17:03:56 +01:00
|
|
|
except SQLAlchemyError as e:
|
|
|
|
|
current_app.logger.exception("Failed to delete invitations", e)
|
2016-06-20 13:33:53 +01:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='timeout-sending-notifications')
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2016-06-20 13:33:53 +01:00
|
|
|
def timeout_notifications():
|
2016-07-08 14:48:07 +01:00
|
|
|
# TODO: optimize the query by adding the date where clause to this query.
|
2016-06-20 13:33:53 +01:00
|
|
|
notifications = get_notifications(filter_dict={'status': 'sending'})
|
|
|
|
|
now = datetime.utcnow()
|
|
|
|
|
for noti in notifications:
|
|
|
|
|
try:
|
|
|
|
|
if (now - noti.created_at) > timedelta(
|
2016-08-24 17:03:56 +01:00
|
|
|
seconds=current_app.config.get('SENDING_NOTIFICATIONS_TIMEOUT_PERIOD')
|
2016-06-20 13:33:53 +01:00
|
|
|
):
|
2016-07-08 14:48:07 +01:00
|
|
|
# TODO: think about making this a bulk update rather than one at a time.
|
2016-08-25 11:55:38 +01:00
|
|
|
updated = update_notification_status_by_id(noti.id, 'temporary-failure')
|
2016-07-08 14:48:07 +01:00
|
|
|
if updated:
|
2016-08-25 11:55:38 +01:00
|
|
|
current_app.logger.info(
|
|
|
|
|
"Timeout period reached for notification ({}), status has been updated.".format(noti.id))
|
2016-06-20 13:33:53 +01:00
|
|
|
except Exception as e:
|
|
|
|
|
current_app.logger.exception(e)
|
2016-08-24 17:08:20 +01:00
|
|
|
current_app.logger.error(
|
|
|
|
|
"Exception raised trying to timeout notification ({}) skipping notification update.".format(noti.id)
|
|
|
|
|
)
|