prosody 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.cargo/config.toml +2 -0
- data/.release-please-manifest.json +3 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/.standard.yml +9 -0
- data/.taplo.toml +6 -0
- data/ARCHITECTURE.md +591 -0
- data/CHANGELOG.md +92 -0
- data/Cargo.lock +3513 -0
- data/Cargo.toml +77 -0
- data/LICENSE +21 -0
- data/Makefile +36 -0
- data/README.md +946 -0
- data/Rakefile +26 -0
- data/ext/prosody/Cargo.toml +38 -0
- data/ext/prosody/extconf.rb +6 -0
- data/ext/prosody/src/admin.rs +171 -0
- data/ext/prosody/src/bridge/callback.rs +60 -0
- data/ext/prosody/src/bridge/mod.rs +332 -0
- data/ext/prosody/src/client/config.rs +819 -0
- data/ext/prosody/src/client/mod.rs +379 -0
- data/ext/prosody/src/gvl.rs +149 -0
- data/ext/prosody/src/handler/context.rs +436 -0
- data/ext/prosody/src/handler/message.rs +144 -0
- data/ext/prosody/src/handler/mod.rs +338 -0
- data/ext/prosody/src/handler/trigger.rs +93 -0
- data/ext/prosody/src/lib.rs +82 -0
- data/ext/prosody/src/logging.rs +353 -0
- data/ext/prosody/src/scheduler/cancellation.rs +67 -0
- data/ext/prosody/src/scheduler/handle.rs +50 -0
- data/ext/prosody/src/scheduler/mod.rs +169 -0
- data/ext/prosody/src/scheduler/processor.rs +166 -0
- data/ext/prosody/src/scheduler/result.rs +197 -0
- data/ext/prosody/src/tracing_util.rs +56 -0
- data/ext/prosody/src/util.rs +219 -0
- data/lib/prosody/configuration.rb +333 -0
- data/lib/prosody/handler.rb +177 -0
- data/lib/prosody/native_stubs.rb +417 -0
- data/lib/prosody/processor.rb +321 -0
- data/lib/prosody/sentry.rb +36 -0
- data/lib/prosody/version.rb +10 -0
- data/lib/prosody.rb +42 -0
- data/release-please-config.json +10 -0
- data/sig/configuration.rbs +252 -0
- data/sig/handler.rbs +79 -0
- data/sig/processor.rbs +100 -0
- data/sig/prosody.rbs +171 -0
- data/sig/version.rbs +9 -0
- metadata +193 -0
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Prosody
|
|
4
|
+
# = Native Interface Stubs
|
|
5
|
+
#
|
|
6
|
+
# This file contains stub definitions for native methods implemented in the
|
|
7
|
+
# Prosody Rust extension. These stubs provide documentation and method
|
|
8
|
+
# signatures for Ruby tooling like editors and documentation generators,
|
|
9
|
+
# but the actual implementations are in the Rust extension.
|
|
10
|
+
#
|
|
11
|
+
# == Implementation Notes
|
|
12
|
+
#
|
|
13
|
+
# The actual implementations of these methods are in the Rust extension at:
|
|
14
|
+
# ext/prosody/src/
|
|
15
|
+
|
|
16
|
+
# Wrapper for dynamically-typed results returned from async operations.
|
|
17
|
+
# This is an internal class used by the native code to transfer results
|
|
18
|
+
# between Rust and Ruby.
|
|
19
|
+
#
|
|
20
|
+
# @private
|
|
21
|
+
class DynamicResult
|
|
22
|
+
# @private
|
|
23
|
+
def initialize
|
|
24
|
+
raise NotImplementedError, "This class is implemented natively in Rust"
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Represents the context of a Kafka message, providing metadata and control
|
|
29
|
+
# capabilities for message handling.
|
|
30
|
+
#
|
|
31
|
+
# Instances of this class are created by the native code and passed to your
|
|
32
|
+
# EventHandler's #on_message method.
|
|
33
|
+
#
|
|
34
|
+
# @see ext/prosody/src/handler/context.rs for implementation
|
|
35
|
+
class Context
|
|
36
|
+
# @private
|
|
37
|
+
def initialize
|
|
38
|
+
raise NotImplementedError, "This class is implemented natively in Rust"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Checks if cancellation has been requested.
|
|
42
|
+
#
|
|
43
|
+
# This method can be called within message handlers to detect when the
|
|
44
|
+
# handler should exit. Cancellation includes message-level cancellation
|
|
45
|
+
# (e.g., handler timeout) and partition shutdown. During shutdown,
|
|
46
|
+
# cancellation is delayed until near the end of the shutdown timeout to
|
|
47
|
+
# allow in-flight work to complete.
|
|
48
|
+
#
|
|
49
|
+
# @return [Boolean] true if cancellation has been requested, false otherwise
|
|
50
|
+
#
|
|
51
|
+
# @example Checking for cancellation in a loop
|
|
52
|
+
# def on_message(context, message)
|
|
53
|
+
# items = message.payload["items"]
|
|
54
|
+
# items.each do |item|
|
|
55
|
+
# return if context.should_cancel?
|
|
56
|
+
# process_item(item)
|
|
57
|
+
# end
|
|
58
|
+
# end
|
|
59
|
+
def should_cancel?
|
|
60
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Blocks until cancellation is signaled.
|
|
64
|
+
#
|
|
65
|
+
# Cancellation includes message-level cancellation (e.g., handler timeout)
|
|
66
|
+
# and partition shutdown. During shutdown, cancellation is delayed until near
|
|
67
|
+
# the end of the shutdown timeout to allow in-flight work to complete.
|
|
68
|
+
# This method is useful for long-running handlers that need to wait for
|
|
69
|
+
# external events while remaining responsive to cancellation.
|
|
70
|
+
#
|
|
71
|
+
# @return [void]
|
|
72
|
+
#
|
|
73
|
+
# @example Waiting for cancellation
|
|
74
|
+
# def on_message(context, message)
|
|
75
|
+
# # Do some work, then wait for cancellation
|
|
76
|
+
# context.on_cancel
|
|
77
|
+
# end
|
|
78
|
+
def on_cancel
|
|
79
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Schedules a timer to fire at the specified time.
|
|
83
|
+
#
|
|
84
|
+
# Timers allow you to delay execution or implement timeout behavior within
|
|
85
|
+
# your message handlers. When a timer fires, your handler's #on_timer method
|
|
86
|
+
# will be called with the timer object.
|
|
87
|
+
#
|
|
88
|
+
# @param time [Time] When the timer should fire
|
|
89
|
+
# @return [void]
|
|
90
|
+
# @raise [ArgumentError] If the time is invalid or outside the supported range (1970-2106)
|
|
91
|
+
# @raise [RuntimeError] If timer scheduling fails
|
|
92
|
+
#
|
|
93
|
+
# @example Scheduling a delayed action
|
|
94
|
+
# def on_message(context, message)
|
|
95
|
+
# # Schedule a timer to fire in 30 seconds
|
|
96
|
+
# context.schedule(Time.now + 30)
|
|
97
|
+
# end
|
|
98
|
+
#
|
|
99
|
+
# def on_timer(context, timer)
|
|
100
|
+
# puts "Timer fired for key: #{timer.key}"
|
|
101
|
+
# end
|
|
102
|
+
def schedule(time)
|
|
103
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Clears all scheduled timers and schedules a new one at the specified time.
|
|
107
|
+
#
|
|
108
|
+
# This is equivalent to calling clear_scheduled followed by schedule, but
|
|
109
|
+
# performed atomically.
|
|
110
|
+
#
|
|
111
|
+
# @param time [Time] When the new timer should fire
|
|
112
|
+
# @return [void]
|
|
113
|
+
# @raise [ArgumentError] If the time is invalid or outside the supported range
|
|
114
|
+
# @raise [RuntimeError] If timer operations fail
|
|
115
|
+
#
|
|
116
|
+
# @example Replacing all timers with a new one
|
|
117
|
+
# def on_message(context, message)
|
|
118
|
+
# # Clear any existing timers and schedule a new one
|
|
119
|
+
# context.clear_and_schedule(Time.now + 60)
|
|
120
|
+
# end
|
|
121
|
+
def clear_and_schedule(time)
|
|
122
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Unschedules a timer that was scheduled for the specified time.
|
|
126
|
+
#
|
|
127
|
+
# If multiple timers were scheduled for the same time, this will remove one
|
|
128
|
+
# of them. If no timer exists for the specified time, this method does nothing.
|
|
129
|
+
#
|
|
130
|
+
# @param time [Time] The time for which to unschedule the timer
|
|
131
|
+
# @return [void]
|
|
132
|
+
# @raise [ArgumentError] If the time is invalid
|
|
133
|
+
# @raise [RuntimeError] If timer unscheduling fails
|
|
134
|
+
#
|
|
135
|
+
# @example Canceling a specific timer
|
|
136
|
+
# def on_message(context, message)
|
|
137
|
+
# timer_time = Time.now + 30
|
|
138
|
+
# context.schedule(timer_time)
|
|
139
|
+
#
|
|
140
|
+
# # Later, cancel that specific timer
|
|
141
|
+
# context.unschedule(timer_time)
|
|
142
|
+
# end
|
|
143
|
+
def unschedule(time)
|
|
144
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# Clears all scheduled timers.
|
|
148
|
+
#
|
|
149
|
+
# After calling this method, no timers will be scheduled to fire for this
|
|
150
|
+
# message context.
|
|
151
|
+
#
|
|
152
|
+
# @return [void]
|
|
153
|
+
# @raise [RuntimeError] If clearing timers fails
|
|
154
|
+
#
|
|
155
|
+
# @example Canceling all timers
|
|
156
|
+
# def on_message(context, message)
|
|
157
|
+
# context.clear_scheduled
|
|
158
|
+
# end
|
|
159
|
+
def clear_scheduled
|
|
160
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# Returns all currently scheduled timer times.
|
|
164
|
+
#
|
|
165
|
+
# The returned array contains Time objects representing when each scheduled
|
|
166
|
+
# timer will fire. The array may be empty if no timers are scheduled.
|
|
167
|
+
#
|
|
168
|
+
# @return [Array<Time>] Array of scheduled timer times
|
|
169
|
+
# @raise [RuntimeError] If retrieving scheduled times fails
|
|
170
|
+
#
|
|
171
|
+
# @example Checking scheduled timers
|
|
172
|
+
# def on_message(context, message)
|
|
173
|
+
# scheduled_times = context.scheduled
|
|
174
|
+
# puts "#{scheduled_times.length} timers scheduled"
|
|
175
|
+
# scheduled_times.each do |time|
|
|
176
|
+
# puts "Timer will fire at: #{time}"
|
|
177
|
+
# end
|
|
178
|
+
# end
|
|
179
|
+
def scheduled
|
|
180
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# Represents a Kafka message with its metadata and payload.
|
|
185
|
+
#
|
|
186
|
+
# Instances of this class are created by the native code and passed to your
|
|
187
|
+
# EventHandler's #on_message method.
|
|
188
|
+
#
|
|
189
|
+
# @see ext/prosody/src/handler/message.rs for implementation
|
|
190
|
+
class Message
|
|
191
|
+
# Returns the Kafka topic this message was published to.
|
|
192
|
+
#
|
|
193
|
+
# @return [String] The topic name
|
|
194
|
+
def topic
|
|
195
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# Returns the Kafka partition number for this message.
|
|
199
|
+
#
|
|
200
|
+
# @return [Integer] The partition number
|
|
201
|
+
def partition
|
|
202
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# Returns the Kafka offset of this message within its partition.
|
|
206
|
+
#
|
|
207
|
+
# @return [Integer] The message offset
|
|
208
|
+
def offset
|
|
209
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
# Returns the message key used for partitioning.
|
|
213
|
+
#
|
|
214
|
+
# @return [String] The message key
|
|
215
|
+
def key
|
|
216
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
# Returns the timestamp when the message was created.
|
|
220
|
+
#
|
|
221
|
+
# @return [Time] The message timestamp
|
|
222
|
+
def timestamp
|
|
223
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# Returns the deserialized message payload.
|
|
227
|
+
#
|
|
228
|
+
# The payload is automatically deserialized from JSON to Ruby objects.
|
|
229
|
+
#
|
|
230
|
+
# @return [Object] The message content
|
|
231
|
+
def payload
|
|
232
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# Represents a timer that was scheduled to fire at a specific time.
|
|
237
|
+
#
|
|
238
|
+
# Timer instances are created by the native code and passed to your
|
|
239
|
+
# EventHandler's #on_timer method when a scheduled timer fires.
|
|
240
|
+
#
|
|
241
|
+
# @see ext/prosody/src/handler/trigger.rs for implementation
|
|
242
|
+
class Timer
|
|
243
|
+
# @private
|
|
244
|
+
def initialize
|
|
245
|
+
raise NotImplementedError, "This class is implemented natively in Rust"
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
# Returns the entity key identifying what this timer belongs to.
|
|
249
|
+
#
|
|
250
|
+
# The key is typically the same as the message key that was being processed
|
|
251
|
+
# when the timer was scheduled.
|
|
252
|
+
#
|
|
253
|
+
# @return [String] The entity key
|
|
254
|
+
def key
|
|
255
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# Returns the time when this timer was scheduled to fire.
|
|
259
|
+
#
|
|
260
|
+
# Note: Due to CompactDateTime's second-level precision, the returned time
|
|
261
|
+
# will have zero nanoseconds even if the original scheduled time had
|
|
262
|
+
# sub-second precision.
|
|
263
|
+
#
|
|
264
|
+
# @return [Time] The scheduled execution time
|
|
265
|
+
def time
|
|
266
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
267
|
+
end
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
# Main client for interacting with the Prosody messaging system.
|
|
271
|
+
# Provides methods for sending messages and subscribing to Kafka topics.
|
|
272
|
+
#
|
|
273
|
+
# @see ext/prosody/src/client/mod.rs for implementation
|
|
274
|
+
class Client
|
|
275
|
+
# Creates a new Prosody client with the given configuration.
|
|
276
|
+
#
|
|
277
|
+
# @param config [Hash, Configuration] Client configuration
|
|
278
|
+
# @return [Client] A new client instance
|
|
279
|
+
# @raise [ArgumentError] If the configuration is invalid
|
|
280
|
+
# @raise [RuntimeError] If client initialization fails
|
|
281
|
+
#
|
|
282
|
+
# @example Creating a client with a Configuration object
|
|
283
|
+
# config = Prosody::Configuration.new do |c|
|
|
284
|
+
# c.bootstrap_servers = "localhost:9092"
|
|
285
|
+
# c.group_id = "my-consumer-group"
|
|
286
|
+
# end
|
|
287
|
+
# client = Prosody::Client.new(config)
|
|
288
|
+
#
|
|
289
|
+
# @example Creating a client with a hash
|
|
290
|
+
# client = Prosody::Client.new(
|
|
291
|
+
# bootstrap_servers: "localhost:9092",
|
|
292
|
+
# group_id: "my-consumer-group"
|
|
293
|
+
# )
|
|
294
|
+
def self.new(config)
|
|
295
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
# Returns the current state of the consumer.
|
|
299
|
+
#
|
|
300
|
+
# The consumer can be in one of three states:
|
|
301
|
+
# - `:unconfigured` - The consumer has not been configured yet
|
|
302
|
+
# - `:configured` - The consumer is configured but not running
|
|
303
|
+
# - `:running` - The consumer is actively consuming messages
|
|
304
|
+
#
|
|
305
|
+
# @return [Symbol] The current consumer state
|
|
306
|
+
def consumer_state
|
|
307
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
# Returns the number of Kafka partitions currently assigned to this consumer.
|
|
311
|
+
#
|
|
312
|
+
# This method can be used to monitor the consumer's workload and ensure
|
|
313
|
+
# proper load balancing across multiple consumer instances.
|
|
314
|
+
#
|
|
315
|
+
# @return [Integer] The number of assigned partitions
|
|
316
|
+
def assigned_partitions
|
|
317
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
# Checks if the consumer is stalled.
|
|
321
|
+
#
|
|
322
|
+
# A stalled consumer is one that has stopped processing messages due to
|
|
323
|
+
# errors or reaching processing limits. This can be used to detect unhealthy
|
|
324
|
+
# consumers that need attention.
|
|
325
|
+
#
|
|
326
|
+
# @return [Boolean] true if the consumer is stalled, false otherwise
|
|
327
|
+
def is_stalled?
|
|
328
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
# Sends a message to the specified Kafka topic.
|
|
332
|
+
#
|
|
333
|
+
# @param topic [String] The destination topic name
|
|
334
|
+
# @param key [String] The message key for partitioning
|
|
335
|
+
# @param payload [Object] The message payload (will be serialized to JSON)
|
|
336
|
+
# @return [void]
|
|
337
|
+
# @raise [RuntimeError] If the message cannot be sent
|
|
338
|
+
#
|
|
339
|
+
# @example Sending a simple message
|
|
340
|
+
# client.send_message("my-topic", "user-123", { event: "login", timestamp: Time.now })
|
|
341
|
+
def send_message(topic, key, payload)
|
|
342
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
# Subscribes to Kafka topics using the provided handler.
|
|
346
|
+
# The handler must implement an `on_message(context, message)` method.
|
|
347
|
+
#
|
|
348
|
+
# @param handler [EventHandler] A handler object that processes messages
|
|
349
|
+
# @return [void]
|
|
350
|
+
# @raise [RuntimeError] If subscription fails
|
|
351
|
+
#
|
|
352
|
+
# @example Subscribing with a handler
|
|
353
|
+
# class MyHandler < Prosody::EventHandler
|
|
354
|
+
# def on_message(context, message)
|
|
355
|
+
# puts "Received message: #{message.payload}"
|
|
356
|
+
# end
|
|
357
|
+
# end
|
|
358
|
+
#
|
|
359
|
+
# client.subscribe(MyHandler.new)
|
|
360
|
+
def subscribe(handler)
|
|
361
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
# Unsubscribes from all topics, stopping message processing.
|
|
365
|
+
#
|
|
366
|
+
# This method gracefully shuts down the consumer, completing any in-flight
|
|
367
|
+
# messages before stopping.
|
|
368
|
+
#
|
|
369
|
+
# @return [void]
|
|
370
|
+
# @raise [RuntimeError] If unsubscription fails
|
|
371
|
+
#
|
|
372
|
+
# @example Shutting down a consumer
|
|
373
|
+
# client.unsubscribe
|
|
374
|
+
def unsubscribe
|
|
375
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
# Returns the configured source system identifier.
|
|
379
|
+
#
|
|
380
|
+
# The source system is used to identify the originating service or
|
|
381
|
+
# component in produced messages, enabling loop detection.
|
|
382
|
+
#
|
|
383
|
+
# @return [String] The source system identifier
|
|
384
|
+
#
|
|
385
|
+
# @example Getting the source system
|
|
386
|
+
# puts client.source_system # => "my-service"
|
|
387
|
+
def source_system
|
|
388
|
+
raise NotImplementedError, "This method is implemented natively in Rust"
|
|
389
|
+
end
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
# Internal processor for executing tasks asynchronously.
|
|
393
|
+
# This class is used internally by the native code.
|
|
394
|
+
#
|
|
395
|
+
# @private
|
|
396
|
+
class AsyncTaskProcessor
|
|
397
|
+
# @private
|
|
398
|
+
def initialize(logger = Prosody.logger)
|
|
399
|
+
# Actual implementation is in lib/prosody/processor.rb
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
# @private
|
|
403
|
+
def start
|
|
404
|
+
# Actual implementation is in lib/prosody/processor.rb
|
|
405
|
+
end
|
|
406
|
+
|
|
407
|
+
# @private
|
|
408
|
+
def stop
|
|
409
|
+
# Actual implementation is in lib/prosody/processor.rb
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
# @private
|
|
413
|
+
def submit(task_id, carrier, callback, &block)
|
|
414
|
+
# Actual implementation is in lib/prosody/processor.rb
|
|
415
|
+
end
|
|
416
|
+
end
|
|
417
|
+
end
|