mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 17:31:14 -05:00
Updates to create job to handle scheduling
- If the job JSON contains a scheduling date then the new 'job_status" column is set to "scheduled" - the date is persisted on the JOB row in the database - Also the job WILL NOT be placed onto the queue of jobs. This is deferred to a later celery beat task.
This commit is contained in:
@@ -27,6 +27,8 @@ from app.schemas import (
|
||||
|
||||
from app.celery.tasks import process_job
|
||||
|
||||
from app.models import JOB_STATUS_SCHEDULED, JOB_STATUS_PENDING
|
||||
|
||||
from app.utils import pagination_links
|
||||
|
||||
job = Blueprint('job', __name__, url_prefix='/service/<uuid:service_id>/job')
|
||||
@@ -105,7 +107,15 @@ def create_job(service_id):
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
|
||||
data.update({"template_version": template.version})
|
||||
|
||||
job = job_schema.load(data).data
|
||||
|
||||
if job.scheduled_for:
|
||||
job.job_status = JOB_STATUS_SCHEDULED
|
||||
|
||||
dao_create_job(job)
|
||||
process_job.apply_async([str(job.id)], queue="process-job")
|
||||
|
||||
if job.job_status == JOB_STATUS_PENDING:
|
||||
process_job.apply_async([str(job.id)], queue="process-job")
|
||||
|
||||
return jsonify(data=job_schema.dump(job).data), 201
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import re
|
||||
from datetime import (
|
||||
datetime,
|
||||
date
|
||||
)
|
||||
date,
|
||||
timedelta)
|
||||
from flask_marshmallow.fields import fields
|
||||
|
||||
from marshmallow import (
|
||||
post_load,
|
||||
ValidationError,
|
||||
@@ -40,11 +39,21 @@ def _validate_positive_number(value, msg="Not a positive integer"):
|
||||
raise ValidationError(msg)
|
||||
|
||||
|
||||
def _validate_not_more_than_24_hours_in_future(dte, msg="Date cannot be more than 24hrs in the future"):
|
||||
if dte > datetime.utcnow() + timedelta(hours=24):
|
||||
raise ValidationError(msg)
|
||||
|
||||
|
||||
def _validate_not_in_future(dte, msg="Date cannot be in the future"):
|
||||
if dte > date.today():
|
||||
raise ValidationError(msg)
|
||||
|
||||
|
||||
def _validate_not_in_past(dte, msg="Date cannot be in the past"):
|
||||
if dte < datetime.today():
|
||||
raise ValidationError(msg)
|
||||
|
||||
|
||||
# TODO I think marshmallow provides a better integration and error handling.
|
||||
# Would be better to replace functionality in dao with the marshmallow supported
|
||||
# functionality.
|
||||
@@ -210,6 +219,13 @@ class JobSchema(BaseSchema):
|
||||
|
||||
job_status = field_for(models.JobStatus, 'name', required=False)
|
||||
|
||||
scheduled_for = fields.DateTime()
|
||||
|
||||
@validates('scheduled_for')
|
||||
def validate_scheduled_for(self, value):
|
||||
_validate_not_in_past(value)
|
||||
_validate_not_more_than_24_hours_in_future(value)
|
||||
|
||||
class Meta:
|
||||
model = models.Job
|
||||
exclude = ('notifications',)
|
||||
|
||||
Reference in New Issue
Block a user