Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add QueueService cleanup method #16

Merged
merged 1 commit into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions fastqueue/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ def stats(cls, id: str, session: Session) -> QueueStatsSchema:
oldest_unacked_message_age_seconds=oldest_unacked_message_age_seconds,
)

@classmethod
def cleanup(cls, id: str, session: Session) -> None:
queue = cls.get(id=id, session=session)
now = datetime.utcnow()

expired_at_filter = [Message.queue_id == queue.id, Message.expired_at <= now]
session.query(Message).filter(*expired_at_filter).delete()

if queue.message_max_deliveries is not None:
delivery_attempts_filter = [
Message.queue_id == queue.id,
Message.delivery_attempts >= queue.message_max_deliveries,
]
session.query(Message).filter(*delivery_attempts_filter).delete()

session.commit()


class MessageService:
@classmethod
Expand Down
29 changes: 29 additions & 0 deletions tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,35 @@ def test_queue_service_stats(session, queue):
assert result.oldest_unacked_message_age_seconds == 10


def test_queue_service_cleanup_expired_at(session, queue):
message1 = MessageFactory(queue_id=queue.id, expired_at=datetime.utcnow() - timedelta(seconds=1))
message2 = MessageFactory(queue_id=queue.id, expired_at=datetime.utcnow() + timedelta(seconds=1))
session.add(message1)
session.add(message2)
session.commit()
assert session.query(Message).filter_by(queue_id=queue.id).count() == 2

assert QueueService.cleanup(id=queue.id, session=session) is None
assert session.query(Message).filter_by(queue_id=queue.id).count() == 1
assert session.query(Message).filter_by(queue_id=queue.id).first() == message2


def test_queue_service_cleanup_delivery_attempts(session, queue):
queue.message_max_deliveries = 2
message1 = MessageFactory(queue_id=queue.id, delivery_attempts=1)
message2 = MessageFactory(queue_id=queue.id, delivery_attempts=2)
message3 = MessageFactory(queue_id=queue.id, delivery_attempts=3)
session.add(message1)
session.add(message2)
session.add(message3)
session.commit()
assert session.query(Message).filter_by(queue_id=queue.id).count() == 3

assert QueueService.cleanup(id=queue.id, session=session) is None
assert session.query(Message).filter_by(queue_id=queue.id).count() == 1
assert session.query(Message).filter_by(queue_id=queue.id).first() == message1


@pytest.mark.parametrize(
"queue_filters,message_attributes,expected",
[
Expand Down