Class: RedisIPC::Channel
- Inherits:
-
Object
- Object
- RedisIPC::Channel
- Defined in:
- lib/redis_ipc/channel.rb
Defined Under Namespace
Classes: Event
Class Method Summary collapse
- .channel(name) ⇒ Object
-
.connect ⇒ Object
Connects to the stream and starts processing any requests.
-
.connected? ⇒ Boolean
Is the channel connected to the stream?.
-
.disconnect ⇒ Object
Disconnects from the stream and stops processing requests.
-
.event(event_id, params: [], &block) ⇒ Object
Defines an event that can be triggered from other groups.
-
.group(name) ⇒ Object
Sets the name of the Redis Consumer Group used by this channel.
- .inherited(sub_class) ⇒ Object
-
.on_error(&block) ⇒ Object
Sets the on_error callback.
-
.stream(name) ⇒ Object
Sets the name of the Redis Stream to be used to communicate across.
-
.trigger_event(event_id, target:, params: {}) ⇒ RedisIPC::Stream::Entry
Triggers an event on the target group using the given params.
Class Method Details
.channel(name) ⇒ Object
42 43 44 |
# File 'lib/redis_ipc/channel.rb', line 42 def self.channel(name) group(name) end |
.connect ⇒ Object
Connects to the stream and starts processing any requests
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/redis_ipc/channel.rb', line 59 def self.connect(**) raise ConnectionError, "Channel #{group_name} is already connected" if connected? @stream = Stream.new(stream_name, group_name) @stream.on_request(&method(:on_request)) @stream.on_error { |e| on_error.call(e) } if on_error @stream.connect(**) true end |
.connected? ⇒ Boolean
Is the channel connected to the stream?
81 82 83 84 85 |
# File 'lib/redis_ipc/channel.rb', line 81 def self.connected? return false if @stream.nil? @stream.connected? end |
.disconnect ⇒ Object
Disconnects from the stream and stops processing requests
73 74 75 76 |
# File 'lib/redis_ipc/channel.rb', line 73 def self.disconnect @stream&.disconnect @stream = nil end |
.event(event_id, params: [], &block) ⇒ Object
Defines an event that can be triggered from other groups
94 95 96 97 98 |
# File 'lib/redis_ipc/channel.rb', line 94 def self.event(event_id, params: [], &block) self.events ||= HashWithIndifferentAccess.new events[event_id] = Event.new(params, &block) end |
.group(name) ⇒ Object
Sets the name of the Redis Consumer Group used by this channel
38 39 40 |
# File 'lib/redis_ipc/channel.rb', line 38 def self.group(name) self.group_name = name end |
.inherited(sub_class) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/redis_ipc/channel.rb', line 17 def self.inherited(sub_class) sub_class.stream_name = stream_name sub_class.group_name = group_name sub_class.events = events.dup sub_class.on_error = on_error end |
.on_error(&block) ⇒ Object
Sets the on_error callback
51 52 53 |
# File 'lib/redis_ipc/channel.rb', line 51 def self.on_error(&block) self.on_error = block end |
.stream(name) ⇒ Object
Sets the name of the Redis Stream to be used to communicate across
29 30 31 |
# File 'lib/redis_ipc/channel.rb', line 29 def self.stream(name) self.stream_name = name end |
.trigger_event(event_id, target:, params: {}) ⇒ RedisIPC::Stream::Entry
Triggers an event on the target group using the given params
110 111 112 113 114 115 116 |
# File 'lib/redis_ipc/channel.rb', line 110 def self.trigger_event(event_id, target:, params: {}) if @stream.nil? raise ConnectionError, "Stream has not been set up correctly. Please call #{self.class}#connect first" end @stream.send_to_group(to: target, content: {event: event_id, params: params}) end |