Files
api2/api/lib/bernard.py
T

38 lines
1009 B
Python

import boto3
import time
import json
class Bernard(object):
def __init__(self, app=None):
self.sqs = boto3.client("sqs")
if app:
self.init_app(app)
def init_app(self, app):
if not "BERNARD_QUEUE_URL" in app.config:
raise Exception("BERNARD_QUEUE_URL missing in config")
self.queue_url = app.config.get("BERNARD_QUEUE_URL")
def produce(self, task, args={}, retries=0):
args["name"] = task
if retries > 0:
args["retries"] = retries
message = {
"args": args,
"class": "Bernard:Message:DefaultMessage",
"timestamp": time.time(),
}
response = self.sqs.send_message(
QueueUrl=self.queue_url, MessageBody=json.dumps(message)
)
# if the response contains a MD5OfMessageBody we assume the message was successfully delivered to the queue
if "MD5OfMessageBody" in response:
return True
return False