|
| 1 | +.. _examples_hybrid_setup: |
| 2 | + |
| 3 | +============== |
| 4 | + hybrid_setup |
| 5 | +============== |
| 6 | + |
| 7 | +:Release: |version| |
| 8 | +:Date: |today| |
| 9 | + |
| 10 | +.. contents:: |
| 11 | + :local: |
| 12 | + :depth: 2 |
| 13 | + |
| 14 | +Description |
| 15 | +=========== |
| 16 | + |
| 17 | +The purpose of this example is to demonstrate a more complex setup with multiple components. |
| 18 | +The example is using two brokers, with the failover feature, a backend and multiple workers of different pools and versions. |
| 19 | +One of the workers is using gevent with the latest Celery release on the default queue, |
| 20 | +while the other is using prefork with Celery 4 and its own queue. |
| 21 | + |
| 22 | +It uses the following workflow to utilize both workers: |
| 23 | + |
| 24 | +.. code-block:: python |
| 25 | +
|
| 26 | + canvas = ( |
| 27 | + group( |
| 28 | + identity.si("Hello, "), |
| 29 | + identity.si("world!"), |
| 30 | + ) |
| 31 | + | noop.s().set(queue="legacy") |
| 32 | + | identity.si("Done!") |
| 33 | + ) |
| 34 | +
|
| 35 | +Highlights |
| 36 | +~~~~~~~~~~ |
| 37 | + |
| 38 | +1. No default components. |
| 39 | +2. Session broker and backend components. |
| 40 | + - Shared between tests, but not between :pypi:`pytest-xdist` sessions. |
| 41 | + - Only the workers are created again for each test case. |
| 42 | +3. Injects tasks and signal handlers modules to all workers. |
| 43 | + |
| 44 | +This example is based on, |
| 45 | + |
| 46 | +- The :ref:`examples_myworker` example. |
| 47 | +- The :ref:`examples_worker_pool` example. |
| 48 | + |
| 49 | +Breakdown |
| 50 | +========= |
| 51 | + |
| 52 | +File Structure |
| 53 | +~~~~~~~~~~~~~~ |
| 54 | + |
| 55 | +The following diagram lists the relevant files in the project. |
| 56 | + |
| 57 | +.. code-block:: text |
| 58 | +
|
| 59 | + hybrid_setup/ |
| 60 | + ├── requirements.txt |
| 61 | + └── tests/ |
| 62 | + ├── conftest.py |
| 63 | + ├── test_hybrid_setup.py |
| 64 | + └── vendors/ |
| 65 | + ├── __init__.py |
| 66 | + ├── memcached.py |
| 67 | + ├── rabbitmq.py |
| 68 | + └── workers/ |
| 69 | + ├── __init__.py |
| 70 | + ├── gevent.Dockerfile |
| 71 | + ├── gevent.py |
| 72 | + ├── legacy.Dockerfile |
| 73 | + ├── legacy.py |
| 74 | + ├── signals.py |
| 75 | + └── tasks.py |
| 76 | +
|
| 77 | +requirements.txt |
| 78 | +~~~~~~~~~~~~~~~~ |
| 79 | + |
| 80 | +Take a look at the requirements file for this example: |
| 81 | + |
| 82 | +.. literalinclude:: ../../../examples/hybrid_setup/requirements.txt |
| 83 | + :language: text |
| 84 | + :caption: examples.hybrid_setup.requirements.txt |
| 85 | + |
| 86 | +Take note the :pypi:`gevent` can be installed independently from the :pypi:`celery` package. |
| 87 | + |
| 88 | +conftest.py |
| 89 | +~~~~~~~~~~~ |
| 90 | + |
| 91 | +The ``conftest.py`` file will be used to aggregate each individual configuration. To understand how it works, |
| 92 | +we'll split the file into three parts. |
| 93 | + |
| 94 | +1. Creating the docker network for the components. |
| 95 | +2. Configuring the broker, backend and workers for the setup. |
| 96 | +3. Injecting the tasks and signal handlers modules. |
| 97 | + |
| 98 | +.. literalinclude:: ../../../examples/hybrid_setup/tests/conftest.py |
| 99 | + :language: python |
| 100 | + :caption: examples.hybrid_setup.tests.conftest.py |
| 101 | + :start-after: # ---------------------------- |
| 102 | + |
| 103 | +test_hybrid_setup.py |
| 104 | +~~~~~~~~~~~~~~~~~~~~ |
| 105 | + |
| 106 | +Every test case that uses the :func:`celery_setup <pytest_celery.fixtures.setup.celery_setup>` fixture will run |
| 107 | +its scenario on the setup that was configured in the ``conftest.py`` file. |
| 108 | + |
| 109 | +For this example, we have the following test cases. |
| 110 | + |
| 111 | +.. literalinclude:: ../../../examples/hybrid_setup/tests/test_hybrid_setup.py |
| 112 | + :language: python |
| 113 | + :caption: examples.hybrid_setup.tests.test_hybrid_setup.py |
| 114 | + :start-after: TestHybridSetupExample |
| 115 | + |
| 116 | +.. tip:: |
| 117 | + |
| 118 | + The components themselves can be used in the test case to easily access their attributes and methods, like shown |
| 119 | + in the failover test case. When used without the :func:`celery_setup <pytest_celery.fixtures.setup.celery_setup>` |
| 120 | + fixture, the components will run independently and might not be aware of each other. |
| 121 | + |
| 122 | +rabbitmq.py and memcached.py |
| 123 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 124 | + |
| 125 | +The brokers and result backend are defined as independent components that are being configured into the setup |
| 126 | +using the ``conftest.py`` file. They add **session scope** fixtures and integrate using the matching :ref:`node class <test-nodes>`. |
| 127 | + |
| 128 | +Main | Failover Brokers |
| 129 | +----------------------- |
| 130 | + |
| 131 | +.. literalinclude:: ../../../examples/hybrid_setup/tests/vendors/rabbitmq.py |
| 132 | + :language: python |
| 133 | + :caption: examples.hybrid_setup.tests.vendors.rabbitmq.py |
| 134 | + |
| 135 | +Result Backend |
| 136 | +-------------- |
| 137 | + |
| 138 | +.. literalinclude:: ../../../examples/hybrid_setup/tests/vendors/memcached.py |
| 139 | + :language: python |
| 140 | + :caption: examples.hybrid_setup.tests.vendors.memcached.py |
| 141 | + |
| 142 | +gevent.py and gevent.Dockerfile |
| 143 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 144 | + |
| 145 | +These files are taken from the :ref:`test_gevent_pool` example with one simple change. |
| 146 | + |
| 147 | +.. code-block:: python |
| 148 | +
|
| 149 | + RUN pip install "celery[gevent]" "pytest-celery[all]==1.0.0b4" |
| 150 | +
|
| 151 | +The Dockerfile doesn't use the requirements file, but instead installs the packages directly. |
| 152 | + |
| 153 | +.. literalinclude:: ../../../examples/hybrid_setup/tests/vendors/workers/gevent.Dockerfile |
| 154 | + :language: docker |
| 155 | + :caption: examples.hybrid_setup.tests.vendors.workers.gevent.Dockerfile |
| 156 | + |
| 157 | +.. note:: |
| 158 | + |
| 159 | + The :ref:`test_gevent_pool` example defines everything in the test file. Here we use the ``gevent.py`` file. |
| 160 | + |
| 161 | +.. literalinclude:: ../../../examples/hybrid_setup/tests/vendors/workers/gevent.py |
| 162 | + :language: python |
| 163 | + :caption: examples.hybrid_setup.tests.vendors.workers.gevent.py |
| 164 | + |
| 165 | +legacy.py and legacy.Dockerfile |
| 166 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 167 | + |
| 168 | +The "legacy" worker is basically Celery 4 worker with the prefork pool. Very similar to the gevent worker, |
| 169 | +we add a new Dockerfile and worker module. |
| 170 | + |
| 171 | +.. literalinclude:: ../../../examples/hybrid_setup/tests/vendors/workers/legacy.Dockerfile |
| 172 | + :language: docker |
| 173 | + :caption: examples.hybrid_setup.tests.vendors.workers.legacy.Dockerfile |
| 174 | + |
| 175 | +.. literalinclude:: ../../../examples/hybrid_setup/tests/vendors/workers/legacy.py |
| 176 | + :language: python |
| 177 | + :caption: examples.hybrid_setup.tests.vendors.workers.legacy.py |
| 178 | + |
| 179 | +.. tip:: |
| 180 | + |
| 181 | + Check all of the configurations above again and notice the usage of ``hybrid_setup_example_network``. |
| 182 | + See how both session and non-session fixtures are sharing the same session docker network. |
| 183 | + |
| 184 | +tasks.py and signals.py |
| 185 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 186 | + |
| 187 | +The tasks and signal handlers are being injected into the workers using the ``conftest.py`` file, |
| 188 | +according to the documentation: |
| 189 | + |
| 190 | +1. :ref:`injecting-tasks`. |
| 191 | +2. :ref:`injecting-signals-handlers`. |
| 192 | + |
| 193 | +The files themselves are very simple, |
| 194 | + |
| 195 | +.. literalinclude:: ../../../examples/hybrid_setup/tests/vendors/workers/tasks.py |
| 196 | + :language: python |
| 197 | + :caption: examples.hybrid_setup.tests.vendors.workers.tasks.py |
| 198 | + |
| 199 | +.. literalinclude:: ../../../examples/hybrid_setup/tests/vendors/workers/signals.py |
| 200 | + :language: python |
| 201 | + :caption: examples.hybrid_setup.tests.vendors.workers.signals.py |
| 202 | + |
| 203 | +And again, from ``confest.py``, |
| 204 | + |
| 205 | +.. code-block:: python |
| 206 | +
|
| 207 | + @pytest.fixture |
| 208 | + def default_worker_tasks(default_worker_tasks: set) -> set: |
| 209 | + from tests.vendors.workers import tasks |
| 210 | +
|
| 211 | + default_worker_tasks.add(tasks) |
| 212 | + return default_worker_tasks |
| 213 | +
|
| 214 | +
|
| 215 | + @pytest.fixture |
| 216 | + def default_worker_signals(default_worker_signals: set) -> set: |
| 217 | + from tests.vendors.workers import signals |
| 218 | +
|
| 219 | + default_worker_signals.add(signals) |
| 220 | + return default_worker_signals |
| 221 | +
|
| 222 | +.. note:: |
| 223 | + |
| 224 | + The tasks and signals are being injected into the workers that use the default volume with: |
| 225 | + |
| 226 | + .. code-block:: python |
| 227 | +
|
| 228 | + volumes={"{default_worker_volume.name}": defaults.DEFAULT_WORKER_VOLUME}, |
| 229 | +
|
| 230 | + Both our workers are using the default volume, so we only need to inject the tasks and signals once. |
0 commit comments