Job processing respects sendlimits

- If a job starts it MUST be able to fit into the days sending limits
- So if service limit is 10, and we've sent 5 messages and the current job is 4 then it's OK.
- If the job is 6 then it's over the limit and it should fail
- Job should NOT start if can't complete in the limit
This commit is contained in:
Martyn Inglis
2016-03-09 11:28:52 +00:00
parent 61a0cf32c8
commit 14d621d243
2 changed files with 71 additions and 5 deletions

View File

@@ -1,10 +1,14 @@
from app import create_uuid, DATETIME_FORMAT
from app import create_uuid, DATETIME_FORMAT, DATE_FORMAT
from app import notify_celery, encryption, firetext_client, aws_ses_client
from app.clients.email.aws_ses import AwsSesClientException
from app.clients.sms.firetext import FiretextClientException
from app.dao.services_dao import dao_fetch_service_by_id
from app.dao.templates_dao import dao_get_template_by_id
from app.dao.notifications_dao import dao_create_notification, dao_update_notification
from app.dao.notifications_dao import (
dao_create_notification,
dao_update_notification,
dao_get_notification_statistics_for_service_and_day
)
from app.dao.jobs_dao import dao_update_job, dao_get_job_by_id
from app.models import Notification, TEMPLATE_TYPE_EMAIL, TEMPLATE_TYPE_SMS
from flask import current_app
@@ -19,12 +23,35 @@ from utils.recipients import RecipientCSV, first_column_heading
def process_job(job_id):
start = datetime.utcnow()
job = dao_get_job_by_id(job_id)
service = job.service
stats = dao_get_notification_statistics_for_service_and_day(
service_id=service.id,
day=job.created_at.strftime(DATE_FORMAT)
)
if stats:
sending_limit = service.limit
job_size = job.notification_count
total_sent = stats.emails_requested + stats.sms_requested
if total_sent + job_size >= sending_limit:
finished = datetime.utcnow()
job.status = 'finished'
job.processing_finished = finished
dao_update_job(job)
current_app.logger.info(
"Job {} size {} error. Sending limits {} exceeded".format(job_id, job.notification_count, service.limit)
)
return
job.status = 'in progress'
dao_update_job(job)
for recipient, personalisation in RecipientCSV(
s3.get_job_from_s3(job.bucket_name, job_id),
template_type=job.template.template_type
s3.get_job_from_s3(job.bucket_name, job_id),
template_type=job.template.template_type
).recipients_and_personalisation:
encrypted = encryption.encrypt({