Skip to content

Commit

Permalink
fixing issues with sent_at and elapsed
Browse files Browse the repository at this point in the history
  • Loading branch information
jpetrucciani committed Aug 21, 2019
1 parent 53b5a9e commit 2fa1c77
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
22 changes: 13 additions & 9 deletions qoo/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"""
Expand All @@ -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:
Expand Down Expand Up @@ -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 []
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
1 change: 1 addition & 0 deletions tests/test_qoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

1 comment on commit 2fa1c77

@EverettBerry
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay!

Please sign in to comment.