import boto3 import time import json class Bernard(object): def __init__(self, app=None): self.sqs = boto3.client("sqs", region_name='eu-west-1') 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