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

Create Django Transport #2228

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion docs/includes/introduction.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ Transport Comparison
+---------------+----------+------------+------------+---------------+--------------+-----------------------+
| *Pyro* | Virtual | Yes | Yes [#f1]_ | No | No | No |
+---------------+----------+------------+------------+---------------+--------------+-----------------------+
| *Django* | Virtual | Yes | Yes | Yes | Yes | Yes |
+---------------+----------+------------+------------+---------------+--------------+-----------------------+


.. [#f1] Declarations only kept in memory, so exchanges/queues
Expand Down Expand Up @@ -264,4 +266,3 @@ There are some concepts you should be familiar with before starting:
zero or more words. For example `"*.stock.#"` matches the
routing keys `"usd.stock"` and `"eur.stock.db"` but not
`"stock.nasdaq"`.

1 change: 1 addition & 0 deletions docs/reference/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Kombu Transports
kombu.transport.virtual.exchange
kombu.transport.azurestoragequeues
kombu.transport.azureservicebus
kombu.transport.django
kombu.transport.pyamqp
kombu.transport.librabbitmq
kombu.transport.qpid
Expand Down
39 changes: 39 additions & 0 deletions docs/reference/kombu.transport.django.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
==============================================
Django Transport - ``kombu.transport.django``
==============================================

.. currentmodule:: kombu.transport.django

.. automodule:: kombu.transport.django

.. contents::
:local:

Transport
---------

.. autoclass:: Transport
:members:
:undoc-members:

Channel
-------

.. autoclass:: Channel
:members:
:undoc-members:

Django Transport Migrations - ``kombu.transport.django.models``
===============================================================

.. currentmodule:: kombu.transport.django

.. automodule:: kombu.transport.django.migrations
:members:
:undoc-members:
:show-inheritance:

.. automodule:: kombu.transport.django.migrations.0001_initial
:members:
:undoc-members:
:show-inheritance:
3 changes: 3 additions & 0 deletions docs/userguide/connections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ All of these are valid URLs:
# Using Pyro with name server running on 'localhost'
pyro://localhost/kombu.broker

# Using Django
django:///


The query part of the URL can also be used to set options, e.g.:

Expand Down
1 change: 1 addition & 0 deletions kombu/transport/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def supports_librabbitmq() -> bool | None:
'azureservicebus': 'kombu.transport.azureservicebus:Transport',
'pyro': 'kombu.transport.pyro:Transport',
'gcpubsub': 'kombu.transport.gcpubsub:Transport',
'django': 'kombu.transport.django.transport.Transport',
}

_transport_cache = {}
Expand Down
11 changes: 11 additions & 0 deletions kombu/transport/django/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from __future__ import annotations

from django.apps import AppConfig


class KombuConfig(AppConfig):
"""Django app config."""

default_auto_field = "django.db.models.BigAutoField"
name = "kombu.transport.django"
label = "kombu"
111 changes: 111 additions & 0 deletions kombu/transport/django/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
"""The initial migration for the Django transport."""

# Generated by Django 5.1.5 on 2025-01-20 18:26

from __future__ import annotations

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
"""The initial migration."""

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="Exchange",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=200, unique=True)),
],
),
migrations.CreateModel(
name="Queue",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=200, unique=True)),
],
),
migrations.CreateModel(
name="Message",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("visible", models.BooleanField(db_index=True, default=True)),
(
"sent_at",
models.DateTimeField(auto_now_add=True, db_index=True, null=True),
),
("message", models.TextField()),
("version", models.PositiveIntegerField(default=1)),
("priority", models.PositiveIntegerField(default=0)),
("ttl", models.IntegerField(blank=True, null=True)),
(
"queue",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="messages",
to="kombu.queue",
),
),
],
),
migrations.CreateModel(
name="Binding",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("routing_key", models.CharField(max_length=255, null=True)),
(
"exchange",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="bindings",
to="kombu.exchange",
),
),
(
"queue",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="bindings",
to="kombu.queue",
),
),
],
),
]
3 changes: 3 additions & 0 deletions kombu/transport/django/migrations/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""The migration files for the Django transport."""

from __future__ import annotations
60 changes: 60 additions & 0 deletions kombu/transport/django/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from __future__ import annotations

from datetime import timedelta

from django.db import models
from django.utils import timezone


class Queue(models.Model):
"""The queue."""

name = models.CharField(max_length=200, unique=True)

def __str__(self):
return self.name


class Message(models.Model):
"""The message."""

visible = models.BooleanField(default=True, db_index=True)
sent_at = models.DateTimeField(null=True, db_index=True, auto_now_add=True)
message = models.TextField()
version = models.PositiveIntegerField(default=1)
priority = models.PositiveIntegerField(default=0)
ttl = models.IntegerField(
null=True, blank=True
) # TTL in seconds (null means no TTL)
queue = models.ForeignKey(Queue, on_delete=models.CASCADE, related_name="messages")

def __str__(self):
return f"{self.sent_at} {self.message} {self.queue_id}"

def is_expired(self):
if self.ttl is None:
return False # No TTL set, so not expired
expiration_time = self.sent_at + timedelta(seconds=self.ttl)
return expiration_time < timezone.now()


class Exchange(models.Model):
"""The exchange."""

name = models.CharField(max_length=200, unique=True)

def __str__(self):
return f"{self.name}"

Check warning on line 47 in kombu/transport/django/models.py

View check run for this annotation

Codecov / codecov/patch

kombu/transport/django/models.py#L47

Added line #L47 was not covered by tests


class Binding(models.Model):
"""The binding."""

queue = models.ForeignKey(Queue, on_delete=models.CASCADE, related_name="bindings")
exchange = models.ForeignKey(
Exchange, on_delete=models.CASCADE, related_name="bindings"
)
routing_key = models.CharField(max_length=255, null=True)

def __str__(self):
return f"Binding: {self.queue.name} -> {self.exchange.name} with routing_key {self.routing_key}"

Check warning on line 60 in kombu/transport/django/models.py

View check run for this annotation

Codecov / codecov/patch

kombu/transport/django/models.py#L60

Added line #L60 was not covered by tests
Loading