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

amateur question #34

Open
daniildotpl opened this issue Jan 8, 2025 · 2 comments
Open

amateur question #34

daniildotpl opened this issue Jan 8, 2025 · 2 comments

Comments

@daniildotpl
Copy link

Hello and sorry for the amateur question..
I use the documentation and write my code like this:

in views.py:

from dramatiq import get_broker
from dramatiq_abort import Abortable, backends, abort

abortable = Abortable(backend=backends.RedisBackend())
get_broker().add_middleware(abortable)

...
def tesr_abort():
    ...
    abort(message_id)
    ...

But I get this error get this error:

TypeError: RedisBackend.__init__() missing 1 required keyword-only argument: 'client'

My settings.py are the same as for django_dramatiq:

DRAMATIQ_REDIS_URL = os.getenv("REDIS_URL", "redis://127.0.0.1:6379/0")
DRAMATIQ_BROKER = {
    "BROKER": "dramatiq.brokers.redis.RedisBroker",
    "OPTIONS": {
        "connection_pool": redis.ConnectionPool.from_url(DRAMATIQ_REDIS_URL),
    },
    "MIDDLEWARE": [
        "dramatiq.middleware.AgeLimit",
        "dramatiq.middleware.TimeLimit",
        "dramatiq.middleware.Retries",
        "django_dramatiq.middleware.AdminMiddleware",
        "django_dramatiq.middleware.DbConnectionsMiddleware",
        # "dramatiq.middleware.GroupCallbacks",
    ]
}
DRAMATIQ_RESULT_BACKEND = {
    "BACKEND": "dramatiq.results.backends.redis.RedisBackend",
    "BACKEND_OPTIONS": {
        "url": "redis://localhost:6379",
    },
    "MIDDLEWARE_OPTIONS": {
        "result_ttl": 1000 * 60 * 10
    }
}

Please help me). What am I doing wrong?)

@Tatayoyoh
Copy link

Tatayoyoh commented Jan 14, 2025

Hey @daniildotpl

I faced the same problem today. I don't understant why a required parameter is not in documentation but this should solve your problem :

import redis
client = redis.Redis(host='localhost', port=6379, db=0)
abortable = Abortable(backend=dramatiq_abort.backends.RedisBackend(client=client))
get_broker().add_middleware(abortable)

BUT in my case I had another error : TypeError: 'Pipeline' object does not support the context manager protocol.

@isra17 it was fixed editing backends/redis.py. Should I open a PR ?

    def notify(self, events: Iterable[Event], ttl: int) -> None:
        pipe = self.client.pipeline()
        # with self.client.pipeline() as pipe:   # <----- error was from this line
        for key, val in events:
            pipe.rpush(self._encode_key(key), self._encode_value(val))
            pipe.pexpire(self._encode_key(key), ttl)
        pipe.execute()

Docker context:

  • redis:7.4-alpine
  • python:3.12-slim

Python context:

  • redis==5.2.1
  • hiredis==3.1.0
  • dramatiq==1.17.1
  • dramatiq-abort==1.2.1

@isra17
Copy link
Member

isra17 commented Jan 14, 2025

Documentation fix make sense.

As for the pipeline, this is really weird. Pipeline is a context manager since 2.4.7: https://github.com/redis/redis-py/blob/v5.2.1/redis/client.py#L1237

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants