Add an endpoint to cancel a job

If you schedule a job you might change your mind or circumstances might
change. So you need to be able to cancel it. This commit adds a `POST`
endpoint for individual jobs which sets their status to `cancelled`.

This also means adding a new status of `cancelled`, so there’s a
migration…
This commit is contained in:
Chris Hill-Scott
2016-09-01 14:31:01 +01:00
parent aa8ee3a8da
commit 4a7267be8b
7 changed files with 92 additions and 2 deletions

View File

@@ -44,6 +44,13 @@ def dao_get_scheduled_jobs():
.all()
def dao_get_future_scheduled_job_by_id_and_service_id(job_id, service_id):
return Job.query \
.filter_by(service_id=service_id, id=job_id) \
.filter(Job.job_status == 'scheduled', Job.scheduled_for > datetime.utcnow()) \
.one()
def dao_create_job(job):
db.session.add(job)
db.session.commit()

View File

@@ -7,8 +7,10 @@ from flask import (
from app.dao.jobs_dao import (
dao_create_job,
dao_update_job,
dao_get_job_by_service_id_and_job_id,
dao_get_jobs_by_service_id,
dao_get_future_scheduled_job_by_id_and_service_id,
dao_get_notification_outcomes_for_job
)
@@ -28,7 +30,7 @@ from app.schemas import (
from app.celery.tasks import process_job
from app.models import JOB_STATUS_SCHEDULED, JOB_STATUS_PENDING
from app.models import JOB_STATUS_SCHEDULED, JOB_STATUS_PENDING, JOB_STATUS_CANCELLED
from app.utils import pagination_links
@@ -53,6 +55,15 @@ def get_job_by_service_and_job_id(service_id, job_id):
return jsonify(data=data)
@job.route('/<job_id>/cancel', methods=['POST'])
def cancel_job(service_id, job_id):
job = dao_get_future_scheduled_job_by_id_and_service_id(job_id, service_id)
job.job_status = JOB_STATUS_CANCELLED
dao_update_job(job)
return get_job_by_service_and_job_id(service_id, job_id)
@job.route('/<job_id>/notifications', methods=['GET'])
def get_all_notifications_for_service_job(service_id, job_id):
data = notifications_filter_schema.load(request.args).data

View File

@@ -308,6 +308,7 @@ JOB_STATUS_IN_PROGRESS = 'in progress'
JOB_STATUS_FINISHED = 'finished'
JOB_STATUS_SENDING_LIMITS_EXCEEDED = 'sending limits exceeded'
JOB_STATUS_SCHEDULED = 'scheduled'
JOB_STATUS_CANCELLED = 'cancelled'
class JobStatus(db.Model):