How to use the dramatiq.brokers.redis.RedisBroker function in dramatiq

To help you get started, we’ve selected a few dramatiq examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Bogdanp / dramatiq / tests / test_redis.py View on Github external
def test_redis_broker_warns_about_deprecated_parameters():
    # When I pass deprecated params to RedisBroker
    # Then it should warn me that those params do nothing
    with pytest.warns(DeprecationWarning) as record:
        RedisBroker(requeue_deadline=1000)

    assert str(record[0].message) == \
        "requeue_{deadline,interval} have been deprecated and no longer do anything"
github Bogdanp / dramatiq / tests / test_watch.py View on Github external
import time
from pathlib import Path

import pytest

import dramatiq
from dramatiq.brokers.redis import RedisBroker
from dramatiq.common import current_millis

from .common import skip_in_ci, skip_on_pypy, skip_on_windows

broker = RedisBroker()
loaded_at = current_millis()


@dramatiq.actor(broker=broker)
def write_loaded_at(filename):
    with open(filename, "w") as f:
        f.write(str(loaded_at))


@skip_in_ci
@skip_on_windows
@skip_on_pypy
@pytest.mark.parametrize("extra_args", [
    (),
    ("--watch-use-polling",),
])
github Bogdanp / dramatiq / tests / test_redis.py View on Github external
def test_redis_broker_can_connect_via_url():
    # Given that I have a connection string
    # When I pass that to RedisBroker
    broker = RedisBroker(url="redis://127.0.0.1")

    # Then I should get back a valid connection
    assert broker.client.ping()
github SolarArbiter / solarforecastarbiter-core / solarforecastarbiter / tasks.py View on Github external
"""
High-level tasks that imports other tasks and defines the task queue
"""
import os


import dramatiq
from dramatiq.brokers.stub import StubBroker


from solarforecastarbiter.validation import tasks as validation_tasks


if 'REDIS_URL' in os.environ:  # pragma: no cover
    from dramatiq.brokers.redis import RedisBroker
    broker = RedisBroker(url=os.environ['REDIS_URL'],
                         db=0,
                         namespace='sfa-queue')
else:
    broker = StubBroker()
dramatiq.set_broker(broker)


def enqueue_function(func, *args, **kwargs):
    """Convience function to queue function. Will allow for altering task
    queue without changing code that queues up the tasks. If broker
    is not a StubBroker, the task is sent to the broker. Otherwise
    the task is commputed synchronously"""
    if isinstance(broker, StubBroker):
        return func(*args, **kwargs)
    else:
        return func.send(*args, **kwargs)
github Bogdanp / dramatiq / examples / time_limit / example.py View on Github external
import os
import sys
import time

import dramatiq

if os.getenv("REDIS") == "1":
    from dramatiq.brokers.redis import RedisBroker
    broker = RedisBroker()
    dramatiq.set_broker(broker)


@dramatiq.actor(time_limit=5000, max_retries=3)
def long_running():
    logger = long_running.logger

    while True:
        logger.info("Sleeping...")
        time.sleep(1)


@dramatiq.actor(time_limit=5000, max_retries=0)
def long_running_with_catch():
    logger = long_running_with_catch.logger
github Bogdanp / dramatiq / benchmarks / bench.py View on Github external
import random
import sys
import subprocess
import time

from dramatiq.brokers.redis import RedisBroker
from dramatiq.brokers.rabbitmq import RabbitmqBroker

logger = logging.getLogger("example")
counter_key = "latench-bench-counter"
memcache_client = pylibmc.Client(["localhost"], binary=True)
memcache_pool = pylibmc.ClientPool(memcache_client, 8)
random.seed(1337)

if os.getenv("REDIS") == "1":
    broker = RedisBroker()
    dramatiq.set_broker(broker)
    celery_app = celery.Celery(broker="redis:///")

else:
    broker = RabbitmqBroker()
    dramatiq.set_broker(broker)
    celery_app = celery.Celery(broker="amqp:///")


def fib_bench(n):
    p, q = 0, 1
    while n > 0:
        p, q = q, p + q
        n -= 1

    with memcache_pool.reserve() as client:
github Bogdanp / dramatiq / examples / persistent / example.py View on Github external
import argparse
import json
import os
import sys
import time

import dramatiq
from dramatiq.middleware import Shutdown

if os.getenv("REDIS") == "1":
    from dramatiq.brokers.redis import RedisBroker
    broker = RedisBroker()
    dramatiq.set_broker(broker)


def path_to(*xs):
    return os.path.abspath(os.path.join(os.path.dirname(__file__), *(str(x) for x in xs)))


def load_state(n):
    try:
        with open(path_to("states", n), "r") as f:
            data = json.load(f)
    except Exception:
        fib.logger.info("Could not read state file, using defaults.")
        return 1, 1, 0

    i, x2, x1 = data["i"], data["x2"], data["x1"]
github SamR1 / FitTrackee / fittrackee_api / fittrackee_api / config.py View on Github external
import os

from dramatiq.brokers.redis import RedisBroker
from dramatiq.brokers.stub import StubBroker
from flask import current_app

if os.getenv('APP_SETTINGS') == 'fittrackee_api.config.TestingConfig':
    broker = StubBroker
else:
    broker = RedisBroker


class BaseConfig:
    """Base configuration"""

    DEBUG = False
    TESTING = False
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    BCRYPT_LOG_ROUNDS = 13
    TOKEN_EXPIRATION_DAYS = 30
    TOKEN_EXPIRATION_SECONDS = 0
    PASSWORD_TOKEN_EXPIRATION_SECONDS = 3600
    UPLOAD_FOLDER = os.path.join(current_app.root_path, 'uploads')
    PICTURE_ALLOWED_EXTENSIONS = {'jpg', 'png', 'gif'}
    ACTIVITY_ALLOWED_EXTENSIONS = {'gpx', 'zip'}
    TEMPLATES_FOLDER = os.path.join(current_app.root_path, 'email/templates')
github Bogdanp / dramatiq / examples / long_running / example.py View on Github external
import os
import random
import sys
import time

import dramatiq

if os.getenv("REDIS") == "1":
    from dramatiq.brokers.redis import RedisBroker
    broker = RedisBroker()
    dramatiq.set_broker(broker)


def fib(n):
    x, y = 1, 1
    while n > 2:
        x, y = x + y, x
        n -= 1
    return x


@dramatiq.actor(time_limit=86_400_000, max_retries=0)
def long_running(duration):
    deadline = time.monotonic() + duration
    while time.monotonic() < deadline:
        long_running.logger.info("%d seconds remaining.", deadline - time.monotonic())
github Bogdanp / dramatiq / examples / basic / example.py View on Github external
import argparse
import os
import random
import sys

import dramatiq

if os.getenv("REDIS") == "1":
    from dramatiq.brokers.redis import RedisBroker
    broker = RedisBroker()
    dramatiq.set_broker(broker)


@dramatiq.actor
def add(x, y):
    add.logger.info("The sum of %d and %d is %d.", x, y, x + y)


def main(args):
    parser = argparse.ArgumentParser()
    parser.add_argument("count", type=int, help="the number of messages to enqueue")
    args = parser.parse_args()
    for _ in range(args.count):
        add.send(random.randint(0, 1000), random.randint(0, 1000))