How to use rq - 10 common examples

To help you get started, we’ve selected a few rq 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 getsentry / sentry-python / tests / integrations / rq / View on Github external
def test_transport_shutdown(sentry_init, capture_events_forksafe):

    events = capture_events_forksafe()

    queue = rq.Queue(connection=FakeStrictRedis())
    worker = rq.Worker([queue], connection=queue.connection)

    queue.enqueue(crashing_job, foo=42)

    event = events.read_event()

    (exception,) = event["exception"]["values"]
    assert exception["type"] == "ZeroDivisionError"
github rq / rq / tests / View on Github external
from rq.compat import as_text
from rq.defaults import DEFAULT_FAILURE_TTL
from rq.exceptions import InvalidJobOperation
from rq.job import Job, JobStatus, requeue_job
from rq.queue import Queue
from rq.utils import current_timestamp
from rq.worker import Worker
from rq.registry import (clean_registries, DeferredJobRegistry,
                         FailedJobRegistry, FinishedJobRegistry,

from tests import RQTestCase
from tests.fixtures import div_by_zero, say_hello

class CustomJob(Job):
    """A custom job class just to test it"""

class TestRegistry(RQTestCase):

    def setUp(self):
        super(TestRegistry, self).setUp()
        self.registry = StartedJobRegistry(connection=self.testconn)

    def test_init(self):
        """Registry can be instantiated with queue or name/Redis connection"""
        queue = Queue('foo', connection=self.testconn)
        registry = StartedJobRegistry(queue=queue)
        self.assertEqual(registry.connection, queue.connection)
github rq / rq / tests / View on Github external
def test_all_custom_job(self):
        class CustomJob(Job):

        q = Queue('all-queue')
        queues = Queue.all(job_class=CustomJob)
        self.assertEqual(len(queues), 1)
        self.assertIs(queues[0].job_class, CustomJob)
github rq / rq / tests / View on Github external
def test_enqueue_scheduled_jobs(self):
        """Scheduler can enqueue scheduled jobs"""
        queue = Queue(connection=self.testconn)
        registry = ScheduledJobRegistry(queue=queue)
        job = Job.create('myfunc', connection=self.testconn)
        registry.schedule(job, datetime(2019, 1, 1, tzinfo=utc))
        scheduler = RQScheduler([queue], connection=self.testconn)
        self.assertEqual(len(queue), 1)

        # After job is scheduled, registry should be empty
        self.assertEqual(len(registry), 0)

        # Jobs scheduled in the far future should not be affected
        registry.schedule(job, datetime(2100, 1, 1, tzinfo=utc))
        self.assertEqual(len(queue), 1)
github rq / rq / tests / View on Github external
def test_enqueue_job_with_invalid_dependency(self):
        """Enqueuing a job fails, if the dependency does not exist at all."""
        parent_job = Job.create(func=say_hello)
        # without save() the job is not visible to others

        q = Queue()
        with self.assertRaises(NoSuchJobError):
            q.enqueue_call(say_hello, depends_on=parent_job)

        with self.assertRaises(NoSuchJobError):

        self.assertEqual(q.job_ids, [])
github mgk / rq-retry / tests / View on Github external
def test_failed_job_max_tries_2__retry_once_then_move_to_dlq():
    q = Queue()
    q2 = Queue('not_used')
    failed_q = get_failed_queue()
    dlq = Queue('dead_letter_queue')

    w = Worker([q])

    # Here the RetryWorker not listening on an active queue: it will not
    # run any jobs, just look to requeue failed jobs.
    rw = RetryWorker([q2], retry_config=dict(
        max_tries=2, maint_interval=0, delays=[]))

    # run job that will fail
    job = q.enqueue(error_fun)
    assert q.count == 0
    assert get_failed_queue().count == 1

    # run retry worker
github rq / rq / tests / View on Github external
def test_find_by_key(self):
        """Worker.find_by_key restores queues, state and job_id."""
        queues = [Queue('foo'), Queue('bar')]
        w = Worker(queues)
        worker = Worker.find_by_key(w.key)
        self.assertEqual(worker.queues, queues)
        self.assertEqual(worker.get_state(), WorkerStatus.STARTED)
        self.assertEqual(worker._job_id, None)
        self.assertTrue(worker.key in Worker.all_keys(worker.connection))
        self.assertEqual(worker.version, VERSION)

        # If worker is gone, its keys should also be removed
        self.assertFalse(worker.key in Worker.all_keys(worker.connection))

        self.assertRaises(ValueError, Worker.find_by_key, 'foo')
github rq / rq / tests / View on Github external
# With iterable of byte strings
            w = Worker(iter([b'foo', b'bar']))
            self.assertEqual(w.queues[0].name, 'foo')
            self.assertEqual(w.queues[1].name, 'bar')

        # With single Queue
        w = Worker(Queue('foo'))
        self.assertEqual(w.queues[0].name, 'foo')

        # With iterable of Queues
        w = Worker(iter([Queue('foo'), Queue('bar')]))
        self.assertEqual(w.queues[0].name, 'foo')
        self.assertEqual(w.queues[1].name, 'bar')

        # With list of Queues
        w = Worker([Queue('foo'), Queue('bar')])
        self.assertEqual(w.queues[0].name, 'foo')
        self.assertEqual(w.queues[1].name, 'bar')
github rq / rq / tests / View on Github external
def test_working_worker_warm_shutdown(self):
        """worker with an ongoing job receiving single SIGTERM signal, allowing job to finish then shutting down"""
        fooq = Queue('foo')
        w = Worker(fooq)

        sentinel_file = '/tmp/.rq_sentinel_warm'
        fooq.enqueue(create_file_after_timeout, sentinel_file, 2)
        p = Process(target=kill_worker, args=(os.getpid(), False))


        self.assertEqual(type(w.shutdown_requested_date).__name__, 'datetime')
github rq / rq / tests / View on Github external
def test_self_modification_persistence(self):
        """Make sure that any meta modification done by
        the job itself persists completely through the
        queue/worker/job stack."""
        q = Queue()
        # Also make sure that previously existing metadata
        # persists properly
        job = q.enqueue(modify_self, meta={'foo': 'bar', 'baz': 42},
                        args=[{'baz': 10, 'newinfo': 'waka'}])

        w = Worker([q])

        job_check = Job.fetch(
        self.assertEqual(job_check.meta['foo'], 'bar')
        self.assertEqual(job_check.meta['baz'], 10)
        self.assertEqual(job_check.meta['newinfo'], 'waka')