Coverage for faststream/nats/subscriber/usecases/object_storage_subscriber.py: 88%

69 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-17 21:20 +0000

1from collections.abc import AsyncIterator, Iterable 

2from contextlib import suppress 

3from typing import TYPE_CHECKING, Any, Optional, cast 

4 

5import anyio 

6from nats.errors import TimeoutError 

7from nats.js.api import ObjectInfo 

8from typing_extensions import override 

9 

10from faststream._internal.endpoint.subscriber.mixins import TasksMixin 

11from faststream._internal.endpoint.utils import process_msg 

12from faststream.nats.parser import ( 

13 ObjParser, 

14) 

15from faststream.nats.subscriber.adapters import ( 

16 UnsubscribeAdapter, 

17) 

18 

19from .basic import LogicSubscriber 

20 

21if TYPE_CHECKING: 

22 from nats.js.object_store import ObjectStore 

23 

24 from faststream._internal.endpoint.publisher import PublisherProto 

25 from faststream._internal.endpoint.subscriber import SubscriberSpecification 

26 from faststream._internal.endpoint.subscriber.call_item import CallsCollection 

27 from faststream.message import StreamMessage 

28 from faststream.nats.message import NatsObjMessage 

29 from faststream.nats.schemas import ObjWatch 

30 from faststream.nats.subscriber.config import NatsSubscriberConfig 

31 

32 

33OBJECT_STORAGE_CONTEXT_KEY = "__object_storage" 

34 

35 

36class ObjStoreWatchSubscriber( 

37 TasksMixin, 

38 LogicSubscriber[ObjectInfo], 

39): 

40 __slots__ = () 

41 

42 subscription: Optional["UnsubscribeAdapter[ObjectStore.ObjectWatcher]"] 

43 _fetch_sub: UnsubscribeAdapter["ObjectStore.ObjectWatcher"] | None 

44 

45 def __init__( 

46 self, 

47 config: "NatsSubscriberConfig", 

48 specification: "SubscriberSpecification[Any, Any]", 

49 calls: "CallsCollection[ObjectInfo]", 

50 *, 

51 obj_watch: "ObjWatch", 

52 ) -> None: 

53 parser = ObjParser(pattern="") 

54 config.parser = parser.parse_message 

55 config.decoder = parser.decode_message 

56 super().__init__(config, specification, calls) 

57 

58 self.obj_watch = obj_watch 

59 self.obj_watch_conn = None 

60 

61 @override 

62 async def get_one(self, *, timeout: float = 5) -> Optional["NatsObjMessage"]: 

63 assert not self.calls, ( 

64 "You can't use `get_one` method if subscriber has registered handlers." 

65 ) 

66 

67 if not self._fetch_sub: 67 ↛ 82line 67 didn't jump to line 82 because the condition on line 67 was always true

68 self.bucket = await self._outer_config.os_declarer.create_object_store( 

69 bucket=self.subject.template, 

70 declare=self.obj_watch.declare, 

71 ) 

72 

73 obj_watch = await self.bucket.watch( 

74 ignore_deletes=self.obj_watch.ignore_deletes, 

75 include_history=self.obj_watch.include_history, 

76 meta_only=self.obj_watch.meta_only, 

77 ) 

78 fetch_sub = self._fetch_sub = UnsubscribeAdapter["ObjectStore.ObjectWatcher"]( 

79 obj_watch 

80 ) 

81 else: 

82 fetch_sub = self._fetch_sub 

83 

84 msg = None 

85 sleep_interval = timeout / 10 

86 with anyio.move_on_after(timeout): 

87 while ( # ruff: ignore[async-busy-wait] 

88 msg := await fetch_sub.obj.updates(timeout) # type: ignore[no-untyped-call] 

89 ) is None: 

90 await anyio.sleep(sleep_interval) 

91 

92 context = self._outer_config.context 

93 async_parser, async_decoder = self._get_parser_and_decoder() 

94 

95 return cast( 

96 "NatsObjMessage", 

97 await process_msg( 

98 msg=msg, 

99 middlewares=(m(msg, context=context) for m in self._broker_middlewares), 

100 parser=async_parser, 

101 decoder=async_decoder, 

102 ), 

103 ) 

104 

105 @override 

106 async def __aiter__(self) -> AsyncIterator["NatsObjMessage"]: 

107 assert not self.calls, ( 

108 "You can't use iterator if subscriber has registered handlers." 

109 ) 

110 

111 if not self._fetch_sub: 111 ↛ 126line 111 didn't jump to line 126 because the condition on line 111 was always true

112 self.bucket = await self._outer_config.os_declarer.create_object_store( 

113 bucket=self.subject.template, 

114 declare=self.obj_watch.declare, 

115 ) 

116 

117 obj_watch = await self.bucket.watch( 

118 ignore_deletes=self.obj_watch.ignore_deletes, 

119 include_history=self.obj_watch.include_history, 

120 meta_only=self.obj_watch.meta_only, 

121 ) 

122 fetch_sub = self._fetch_sub = UnsubscribeAdapter["ObjectStore.ObjectWatcher"]( 

123 obj_watch 

124 ) 

125 else: 

126 fetch_sub = self._fetch_sub 

127 

128 timeout = 5 

129 sleep_interval = timeout / 10 

130 

131 context = self._outer_config.context 

132 async_parser, async_decoder = self._get_parser_and_decoder() 

133 

134 while True: 

135 msg = None 

136 with anyio.move_on_after(timeout): 

137 while ( # ruff: ignore[async-busy-wait] 

138 msg := await fetch_sub.obj.updates(timeout) # type: ignore[no-untyped-call] 

139 ) is None: 

140 await anyio.sleep(sleep_interval) 

141 

142 if msg is None: 142 ↛ 143line 142 didn't jump to line 143 because the condition on line 142 was never true

143 continue 

144 

145 yield cast( 

146 "NatsObjMessage", 

147 await process_msg( 

148 msg=msg, 

149 middlewares=( 

150 m(msg, context=context) for m in self._broker_middlewares 

151 ), 

152 parser=async_parser, 

153 decoder=async_decoder, 

154 ), 

155 ) 

156 

157 @override 

158 async def _create_subscription(self) -> None: 

159 if self.subscription: 159 ↛ 160line 159 didn't jump to line 160 because the condition on line 159 was never true

160 return 

161 

162 self.bucket = await self._outer_config.os_declarer.create_object_store( 

163 bucket=self.subject.template, 

164 declare=self.obj_watch.declare, 

165 ) 

166 

167 self.add_task(self.__consume_watch) 

168 

169 async def __consume_watch(self) -> None: 

170 # Should be created inside task to avoid nats-py lock 

171 obj_watch = await self.bucket.watch( 

172 ignore_deletes=self.obj_watch.ignore_deletes, 

173 include_history=self.obj_watch.include_history, 

174 meta_only=self.obj_watch.meta_only, 

175 ) 

176 

177 self.subscription = UnsubscribeAdapter["ObjectStore.ObjectWatcher"](obj_watch) 

178 

179 context = self._outer_config.context 

180 

181 while self.running: 181 ↛ exitline 181 didn't return from function '__consume_watch' because the condition on line 181 was always true

182 with suppress(TimeoutError): 

183 message = cast( 

184 "ObjectInfo | None", 

185 await obj_watch.updates(self.obj_watch.timeout), # type: ignore[no-untyped-call] 

186 ) 

187 

188 if message: 

189 with context.scope(OBJECT_STORAGE_CONTEXT_KEY, self.bucket): 

190 await self.consume(message) 

191 

192 @override 

193 def _make_response_publisher( 

194 self, 

195 message: "StreamMessage[ObjectInfo]", 

196 ) -> Iterable["PublisherProto"]: 

197 """Create Publisher objects to use it as one of `publishers` in `self.consume` scope. 

198 

199 Args: 

200 message: Message requiring reply 

201 """ 

202 return () 

203 

204 def get_log_context( 

205 self, 

206 message: Optional["StreamMessage[ObjectInfo]"], 

207 ) -> dict[str, str]: 

208 """Log context factory using in `self.consume` scope. 

209 

210 Args: 

211 message: Message which we are building context for 

212 """ 

213 return self.build_log_context( 

214 message=message, 

215 subject=self.subject.template, 

216 )