2016-02-02 14:58:25 +00:00
|
|
|
import boto3
|
|
|
|
|
import json
|
|
|
|
|
|
2016-01-15 15:48:05 +00:00
|
|
|
from flask import (
|
|
|
|
|
Blueprint,
|
|
|
|
|
jsonify,
|
2016-02-02 14:58:25 +00:00
|
|
|
request,
|
|
|
|
|
current_app
|
2016-01-15 15:48:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
from sqlalchemy.exc import DataError
|
|
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
|
|
|
|
|
|
|
|
|
from app.dao.jobs_dao import (
|
|
|
|
|
save_job,
|
2016-01-18 09:57:04 +00:00
|
|
|
get_job,
|
|
|
|
|
get_jobs_by_service
|
2016-01-15 15:48:05 +00:00
|
|
|
)
|
|
|
|
|
|
2016-02-16 11:22:44 +00:00
|
|
|
from app.dao import notifications_dao
|
2016-02-09 14:17:42 +00:00
|
|
|
|
2016-01-15 15:48:05 +00:00
|
|
|
from app.schemas import (
|
|
|
|
|
job_schema,
|
2016-02-04 20:55:09 +00:00
|
|
|
jobs_schema,
|
2016-02-09 14:17:42 +00:00
|
|
|
job_schema_load_json,
|
|
|
|
|
notification_status_schema,
|
|
|
|
|
notifications_status_schema,
|
|
|
|
|
notification_status_schema_load_json
|
2016-01-15 15:48:05 +00:00
|
|
|
)
|
|
|
|
|
|
2016-02-02 14:16:08 +00:00
|
|
|
job = Blueprint('job', __name__, url_prefix='/service/<service_id>/job')
|
2016-01-15 15:48:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@job.route('/<job_id>', methods=['GET'])
|
2016-01-15 16:34:48 +00:00
|
|
|
@job.route('', methods=['GET'])
|
2016-01-18 09:57:04 +00:00
|
|
|
def get_job_for_service(service_id, job_id=None):
|
2016-01-15 15:48:05 +00:00
|
|
|
if job_id:
|
|
|
|
|
try:
|
2016-01-18 09:57:04 +00:00
|
|
|
job = get_job(service_id, job_id)
|
2016-01-15 15:48:05 +00:00
|
|
|
data, errors = job_schema.dump(job)
|
|
|
|
|
return jsonify(data=data)
|
|
|
|
|
except DataError:
|
|
|
|
|
return jsonify(result="error", message="Invalid job id"), 400
|
|
|
|
|
except NoResultFound:
|
|
|
|
|
return jsonify(result="error", message="Job not found"), 404
|
|
|
|
|
else:
|
2016-01-18 09:57:04 +00:00
|
|
|
jobs = get_jobs_by_service(service_id)
|
2016-01-15 15:48:05 +00:00
|
|
|
data, errors = jobs_schema.dump(jobs)
|
|
|
|
|
return jsonify(data=data)
|
|
|
|
|
|
|
|
|
|
|
2016-01-15 16:34:48 +00:00
|
|
|
@job.route('', methods=['POST'])
|
2016-01-18 09:57:04 +00:00
|
|
|
def create_job(service_id):
|
2016-01-15 15:48:05 +00:00
|
|
|
job, errors = job_schema.load(request.get_json())
|
|
|
|
|
if errors:
|
|
|
|
|
return jsonify(result="error", message=errors), 400
|
|
|
|
|
try:
|
|
|
|
|
save_job(job)
|
2016-02-02 14:58:25 +00:00
|
|
|
_enqueue_job(job)
|
2016-01-15 15:48:05 +00:00
|
|
|
except Exception as e:
|
|
|
|
|
return jsonify(result="error", message=str(e)), 500
|
|
|
|
|
return jsonify(data=job_schema.dump(job).data), 201
|
2016-02-02 14:58:25 +00:00
|
|
|
|
|
|
|
|
|
2016-02-04 20:55:09 +00:00
|
|
|
@job.route('/<job_id>', methods=['PUT'])
|
|
|
|
|
def update_job(service_id, job_id):
|
2016-02-05 13:07:02 +00:00
|
|
|
|
2016-02-04 20:55:09 +00:00
|
|
|
job = get_job(service_id, job_id)
|
|
|
|
|
update_dict, errors = job_schema_load_json.load(request.get_json())
|
|
|
|
|
if errors:
|
|
|
|
|
return jsonify(result="error", message=errors), 400
|
|
|
|
|
try:
|
|
|
|
|
save_job(job, update_dict=update_dict)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return jsonify(result="error", message=str(e)), 400
|
|
|
|
|
return jsonify(data=job_schema.dump(job).data), 200
|
|
|
|
|
|
|
|
|
|
|
2016-02-09 14:17:42 +00:00
|
|
|
@job.route('/<job_id>/notification', methods=['POST'])
|
|
|
|
|
def create_notification_for_job(service_id, job_id):
|
|
|
|
|
|
|
|
|
|
# TODO assert service_id == payload service id
|
|
|
|
|
# and same for job id
|
|
|
|
|
notification, errors = notification_status_schema.load(request.get_json())
|
|
|
|
|
if errors:
|
|
|
|
|
return jsonify(result="error", message=errors), 400
|
|
|
|
|
try:
|
2016-02-16 11:22:44 +00:00
|
|
|
notifications_dao.save_notification(notification)
|
2016-02-09 14:17:42 +00:00
|
|
|
except Exception as e:
|
|
|
|
|
return jsonify(result="error", message=str(e)), 500
|
|
|
|
|
return jsonify(data=notification_status_schema.dump(notification).data), 201
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@job.route('/<job_id>/notification', methods=['GET'])
|
|
|
|
|
@job.route('/<job_id>/notification/<notification_id>')
|
|
|
|
|
def get_notification_for_job(service_id, job_id, notification_id=None):
|
|
|
|
|
if notification_id:
|
|
|
|
|
try:
|
2016-02-16 11:22:44 +00:00
|
|
|
notification = notifications_dao.get_notification_for_job(service_id, job_id, notification_id)
|
2016-02-09 14:17:42 +00:00
|
|
|
data, errors = notification_status_schema.dump(notification)
|
|
|
|
|
return jsonify(data=data)
|
|
|
|
|
except DataError:
|
|
|
|
|
return jsonify(result="error", message="Invalid notification id"), 400
|
|
|
|
|
except NoResultFound:
|
|
|
|
|
return jsonify(result="error", message="Notification not found"), 404
|
|
|
|
|
else:
|
2016-02-16 11:22:44 +00:00
|
|
|
notifications = notifications_dao.get_notifications_for_job(service_id, job_id)
|
2016-02-09 14:17:42 +00:00
|
|
|
data, errors = notifications_status_schema.dump(notifications)
|
|
|
|
|
return jsonify(data=data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@job.route('/<job_id>/notification/<notification_id>', methods=['PUT'])
|
|
|
|
|
def update_notification_for_job(service_id, job_id, notification_id):
|
|
|
|
|
|
2016-02-16 11:22:44 +00:00
|
|
|
notification = notifications_dao.get_notification_for_job(service_id, job_id, notification_id)
|
2016-02-09 14:17:42 +00:00
|
|
|
update_dict, errors = notification_status_schema_load_json.load(request.get_json())
|
|
|
|
|
|
|
|
|
|
if errors:
|
|
|
|
|
return jsonify(result="error", message=errors), 400
|
|
|
|
|
try:
|
2016-02-16 11:22:44 +00:00
|
|
|
notifications_dao.save_notification(notification, update_dict=update_dict)
|
2016-02-09 14:17:42 +00:00
|
|
|
except Exception as e:
|
|
|
|
|
return jsonify(result="error", message=str(e)), 400
|
|
|
|
|
|
|
|
|
|
return jsonify(data=job_schema.dump(notification).data), 200
|
|
|
|
|
|
|
|
|
|
|
2016-02-02 14:58:25 +00:00
|
|
|
def _enqueue_job(job):
|
|
|
|
|
aws_region = current_app.config['AWS_REGION']
|
|
|
|
|
queue_name = current_app.config['NOTIFY_JOB_QUEUE']
|
|
|
|
|
|
|
|
|
|
queue = boto3.resource('sqs', region_name=aws_region).create_queue(QueueName=queue_name)
|
2016-02-04 12:34:53 +00:00
|
|
|
data = {
|
2016-02-05 13:07:02 +00:00
|
|
|
'id': str(job.id),
|
2016-02-05 16:33:07 +00:00
|
|
|
'service': str(job.service.id),
|
|
|
|
|
'template': job.template.id,
|
2016-02-05 13:07:02 +00:00
|
|
|
'bucket_name': job.bucket_name,
|
|
|
|
|
'file_name': job.file_name,
|
|
|
|
|
'original_file_name': job.original_file_name
|
2016-02-04 12:34:53 +00:00
|
|
|
}
|
|
|
|
|
job_json = json.dumps(data)
|
2016-02-02 14:58:25 +00:00
|
|
|
queue.send_message(MessageBody=job_json,
|
2016-02-05 13:07:02 +00:00
|
|
|
MessageAttributes={'id': {'StringValue': str(job.id), 'DataType': 'String'},
|
|
|
|
|
'service': {'StringValue': str(job.service.id), 'DataType': 'String'},
|
|
|
|
|
'template': {'StringValue': str(job.template.id), 'DataType': 'String'},
|
|
|
|
|
'bucket_name': {'StringValue': job.bucket_name, 'DataType': 'String'},
|
|
|
|
|
'file_name': {'StringValue': job.file_name, 'DataType': 'String'},
|
|
|
|
|
'original_file_name': {'StringValue': job.original_file_name,
|
|
|
|
|
'DataType': 'String'}})
|