diff --git a/qoo/queues.py b/qoo/queues.py index f9445f0..11d54e0 100644 --- a/qoo/queues.py +++ b/qoo/queues.py @@ -6,7 +6,7 @@ import time import hashlib from qoo.utils import jsond, jsonl -from typing import List, Optional +from typing import Dict, List, Optional class Job(object): @@ -20,14 +20,15 @@ def __init__(self, sqs_message: dict, queue: "Queue") -> None: self._id = self._data["MessageId"] self._body = jsonl(self._data["Body"]) self._attributes = self._data["Attributes"] + self._sent_at = float(self._attributes["SentTimestamp"]) / 1000 self._received_at = float(time.time()) - self.elapsed = time.time() - self._body["created_at"] + self.elapsed = self._received_at - self._sent_at self.approximate_receive_count = int( self._attributes["ApproximateReceiveCount"] ) for key in self._body: setattr(self, key, self._body[key]) - self.handle = self._data["ReceiptHandle"] + self._handle = self._data["ReceiptHandle"] def __str__(self) -> str: """return a human-friendly object representation""" @@ -37,13 +38,13 @@ def __repr__(self) -> str: """repr""" return self.__str__() - def __del__(self) -> dict: + def __del__(self) -> Dict: """del keyword for the job""" return self.delete() - def delete(self) -> dict: + def delete(self) -> Dict: """delete this object""" - return self._queue.delete_job(self.handle) + return self._queue.delete_job(self._handle) @property def md5_matches(self) -> bool: @@ -138,20 +139,23 @@ def send_job(self, **attributes) -> str: using the kwarg attributes, send a job to this queue. pass job attributes to set the message body """ - attributes.update({"created_at": int(time.time())}) response = self._client.send_message( MessageBody=jsond(attributes), QueueUrl=self._queue_url ) return response["MessageId"] def receive_jobs( - self, max_messages: int = None, wait_time: int = None + self, + max_messages: int = None, + wait_time: int = None, + attribute_names: str = "All", ) -> List[Job]: """receive a list of jobs from the queue""" jobs = self._client.receive_message( QueueUrl=self._queue_url, MaxNumberOfMessages=max_messages if max_messages else self._max_messages, WaitTimeSeconds=wait_time if wait_time else self._wait_time, + AttributeNames=[attribute_names], ) if "Messages" not in jobs: return [] @@ -162,7 +166,7 @@ def receive(self, wait_time: int = None) -> Optional[Job]: jobs = self.receive_jobs(max_messages=1, wait_time=wait_time) return jobs[0] if jobs else None - def delete_job(self, handle: str) -> dict: + def delete_job(self, handle: str) -> Dict: """delete a job by the message handle""" return self._client.delete_message( QueueUrl=self._queue_url, ReceiptHandle=handle diff --git a/setup.py b/setup.py index 77b22bb..ff7181c 100755 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ setup( name="qoo", - version="0.0.1", + version="0.0.2", description=("A simple library for interacting with Amazon SQS."), long_description=LONG_DESCRIPTION, author="Jacobi Petrucciani", @@ -24,7 +24,6 @@ packages=["qoo"], install_requires=REQUIRED, classifiers=[ - "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", diff --git a/tests/test_qoo.py b/tests/test_qoo.py index 8ad180d..c7ed23d 100644 --- a/tests/test_qoo.py +++ b/tests/test_qoo.py @@ -75,6 +75,7 @@ def test_can_send_and_receive_job(queue): assert job assert job.md5_matches assert job.approximate_receive_count == 1 + assert job.elapsed > 0.0 def test_can_delete_job(queue_with_job):