Add job to queue as soon as it in created.

Added status to job.
This commit is contained in:
Adam Shimali
2016-02-02 14:58:25 +00:00
parent b5c662eca8
commit 0ade39e63f
5 changed files with 65 additions and 2 deletions

View File

@@ -1,7 +1,11 @@
import boto3
import json
from flask import (
Blueprint,
jsonify,
request
request,
current_app
)
from sqlalchemy.exc import DataError
@@ -46,6 +50,18 @@ def create_job(service_id):
return jsonify(result="error", message=errors), 400
try:
save_job(job)
_enqueue_job(job)
except Exception as e:
return jsonify(result="error", message=str(e)), 500
return jsonify(data=job_schema.dump(job).data), 201
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)
job_json = json.dumps({'job_id': str(job.id), 'service_id': str(job.service.id)})
queue.send_message(MessageBody=job_json,
MessageAttributes={'job_id': {'StringValue': str(job.id), 'DataType': 'String'},
'service_id': {'StringValue': str(job.service.id), 'DataType': 'String'}})

View File

@@ -129,6 +129,9 @@ class Template(db.Model):
service = db.relationship('Service', backref=db.backref('templates', lazy='dynamic'))
JOB_STATUS_TYPES = ['pending', 'in progress', 'finished']
class Job(db.Model):
__tablename__ = 'jobs'
@@ -152,6 +155,7 @@ class Job(db.Model):
unique=False,
nullable=True,
onupdate=datetime.datetime.now)
status = db.Column(db.Enum(*JOB_STATUS_TYPES, name='job_status_types'), nullable=False, default='pending')
VERIFY_CODE_TYPES = ['email', 'sms']