Class: CelluloidPubsub::RedisReactor

Inherits:
Reactor
  • Object
show all
Includes:
BaseActor
Defined in:
lib/celluloid_pubsub_redis_adapter/redis_reactor.rb

Overview

reactor used for redis pubsub

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectedBoolean

returns true if already connected to redis otherwise false

Returns:

  • (Boolean)

    returns true if already connected to redis otherwise false



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/celluloid_pubsub_redis_adapter/redis_reactor.rb', line 9

class RedisReactor < CelluloidPubsub::Reactor
  include CelluloidPubsub::BaseActor

  attr_accessor :connected, :connection

  alias_method :connected?, :connected

  # returns true if already connected to redis otherwise false
  #
  # @return [Boolean] returns true if already connected to redis otherwise false
  #
  # @api public
  def connected
    @connected ||= false
  end

  # method used to unsubscribe from a channel
  # @see #redis_action
  #
  # @return [void]
  #
  # @api public
  def unsubscribe(channel, data)
    super
    async.redis_action('unsubscribe', channel)
  end

  # method used to subscribe to a channel
  # @see #redis_action
  #
  # @return [void]
  #
  # @api public
  def add_subscriber_to_channel(channel, message)
    super
    async.redis_action('subscribe', channel, message)
  end

  # method used to unsubscribe from a channel
  # @see #redis_action
  #
  # @return [void]
  #
  # @api public
  def unsubscribe_from_channel(channel)
    super
    async.redis_action('unsubscribe', channel)
  end

  # method used to unsubscribe  from all channels
  # @see #redis_action
  #
  # @return [void]
  #
  # @api public
  def unsubscribe_all(channel, data)
    info 'clearing connections'
    shutdown
  end

  # method used to shutdown the reactor and unsubscribe from all channels
  # @see #redis_action
  #
  # @return [void]
  #
  # @api public
  def shutdown
    @channels.dup.each do |channel|
      redis_action('unsubscribe', channel) unless ENV['RACK_ENV'] == 'test'
    end if @channels.present?
    super
  end

  # method used to publish event using redis
  #
  # @return [void]
  #
  # @api public
  def server_pusblish_event(topic, data)
    return if topic.blank? || data.blank?
    connect_to_redis do |connection|
      connection.publish(topic, data)
    end
  rescue => exception
    log_debug("could not publish message #{message} into topic #{current_topic} because of #{exception.inspect}")
  end

private

  # method used to run the enventmachine and setup the exception handler
  # @see #run_the_eventmachine
  # @see #setup_em_exception_handler
  #
  # @param [Proc] block the block that will use the connection
  #
  # @return [void]
  #
  # @api private
  def connect_to_redis(&block)
    require 'eventmachine'
    require 'em-hiredis'
    run_the_eventmachine(&block)
    setup_em_exception_handler
  end

  # method used to connect to redis and yield the connection
  #
  # @param [Proc] block the block that will use the connection
  #
  # @return [void]
  #
  # @api private
  def run_the_eventmachine(&block)
    EM.run do
      @connection ||= EM::Hiredis.connect
      @connected = true
      block.call @connection
    end
  end

  # method used to setup the eventmachine exception handler
  #
  # @return [void]
  #
  # @api private
  def setup_em_exception_handler
    EM.error_handler do |error|
      debug error unless filtered_error?(error)
    end
  end

  # method used to fetch the pubsub client from the connection and yield it
  #
  # @return [void]
  #
  # @api private
  def fetch_pubsub
    connect_to_redis do |connection|
      @pubsub ||= connection.pubsub
      yield @pubsub if block_given?
    end
  end

  # method used to fetch the pubsub client from the connection and yield it
  # @see #action_subscribe
  #
  # @param [string] action The action that will be checked
  # @param [string] channel The channel that reactor has subscribed to
  # @param [string] message The initial message used to subscribe
  #
  # @return [void]
  #
  # @api private
  def action_success(action, channel, message)
    action_subscribe?(action) ? message.merge('client_action' => 'successful_subscription', 'channel' => channel) : nil
  end

  # method used execute an action (subscribe or unsubscribe ) to redis
  # @see #prepare_redis_action
  # @see #action_success
  # @see #register_subscription_callbacks
  #
  # @param [string] action The action that will be checked
  # @param [string] channel The channel that reactor has subscribed to
  # @param [string] message The initial message used to subscribe
  #
  # @return [void]
  #
  # @api private
  def redis_action(action, channel = nil, message = {})
    fetch_pubsub do |pubsub|
      callback = prepare_redis_action(pubsub, action)
      success_message = action_success(action, channel, message)
      args = action_subscribe?(action) ? [channel, callback] : [channel]
      subscription = pubsub.send(action, *args)
      register_subscription_callbacks(subscription, action, success_message)
    end
  end

  # method used check if the action is subscribe and write the incoming message to be websocket or log the message otherwise
  # @see #log_unsubscriptions
  # @see #action_subscribe
  #
  # @param [String] action The action that will be checked if it is subscribed
  #
  # @return [void]
  #
  # @api private
  def prepare_redis_action(pubsub, action)
    log_unsubscriptions(pubsub)
    proc do |subscribed_message|
      action_subscribe?(action) ? (@websocket << subscribed_message) : log_debug(message)
    end
  end

  # method used to listen to unsubscriptions and log them to log file
  # @see #register_redis_callback
  # @see #register_redis_error_callback
  #
  # @param [EM::Hiredis::PubsubClient] pubsub The pubsub client that will be used to listen to unsubscriptions
  #
  # @return [void]
  #
  # @api private
  def log_unsubscriptions(pubsub)
    pubsub.on(:unsubscribe) do |subscribed_channel, remaining_subscriptions|
      log_debug [:unsubscribe_happened, subscribed_channel, remaining_subscriptions].inspect
    end
  end

  # method used registers the sucess and error callabacks
  # @see #register_redis_callback
  # @see #register_redis_error_callback
  #
  # @param [EM::DefaultDeferrable] subscription The subscription object
  # @param [string] action The action that will be checked
  # @param [string] sucess_message The initial message used to subscribe
  #
  # @return [void]
  #
  # @api private
  def register_subscription_callbacks(subscription, action, sucess_message = nil)
    register_redis_callback(subscription, action, sucess_message)
    register_redis_error_callback(subscription, action)
  end

  # the method will return true if debug is enabled
  #
  #
  # @return [Boolean] returns true if debug is enabled otherwise false
  #
  # @api public
  def debug_enabled?
    @server.debug_enabled?
  end

  # method used to register a success callback  and if action is subscribe will write
  # back to the websocket a message that will say it is a successful_subscription
  # If action is something else, will log the incoming message
  # @see #log_debug
  #
  # @param [EM::DefaultDeferrable] subscription The subscription object
  # @param [string] sucess_message The initial message used to subscribe
  #
  # @return [void]
  #
  # @api private
  def register_redis_callback(subscription, action, sucess_message = nil)
    subscription.callback do |subscriptions_ids|
      if sucess_message.present?
        @websocket << sucess_message.merge('subscriptions' => subscriptions_ids).to_json
      else
        log_debug "#{action} success #{sucess_message.inspect}"
      end
    end
  end

  # Register an error callback on the deferrable object and logs to file the incoming message
  # @see #log_debug
  #
  # @param [EM::DefaultDeferrable] subscription The subscription object
  # @param [string] action The action that will be checked
  #
  # @return [void]
  #
  # @api private
  def register_redis_error_callback(subscription, action)
    subscription.errback { |reply| log_debug "#{action} error #{reply.inspect}" }
  end
end

#connectionEM::Hiredis

Returns The connection used for redis.

Returns:

  • (EM::Hiredis)

    The connection used for redis



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/celluloid_pubsub_redis_adapter/redis_reactor.rb', line 9

class RedisReactor < CelluloidPubsub::Reactor
  include CelluloidPubsub::BaseActor

  attr_accessor :connected, :connection

  alias_method :connected?, :connected

  # returns true if already connected to redis otherwise false
  #
  # @return [Boolean] returns true if already connected to redis otherwise false
  #
  # @api public
  def connected
    @connected ||= false
  end

  # method used to unsubscribe from a channel
  # @see #redis_action
  #
  # @return [void]
  #
  # @api public
  def unsubscribe(channel, data)
    super
    async.redis_action('unsubscribe', channel)
  end

  # method used to subscribe to a channel
  # @see #redis_action
  #
  # @return [void]
  #
  # @api public
  def add_subscriber_to_channel(channel, message)
    super
    async.redis_action('subscribe', channel, message)
  end

  # method used to unsubscribe from a channel
  # @see #redis_action
  #
  # @return [void]
  #
  # @api public
  def unsubscribe_from_channel(channel)
    super
    async.redis_action('unsubscribe', channel)
  end

  # method used to unsubscribe  from all channels
  # @see #redis_action
  #
  # @return [void]
  #
  # @api public
  def unsubscribe_all(channel, data)
    info 'clearing connections'
    shutdown
  end

  # method used to shutdown the reactor and unsubscribe from all channels
  # @see #redis_action
  #
  # @return [void]
  #
  # @api public
  def shutdown
    @channels.dup.each do |channel|
      redis_action('unsubscribe', channel) unless ENV['RACK_ENV'] == 'test'
    end if @channels.present?
    super
  end

  # method used to publish event using redis
  #
  # @return [void]
  #
  # @api public
  def server_pusblish_event(topic, data)
    return if topic.blank? || data.blank?
    connect_to_redis do |connection|
      connection.publish(topic, data)
    end
  rescue => exception
    log_debug("could not publish message #{message} into topic #{current_topic} because of #{exception.inspect}")
  end

private

  # method used to run the enventmachine and setup the exception handler
  # @see #run_the_eventmachine
  # @see #setup_em_exception_handler
  #
  # @param [Proc] block the block that will use the connection
  #
  # @return [void]
  #
  # @api private
  def connect_to_redis(&block)
    require 'eventmachine'
    require 'em-hiredis'
    run_the_eventmachine(&block)
    setup_em_exception_handler
  end

  # method used to connect to redis and yield the connection
  #
  # @param [Proc] block the block that will use the connection
  #
  # @return [void]
  #
  # @api private
  def run_the_eventmachine(&block)
    EM.run do
      @connection ||= EM::Hiredis.connect
      @connected = true
      block.call @connection
    end
  end

  # method used to setup the eventmachine exception handler
  #
  # @return [void]
  #
  # @api private
  def setup_em_exception_handler
    EM.error_handler do |error|
      debug error unless filtered_error?(error)
    end
  end

  # method used to fetch the pubsub client from the connection and yield it
  #
  # @return [void]
  #
  # @api private
  def fetch_pubsub
    connect_to_redis do |connection|
      @pubsub ||= connection.pubsub
      yield @pubsub if block_given?
    end
  end

  # method used to fetch the pubsub client from the connection and yield it
  # @see #action_subscribe
  #
  # @param [string] action The action that will be checked
  # @param [string] channel The channel that reactor has subscribed to
  # @param [string] message The initial message used to subscribe
  #
  # @return [void]
  #
  # @api private
  def action_success(action, channel, message)
    action_subscribe?(action) ? message.merge('client_action' => 'successful_subscription', 'channel' => channel) : nil
  end

  # method used execute an action (subscribe or unsubscribe ) to redis
  # @see #prepare_redis_action
  # @see #action_success
  # @see #register_subscription_callbacks
  #
  # @param [string] action The action that will be checked
  # @param [string] channel The channel that reactor has subscribed to
  # @param [string] message The initial message used to subscribe
  #
  # @return [void]
  #
  # @api private
  def redis_action(action, channel = nil, message = {})
    fetch_pubsub do |pubsub|
      callback = prepare_redis_action(pubsub, action)
      success_message = action_success(action, channel, message)
      args = action_subscribe?(action) ? [channel, callback] : [channel]
      subscription = pubsub.send(action, *args)
      register_subscription_callbacks(subscription, action, success_message)
    end
  end

  # method used check if the action is subscribe and write the incoming message to be websocket or log the message otherwise
  # @see #log_unsubscriptions
  # @see #action_subscribe
  #
  # @param [String] action The action that will be checked if it is subscribed
  #
  # @return [void]
  #
  # @api private
  def prepare_redis_action(pubsub, action)
    log_unsubscriptions(pubsub)
    proc do |subscribed_message|
      action_subscribe?(action) ? (@websocket << subscribed_message) : log_debug(message)
    end
  end

  # method used to listen to unsubscriptions and log them to log file
  # @see #register_redis_callback
  # @see #register_redis_error_callback
  #
  # @param [EM::Hiredis::PubsubClient] pubsub The pubsub client that will be used to listen to unsubscriptions
  #
  # @return [void]
  #
  # @api private
  def log_unsubscriptions(pubsub)
    pubsub.on(:unsubscribe) do |subscribed_channel, remaining_subscriptions|
      log_debug [:unsubscribe_happened, subscribed_channel, remaining_subscriptions].inspect
    end
  end

  # method used registers the sucess and error callabacks
  # @see #register_redis_callback
  # @see #register_redis_error_callback
  #
  # @param [EM::DefaultDeferrable] subscription The subscription object
  # @param [string] action The action that will be checked
  # @param [string] sucess_message The initial message used to subscribe
  #
  # @return [void]
  #
  # @api private
  def register_subscription_callbacks(subscription, action, sucess_message = nil)
    register_redis_callback(subscription, action, sucess_message)
    register_redis_error_callback(subscription, action)
  end

  # the method will return true if debug is enabled
  #
  #
  # @return [Boolean] returns true if debug is enabled otherwise false
  #
  # @api public
  def debug_enabled?
    @server.debug_enabled?
  end

  # method used to register a success callback  and if action is subscribe will write
  # back to the websocket a message that will say it is a successful_subscription
  # If action is something else, will log the incoming message
  # @see #log_debug
  #
  # @param [EM::DefaultDeferrable] subscription The subscription object
  # @param [string] sucess_message The initial message used to subscribe
  #
  # @return [void]
  #
  # @api private
  def register_redis_callback(subscription, action, sucess_message = nil)
    subscription.callback do |subscriptions_ids|
      if sucess_message.present?
        @websocket << sucess_message.merge('subscriptions' => subscriptions_ids).to_json
      else
        log_debug "#{action} success #{sucess_message.inspect}"
      end
    end
  end

  # Register an error callback on the deferrable object and logs to file the incoming message
  # @see #log_debug
  #
  # @param [EM::DefaultDeferrable] subscription The subscription object
  # @param [string] action The action that will be checked
  #
  # @return [void]
  #
  # @api private
  def register_redis_error_callback(subscription, action)
    subscription.errback { |reply| log_debug "#{action} error #{reply.inspect}" }
  end
end

Instance Method Details

#add_subscriber_to_channel(channel, message) ⇒ void

This method returns an undefined value.

method used to subscribe to a channel

See Also:

  • #redis_action


42
43
44
45
# File 'lib/celluloid_pubsub_redis_adapter/redis_reactor.rb', line 42

def add_subscriber_to_channel(channel, message)
  super
  async.redis_action('subscribe', channel, message)
end

#connected?Boolean

Returns true if already connected to redis

Returns:

  • (Boolean)

    returns true if already connected to redis



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/celluloid_pubsub_redis_adapter/redis_reactor.rb', line 14

class RedisReactor < CelluloidPubsub::Reactor
  include CelluloidPubsub::BaseActor

  attr_accessor :connected, :connection

  alias_method :connected?, :connected

  # returns true if already connected to redis otherwise false
  #
  # @return [Boolean] returns true if already connected to redis otherwise false
  #
  # @api public
  def connected
    @connected ||= false
  end

  # method used to unsubscribe from a channel
  # @see #redis_action
  #
  # @return [void]
  #
  # @api public
  def unsubscribe(channel, data)
    super
    async.redis_action('unsubscribe', channel)
  end

  # method used to subscribe to a channel
  # @see #redis_action
  #
  # @return [void]
  #
  # @api public
  def add_subscriber_to_channel(channel, message)
    super
    async.redis_action('subscribe', channel, message)
  end

  # method used to unsubscribe from a channel
  # @see #redis_action
  #
  # @return [void]
  #
  # @api public
  def unsubscribe_from_channel(channel)
    super
    async.redis_action('unsubscribe', channel)
  end

  # method used to unsubscribe  from all channels
  # @see #redis_action
  #
  # @return [void]
  #
  # @api public
  def unsubscribe_all(channel, data)
    info 'clearing connections'
    shutdown
  end

  # method used to shutdown the reactor and unsubscribe from all channels
  # @see #redis_action
  #
  # @return [void]
  #
  # @api public
  def shutdown
    @channels.dup.each do |channel|
      redis_action('unsubscribe', channel) unless ENV['RACK_ENV'] == 'test'
    end if @channels.present?
    super
  end

  # method used to publish event using redis
  #
  # @return [void]
  #
  # @api public
  def server_pusblish_event(topic, data)
    return if topic.blank? || data.blank?
    connect_to_redis do |connection|
      connection.publish(topic, data)
    end
  rescue => exception
    log_debug("could not publish message #{message} into topic #{current_topic} because of #{exception.inspect}")
  end

private

  # method used to run the enventmachine and setup the exception handler
  # @see #run_the_eventmachine
  # @see #setup_em_exception_handler
  #
  # @param [Proc] block the block that will use the connection
  #
  # @return [void]
  #
  # @api private
  def connect_to_redis(&block)
    require 'eventmachine'
    require 'em-hiredis'
    run_the_eventmachine(&block)
    setup_em_exception_handler
  end

  # method used to connect to redis and yield the connection
  #
  # @param [Proc] block the block that will use the connection
  #
  # @return [void]
  #
  # @api private
  def run_the_eventmachine(&block)
    EM.run do
      @connection ||= EM::Hiredis.connect
      @connected = true
      block.call @connection
    end
  end

  # method used to setup the eventmachine exception handler
  #
  # @return [void]
  #
  # @api private
  def setup_em_exception_handler
    EM.error_handler do |error|
      debug error unless filtered_error?(error)
    end
  end

  # method used to fetch the pubsub client from the connection and yield it
  #
  # @return [void]
  #
  # @api private
  def fetch_pubsub
    connect_to_redis do |connection|
      @pubsub ||= connection.pubsub
      yield @pubsub if block_given?
    end
  end

  # method used to fetch the pubsub client from the connection and yield it
  # @see #action_subscribe
  #
  # @param [string] action The action that will be checked
  # @param [string] channel The channel that reactor has subscribed to
  # @param [string] message The initial message used to subscribe
  #
  # @return [void]
  #
  # @api private
  def action_success(action, channel, message)
    action_subscribe?(action) ? message.merge('client_action' => 'successful_subscription', 'channel' => channel) : nil
  end

  # method used execute an action (subscribe or unsubscribe ) to redis
  # @see #prepare_redis_action
  # @see #action_success
  # @see #register_subscription_callbacks
  #
  # @param [string] action The action that will be checked
  # @param [string] channel The channel that reactor has subscribed to
  # @param [string] message The initial message used to subscribe
  #
  # @return [void]
  #
  # @api private
  def redis_action(action, channel = nil, message = {})
    fetch_pubsub do |pubsub|
      callback = prepare_redis_action(pubsub, action)
      success_message = action_success(action, channel, message)
      args = action_subscribe?(action) ? [channel, callback] : [channel]
      subscription = pubsub.send(action, *args)
      register_subscription_callbacks(subscription, action, success_message)
    end
  end

  # method used check if the action is subscribe and write the incoming message to be websocket or log the message otherwise
  # @see #log_unsubscriptions
  # @see #action_subscribe
  #
  # @param [String] action The action that will be checked if it is subscribed
  #
  # @return [void]
  #
  # @api private
  def prepare_redis_action(pubsub, action)
    log_unsubscriptions(pubsub)
    proc do |subscribed_message|
      action_subscribe?(action) ? (@websocket << subscribed_message) : log_debug(message)
    end
  end

  # method used to listen to unsubscriptions and log them to log file
  # @see #register_redis_callback
  # @see #register_redis_error_callback
  #
  # @param [EM::Hiredis::PubsubClient] pubsub The pubsub client that will be used to listen to unsubscriptions
  #
  # @return [void]
  #
  # @api private
  def log_unsubscriptions(pubsub)
    pubsub.on(:unsubscribe) do |subscribed_channel, remaining_subscriptions|
      log_debug [:unsubscribe_happened, subscribed_channel, remaining_subscriptions].inspect
    end
  end

  # method used registers the sucess and error callabacks
  # @see #register_redis_callback
  # @see #register_redis_error_callback
  #
  # @param [EM::DefaultDeferrable] subscription The subscription object
  # @param [string] action The action that will be checked
  # @param [string] sucess_message The initial message used to subscribe
  #
  # @return [void]
  #
  # @api private
  def register_subscription_callbacks(subscription, action, sucess_message = nil)
    register_redis_callback(subscription, action, sucess_message)
    register_redis_error_callback(subscription, action)
  end

  # the method will return true if debug is enabled
  #
  #
  # @return [Boolean] returns true if debug is enabled otherwise false
  #
  # @api public
  def debug_enabled?
    @server.debug_enabled?
  end

  # method used to register a success callback  and if action is subscribe will write
  # back to the websocket a message that will say it is a successful_subscription
  # If action is something else, will log the incoming message
  # @see #log_debug
  #
  # @param [EM::DefaultDeferrable] subscription The subscription object
  # @param [string] sucess_message The initial message used to subscribe
  #
  # @return [void]
  #
  # @api private
  def register_redis_callback(subscription, action, sucess_message = nil)
    subscription.callback do |subscriptions_ids|
      if sucess_message.present?
        @websocket << sucess_message.merge('subscriptions' => subscriptions_ids).to_json
      else
        log_debug "#{action} success #{sucess_message.inspect}"
      end
    end
  end

  # Register an error callback on the deferrable object and logs to file the incoming message
  # @see #log_debug
  #
  # @param [EM::DefaultDeferrable] subscription The subscription object
  # @param [string] action The action that will be checked
  #
  # @return [void]
  #
  # @api private
  def register_redis_error_callback(subscription, action)
    subscription.errback { |reply| log_debug "#{action} error #{reply.inspect}" }
  end
end

#server_pusblish_event(topic, data) ⇒ void

This method returns an undefined value.

method used to publish event using redis



87
88
89
90
91
92
93
94
# File 'lib/celluloid_pubsub_redis_adapter/redis_reactor.rb', line 87

def server_pusblish_event(topic, data)
  return if topic.blank? || data.blank?
  connect_to_redis do |connection|
    connection.publish(topic, data)
  end
rescue => exception
  log_debug("could not publish message #{message} into topic #{current_topic} because of #{exception.inspect}")
end

#shutdownvoid

This method returns an undefined value.

method used to shutdown the reactor and unsubscribe from all channels

See Also:

  • #redis_action


75
76
77
78
79
80
# File 'lib/celluloid_pubsub_redis_adapter/redis_reactor.rb', line 75

def shutdown
  @channels.dup.each do |channel|
    redis_action('unsubscribe', channel) unless ENV['RACK_ENV'] == 'test'
  end if @channels.present?
  super
end

#unsubscribe(channel, data) ⇒ void

This method returns an undefined value.

method used to unsubscribe from a channel

See Also:

  • #redis_action


31
32
33
34
# File 'lib/celluloid_pubsub_redis_adapter/redis_reactor.rb', line 31

def unsubscribe(channel, data)
  super
  async.redis_action('unsubscribe', channel)
end

#unsubscribe_all(channel, data) ⇒ void

This method returns an undefined value.

method used to unsubscribe from all channels

See Also:

  • #redis_action


64
65
66
67
# File 'lib/celluloid_pubsub_redis_adapter/redis_reactor.rb', line 64

def unsubscribe_all(channel, data)
  info 'clearing connections'
  shutdown
end

#unsubscribe_from_channel(channel) ⇒ void

This method returns an undefined value.

method used to unsubscribe from a channel

See Also:

  • #redis_action


53
54
55
56
# File 'lib/celluloid_pubsub_redis_adapter/redis_reactor.rb', line 53

def unsubscribe_from_channel(channel)
  super
  async.redis_action('unsubscribe', channel)
end