Class: RedisIPC::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_ipc/channel.rb

Defined Under Namespace

Classes: Event

Class Method Summary collapse

Class Method Details

.channel(name) ⇒ Object



42
43
44
# File 'lib/redis_ipc/channel.rb', line 42

def self.channel(name)
  group(name)
end

.connectObject

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?

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/redis_ipc/channel.rb', line 81

def self.connected?
  return false if @stream.nil?

  @stream.connected?
end

.disconnectObject

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

Parameters:

  • event_id (String, Symbol)

    A unique name of this event

  • params (Array) (defaults to: [])

    A list of params that this event is expected

  • &block (Proc)

    The code that is called when the event is triggered



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

Parameters:

  • name (String)


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

Parameters:

  • &block (Proc)


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

Parameters:

  • name (String)


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

Parameters:

  • event_id (String, Symbol)

    The unique event name to be called on the target group.

  • target (String, Symbol)

    The name of the target group to call this event on

  • params (Hash) (defaults to: {})

    The keys and values to be sent into the event

Returns:



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