2016-10-07 12:28:42 +01:00
|
|
|
from datetime import datetime
|
2016-06-20 13:33:53 +01:00
|
|
|
|
|
|
|
|
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-10-07 12:28:42 +01:00
|
|
|
from app.dao.jobs_dao import dao_set_scheduled_jobs_to_pending, dao_get_jobs_older_than
|
2016-09-13 16:42:53 +01:00
|
|
|
from app.dao.notifications_dao import (delete_notifications_created_more_than_a_week_ago,
|
|
|
|
|
dao_timeout_notifications)
|
2016-06-20 13:33:53 +01:00
|
|
|
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.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:
|
2016-10-07 12:28:42 +01:00
|
|
|
for job in dao_set_scheduled_jobs_to_pending():
|
|
|
|
|
process_job.apply_async([str(job.id)], queue="process-job")
|
2016-08-24 17:03:56 +01:00
|
|
|
current_app.logger.info(
|
2016-10-07 12:28:42 +01:00
|
|
|
"Job ID {} added to process job queue".format(job.id)
|
2016-08-24 17:03:56 +01:00
|
|
|
)
|
|
|
|
|
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-09-14 10:38:34 +01:00
|
|
|
updated = dao_timeout_notifications(current_app.config.get('SENDING_NOTIFICATIONS_TIMEOUT_PERIOD'))
|
|
|
|
|
if updated:
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Timeout period reached for {} notifications, status has been updated.".format(updated))
|