Skip to content

LogicSubscriber

faststream.nats.subscriber.LogicSubscriber #

LogicSubscriber(
    config: NatsSubscriberConfig,
    specification: SubscriberSpecification[Any, Any],
    calls: CallsCollection[MsgType],
)

Bases: SubscriberUsecase[MsgType]

Basic class for all NATS Subscriber types (KeyValue, ObjectStorage, Core & JetStream).

Unslotted on purpose: ConcurrentCoreSubscriber mixes ConcurrentMixin into this, and TasksMixin under it already extends SubscriberUsecase's lay-out. NATS is the one broker whose subscribers do not inherit TasksMixin in a straight line.

Source code in faststream/nats/subscriber/usecases/basic.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def __init__(
    self,
    config: "NatsSubscriberConfig",
    specification: "SubscriberSpecification[Any, Any]",
    calls: "CallsCollection[MsgType]",
) -> None:
    super().__init__(config, specification, calls)

    self._subject = config.subject
    self.config = config.sub_config

    self.extra_options = config.extra_options or {}

    self._fetch_sub = None
    self.subscription = None

lock instance-attribute #

lock: AbstractContextManager[Any] = FakeContext()

extra_watcher_options instance-attribute #

extra_watcher_options: dict[str, Any] = {}

graceful_timeout instance-attribute #

graceful_timeout: float | None

calls instance-attribute #

calls = calls

specification instance-attribute #

specification = specification

ack_policy instance-attribute #

ack_policy = config.ack_policy

running instance-attribute #

running = False

subscription instance-attribute #

subscription: Unsubscriptable | None = None

config instance-attribute #

config = config.sub_config

extra_options instance-attribute #

extra_options = config.extra_options or {}

subject property #

subject: Address

The subject this Subscriber was declared with, and its Broker address.

filter_subjects property #

filter_subjects: list[str]

connection property #

connection: Client

jetstream property #

jetstream: JetStreamContext

add_call #

add_call(
    *,
    parser_: Optional[CustomCallable],
    decoder_: Optional[CustomCallable],
    dependencies_: Sequence[Dependant],
    codec_: Optional[CodecProto] = None,
) -> Self
Source code in faststream/_internal/endpoint/subscriber/usecase.py
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
def add_call(
    self,
    *,
    parser_: Optional["CustomCallable"],
    decoder_: Optional["CustomCallable"],
    dependencies_: Sequence["Dependant"],
    codec_: Optional["CodecProto"] = None,
) -> Self:
    self._call_options = _CallOptions(
        parser=parser_,
        decoder=decoder_,
        dependencies=dependencies_,
        codec=codec_,
    )
    return self

consume async #

consume(msg: MsgType) -> Any

Consume a message asynchronously.

Source code in faststream/_internal/endpoint/subscriber/usecase.py
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
async def consume(self, msg: MsgType) -> Any:
    """Consume a message asynchronously."""
    if not self.running:
        return None

    try:
        return await self.process_message(msg)

    except StopConsume:
        # Stop handler at StopConsume exception
        await self.stop()

    except SystemExit:
        # Stop handler at `exit()` call
        await self.stop()

        if app := self._outer_config.context.get("app"):
            app.exit()

    except Exception:  # nosec B110  # noqa: S110
        # All other exceptions were logged by CriticalLogMiddleware
        pass

process_message async #

process_message(msg: MsgType) -> Response

Execute all message processing stages.

Source code in faststream/_internal/endpoint/subscriber/usecase.py
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
async def process_message(self, msg: MsgType) -> "Response":
    """Execute all message processing stages."""
    context = self._outer_config.context
    logger_state = self._outer_config.logger

    async with AsyncExitStack() as stack:
        stack.enter_context(self.lock)

        # Enter context before middlewares
        stack.enter_context(
            context.scopes(
                (
                    ("handler_", self),
                    ("logger", logger_state.logger.logger),
                    *self._outer_config.extra_context.items(),
                ),
            ),
        )

        # enter all middlewares
        middlewares: list[BaseMiddleware] = []
        for base_m in self.__build__middlewares_stack():
            middleware = base_m(msg, context=context)
            middlewares.append(middleware)
            await middleware.__aenter__()

        cache: dict[Any, Any] = {}
        parsing_error: Exception | None = None
        for h in self.calls:
            try:
                message = await h.is_suitable(msg, cache)
            except Exception as e:
                parsing_error = e
                break

            if message is not None:
                stack.enter_context(
                    context.scopes(
                        (
                            ("log_context", self.get_log_context(message)),
                            ("message", message),
                        ),
                    ),
                )

                # Middlewares should be exited before scope release
                for m in middlewares:
                    stack.push_async_exit(m.__aexit__)

                result_msg = ensure_response(
                    await h.call(
                        message=message,
                        # consumer middlewares
                        _extra_middlewares=(
                            m.consume_scope for m in middlewares[::-1]
                        ),
                    ),
                )

                if not result_msg.correlation_id:
                    result_msg.correlation_id = message.correlation_id

                for p in chain(
                    self.__get_response_publisher(message),
                    h.handler._publishers,
                ):
                    await p._publish(
                        result_msg.as_publish_command(),
                        _extra_middlewares=(
                            m.publish_scope for m in middlewares[::-1]
                        ),
                    )

                # Return data for tests
                return result_msg

        # Suitable handler was not found or
        # parsing/decoding exception occurred
        for m in middlewares:
            stack.push_async_exit(m.__aexit__)

        # Reraise it to catch in tests
        if parsing_error:
            raise parsing_error

        error_msg = f"There is no suitable handler for {msg=}"
        raise SubscriberNotFound(error_msg)

    # An error was raised and processed by some middleware
    return ensure_response(None)

get_one abstractmethod async #

get_one(
    *, timeout: float = 5
) -> Optional[StreamMessage[MsgType]]
Source code in faststream/_internal/endpoint/subscriber/usecase.py
483
484
485
@abstractmethod
async def get_one(self, *, timeout: float = 5) -> Optional["StreamMessage[MsgType]"]:
    raise NotImplementedError

get_log_context #

get_log_context(
    message: Optional[StreamMessage[MsgType]],
) -> dict[str, str]

Generate log context.

Source code in faststream/_internal/endpoint/subscriber/usecase.py
495
496
497
498
499
500
501
502
def get_log_context(  # noqa: PLR6301
    self,
    message: Optional["StreamMessage[MsgType]"],
) -> dict[str, str]:
    """Generate log context."""
    return {
        "message_id": getattr(message, "message_id", ""),
    }

schema #

schema() -> dict[str, SubscriberSpec]
Source code in faststream/_internal/endpoint/subscriber/usecase.py
518
519
520
def schema(self) -> dict[str, "SubscriberSpec"]:
    self._build_fastdepends_model()
    return self.specification.get_schema()

start async #

start() -> None

Create NATS subscription and start consume tasks.

Source code in faststream/nats/subscriber/usecases/basic.py
78
79
80
81
82
83
84
85
async def start(self) -> None:
    """Create NATS subscription and start consume tasks."""
    await super().start()

    if self.calls:
        await self._create_subscription()

    self._post_start()

stop async #

stop() -> None

Clean up handler subscription, cancel consume task in graceful mode.

Source code in faststream/nats/subscriber/usecases/basic.py
87
88
89
90
91
92
93
94
95
96
97
async def stop(self) -> None:
    """Clean up handler subscription, cancel consume task in graceful mode."""
    await super().stop()

    if self.subscription is not None:
        await self.subscription.unsubscribe()
        self.subscription = None

    if self._fetch_sub is not None:
        await self._fetch_sub.unsubscribe()
        self._fetch_sub = None

build_log_context staticmethod #

build_log_context(
    message: Optional[StreamMessage[MsgType]],
    subject: str,
    *,
    queue: str = "",
    stream: str = "",
) -> dict[str, str]

Static method to build log context out of self.consume scope.

Source code in faststream/nats/subscriber/usecases/basic.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
@staticmethod
def build_log_context(
    message: Optional["StreamMessage[MsgType]"],
    subject: str,
    *,
    queue: str = "",
    stream: str = "",
) -> dict[str, str]:
    """Static method to build log context out of `self.consume` scope."""
    return {
        "subject": subject,
        "queue": queue,
        "stream": stream,
        "message_id": getattr(message, "message_id", ""),
    }