Example #1
0
    def messages(self, attributes="All", visibilityTimeout=None, autodelete=True, neverfail=True):
        """
        Generator to retrieve infinitely messages from a queue
        @param visibilityTimeout:
        @type visibilityTimeout: int
        @param autodelete: the message deletion will be attempted upon release
        @type autodelete: bool
        @param neverfail: don't fail on aws exceptions
        @type neverfail: bool
        @rtype: SQSMessage
        """
        while True:
            try:
                items = self.sqsclient.receiveMessage(
                    self.qName,
                    attributes,
                    maxNumberOfMessages=1,
                    visibilityTimeout=visibilityTimeout,
                    waitTimeSeconds=20,
                )
            except Exception as e:
                if neverfail:
                    self.logger.warn("receiveMessage error [%s]", e)
                    time.sleep(self.COOLDOWN_BETWEEN_RECEIVEMESSAGES)
                    continue
                raise

            if not items:
                time.sleep(self.COOLDOWN_BETWEEN_RECEIVEMESSAGES)
                continue

            item = items[0]

            message = SQSMessage(item["Body"])
            message.id = item["MessageId"]
            message.receiptHandle = item["ReceiptHandle"]
            message.queue = self
            message.visibilityTimeout = visibilityTimeout
            message.receptionTimestamp = time.time()
            for attribute in item["Attribute"]:
                setattr(message, attribute["Name"], attribute["Value"])

            yield message

            if autodelete:
                timeleft = message.visibilityTimeoutLeft()
                if timeleft < 0:
                    self.logger.warn("Missed the message deletion by %s s. %s", -1 * timeleft, message)
                else:
                    try:
                        self.delete(message)
                    except Exception as e:
                        if neverfail:
                            self.logger.warn("Could not delete the message %s [%s]", message, e)
                        raise
Example #2
0
    def receive(self, attributes="All", maxNumberOfMessages=1, visibilityTimeout=None, waitTimeSeconds=None):
        """
        @type visibilityTimeout: int
        @type waitTimeSeconds: int
        @type maxNumberOfMessages: int
        """
        messages = []
        if waitTimeSeconds is None or waitTimeSeconds <= 20:
            messages = self.sqsclient.receiveMessage(
                self.qName, attributes, maxNumberOfMessages, visibilityTimeout, waitTimeSeconds
            )
        else:
            starttime = int(time.time())
            while True:
                remainingtime = waitTimeSeconds - (int(time.time()) - starttime)
                _waitTimeSeconds = min(remainingtime, 20)
                if _waitTimeSeconds <= 0:
                    return None
                print(time.time(), waitTimeSeconds, _waitTimeSeconds)
                messages = self.sqsclient.receiveMessage(
                    self.qName, attributes, maxNumberOfMessages, visibilityTimeout, _waitTimeSeconds
                )
                if messages:
                    break
                time.sleep(self.COOLDOWN_BETWEEN_RECEIVEMESSAGES)

        if not messages:
            if maxNumberOfMessages == 1:
                return None
            else:
                return []

        result = []
        if visibilityTimeout is None:
            visibilityTimeout = int(self.VisibilityTimeout)

        for item in messages:
            message = SQSMessage(item["Body"])
            message.id = item["MessageId"]
            message.receiptHandle = item["ReceiptHandle"]
            message.queue = self
            message.visibilityTimeout = visibilityTimeout
            message.receptionTimestamp = time.time()
            for attribute in item["Attribute"]:
                setattr(message, attribute["Name"], attribute["Value"])
            if maxNumberOfMessages == 1:
                return message
            result.append(message)
        return result