API Reference
Consumer
The primary base classes for building message consumers.
Consumer
rejected.consumer.Consumer
Bases: _Consumer
Backward-compatible consumer for rejected 3.x.
Processes one message at a time (locked). Message properties are
accessible via self.body, self.content_type, etc.
Override prepare(), process(), finish().
Source code in rejected/consumer.py
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 | |
Attributes
body
property
app_id
property
content_encoding
property
content_type
property
exchange
property
expiration
property
headers
property
message_id
property
message_type
property
priority
property
properties
property
redelivered
property
reply_to
property
returned
property
routing_key
property
timestamp
property
user_id
property
Functions
process()
async
prepare()
async
finish()
async
on_finish()
async
stats_add_duration(key, duration, ctx=None)
stats_incr(key, value=1, ctx=None)
stats_set_tag(key, value=1, ctx=None)
stats_set_value(key, value=1, ctx=None)
stats_track_duration(key, ctx=None)
Source code in rejected/consumer.py
FunctionalConsumer
rejected.consumer.FunctionalConsumer
Bases: _Consumer
Concurrent consumer that receives a ProcessingContext.
No lock — multiple messages may be processed concurrently.
Override prepare(ctx), process(ctx), finish(ctx).
Source code in rejected/consumer.py
Functions
process(ctx)
async
prepare(ctx)
async
Exceptions
rejected.exceptions.ConsumerException
Bases: RejectedException
May be called when processing a message to indicate a problem that the Consumer may be experiencing that should cause it to stop.
:param str value: An optional value used in string representation :param str metric: An optional value for auto-instrumentation
Source code in rejected/exceptions.py
rejected.exceptions.MessageException
Bases: RejectedException
Invoke when a message should be rejected and not re-queued, but not due to a processing error that should cause the consumer to stop.
:param str value: An optional value used in string representation :param str metric: An optional value for auto-instrumentation
Source code in rejected/exceptions.py
rejected.exceptions.ProcessingException
Bases: RejectedException
Invoke when a message should be rejected and not re-queued, but not due to a processing error that should cause the consumer to stop. This should be used for when you want to reject a message which will be republished to a retry queue, without anything being stated about the exception.
:param str value: An optional value used in string representation :param str metric: An optional value for auto-instrumentation
Source code in rejected/exceptions.py
rejected.exceptions.RejectedException
Bases: Exception
Base exception for :py:class:~rejected.consumer.Consumer related
exceptions.
If provided, the metric will be used to automatically record exception
metric counts using the path
[prefix].[consumer-name].exceptions.[exception-type].[metric].
Positional and keyword arguments are used to format the value that is passed in when providing the string value of the exception.
:param str value: An optional value used in string representation :param str metric: An optional value for auto-instrumentation
.. versionadded:: 3.19.0
Source code in rejected/exceptions.py
Models
Message
rejected.models.Message
Bases: BaseModel
The deserialized message to be processed by the consumer.
Source code in rejected/models.py
ProcessingContext
rejected.models.ProcessingContext
Bases: BaseModel
Tracks a single in-flight message being processed.
Source code in rejected/models.py
Result
rejected.models.Result
Bases: IntEnum
Result codes returned by Consumer.execute() to indicate how the message should be handled by the process.
Source code in rejected/models.py
Testing
rejected.testing.AsyncTestCase
Bases: IsolatedAsyncioTestCase
:class:unittest.IsolatedAsyncioTestCase subclass for testing
:class:~rejected.consumer.Consumer and
:class:~rejected.consumer.FunctionalConsumer classes.
Source code in rejected/testing.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
Attributes
published_messages
property
Return a list of :class:PublishedMessage extracted from
all calls to :meth:pika.channel.Channel.basic_publish during
the test.
measurement
property
Return the Measurement for the last processed message.
Functions
get_consumer()
get_settings()
create_context(message_body=None, content_type='application/json', message_type=None, properties=None, exchange='rejected', routing_key='test')
Create a :class:~rejected.models.ProcessingContext for
testing.
The body is stored as raw bytes on the message, matching what
RabbitMQ delivers. Non-bytes/str bodies are serialized via the
:class:~rejected.codecs.Codec. Use :meth:process_message
to also run the codec decode step before the consumer sees the
message (matching production behavior).
Source code in rejected/testing.py
process_message(message_body=None, content_type='application/json', message_type=None, properties=None, exchange='rejected', routing_key='routing-key')
async
Process a message as if it were being delivered by RabbitMQ.
Builds a :class:~rejected.models.ProcessingContext and
passes it through the consumer's execute method.
If an exception is not raised, returns the
:class:~rejected.measurement.Measurement collected during
processing.
:raises: :exc:rejected.exceptions.ConsumerException
:raises: :exc:rejected.exceptions.MessageException
:raises: :exc:rejected.exceptions.ProcessingException
Source code in rejected/testing.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
rejected.testing.PublishedMessage
Contains information about messages published during a test.
:param str exchange: The exchange the message was published to :param str routing_key: The routing key used :param pika.spec.BasicProperties properties: AMQP message properties :param bytes body: AMQP message body
.. versionadded:: 3.18.9
Source code in rejected/testing.py
Measurement
rejected.measurement.Measurement
Per-message instrumentation collector.
Accumulates counters, durations, tags, and values during message
processing, then submitted to statsd and/or Prometheus by the
:class:~rejected.process.Process.
.. versionadded:: 3.13.0
Source code in rejected/measurement.py
Functions
add_duration(key, value)
Add a duration for the specified key.
:param key: The value name :param value: The value
.. versionadded:: 3.19.0
Source code in rejected/measurement.py
decr(key, value=1)
Decrement a counter.
:param key: The key to decrement :param value: The value to decrement by
incr(key, value=1)
Increment a counter.
:param key: The key to increment :param value: The value to increment by
set_tag(key, value)
Set a tag for metrics submission.
:param key: The tag name :param value: The tag value
set_value(key, value)
track_duration(key)
Context manager that records the duration of the wrapped block.
:param key: The timing name
Source code in rejected/measurement.py
Mixins
rejected.mixins.GarbageCollectorMixin
Consumer mixin to periodically call gc.collect in the
:meth:on_finish method.
By default, gc.collect is invoked every 10,000 messages.
To configure frequency of collection, include a
gc_collection_frequency setting in the consumer configuration.
Source code in rejected/mixins.py
Attributes
collection_cycle
property
writable
Call :func:gc.collect every this many messages.
Functions
on_finish()
async
Used to initiate the garbage collection