Skip to content

Instantly share code, notes, and snippets.

@marcelnicolay
Created July 20, 2012 20:29
Show Gist options
  • Save marcelnicolay/3153049 to your computer and use it in GitHub Desktop.
Save marcelnicolay/3153049 to your computer and use it in GitHub Desktop.
SimpleQueue - SQS
from boto.sqs.connection import SQSConnection
from boto.sqs.message import Message
from torneira import settings
import pickle
import logging
__connection__ = None
__queue__ = None
class Singleton(object):
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(Singleton, cls).__new__(
cls, *args, **kwargs)
return cls._instance
class SimpleQueue(Singleton):
def __init__(self):
self._connection = None
def get_connection(self):
global __connection__
if not __connection__:
logging.info("[SimpleQueue] - Create SQS Connection")
__connection__ = SQSConnection(settings.AWS_ACCESS_KEY, settings.AWS_SECRET_ACCESS_KEY)
return __connection__
def get_queue(self):
global __queue__
if not __queue__:
logging.info("[SimpleQueue] - Create or Get queue %s " % settings.AWS_QUEUE_NAME)
conn = self.get_connection()
__queue__ = conn.create_queue(settings.AWS_QUEUE_NAME, 120)
return __queue__
def clear_connection(self):
global __connection__
__connection__ = None
def put(self, message):
logging.info("[SimpleQueue] - PUT message %s" % message)
try:
queue = self.get_queue()
m = Message()
m.set_body(pickle.dumps(message))
status = queue.write(m)
logging.info("[SimpleQueue] - PUT message %s success" % message)
return status
except:
logging.exception("[SimpleQueue] - PUT message %s ERRO " % message)
self.clear_connection()
return False
def read(self):
queue = self.get_queue()
message = queue.read(60)
return message
def delete(self, message):
logging.info("[SimpleQueue] - DELETE message")
queue = self.get_queue()
queue.delete_message(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment