Class: Debounced::ServiceProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/debounced/service_proxy.rb

Overview

Ruby interface to the debounce service Input is an activity descriptor, and an object. When the activity is debounced, a callback method is invoked on the object. Assumes the object class has an initializer that accepts a hash of attributes, which are the instance variables

Constant Summary collapse

DELIMITER =
"\f".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServiceProxy

Returns a new instance of ServiceProxy.



15
16
17
18
19
20
# File 'lib/debounced/service_proxy.rb', line 15

def initialize
  @wait_timeout = Debounced.configuration.wait_timeout
  @logger = Debounced.configuration.logger
  @listening = false
  @mutex = Mutex.new
end

Instance Attribute Details

#listeningObject (readonly)

Returns the value of attribute listening.



13
14
15
# File 'lib/debounced/service_proxy.rb', line 13

def listening
  @listening
end

#loggerObject (readonly)

Returns the value of attribute logger.



13
14
15
# File 'lib/debounced/service_proxy.rb', line 13

def logger
  @logger
end

#wait_timeoutObject (readonly)

Returns the value of attribute wait_timeout.



13
14
15
# File 'lib/debounced/service_proxy.rb', line 13

def wait_timeout
  @wait_timeout
end

Instance Method Details

#debounce_activity(activity_descriptor, timeout, callback) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/debounced/service_proxy.rb', line 41

def debounce_activity(activity_descriptor, timeout, callback)
  SemanticLogger.tagged("send") do
    if !listening || socket.nil?
      logger.debug { "No connection to #{server_name}; skipping debounce step." }
      callback.call
    else
      logger_trace { "Sending #{activity_descriptor} to #{server_name}" }
      transmit(build_request(activity_descriptor, timeout, callback))
    end
  end
end

#listen(abort_signal = nil) ⇒ Object

Parameters:

  • abort_signal (Concurrent::AtomicBoolean) (defaults to: nil)

    set to true to stop listening for messages



24
25
26
27
28
# File 'lib/debounced/service_proxy.rb', line 24

def listen(abort_signal = nil)
  Thread.new do
    receive(abort_signal)
  end
end

#receive(abort_signal = nil) ⇒ Object

Parameters:

  • abort_signal (Concurrent::AtomicBoolean) (defaults to: nil)

    set to true to stop listening for messages



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
# File 'lib/debounced/service_proxy.rb', line 55

def receive(abort_signal = nil)
  @abort_signal = abort_signal || Concurrent::AtomicBoolean.new
  SemanticLogger.tagged("receive") do
    logger.info { "Listening for messages from #{server_name}..." }

    loop do
      @listening = true
      break if @abort_signal.true?

      message = receive_message_from_server
      next unless message

      payload = deserialize_message(message)
      raise SocketConflictError if payload['type'] == 'rejectClient'

      instantiate_callback(payload['callback']).call
    rescue Debounced::NoServerError => e
      logger_trace { e.message }
      sleep wait_timeout
    end

    close
  end
rescue SocketConflictError, StandardError => e
  logger.warn("Unable to listen for messages from #{server_name}: #{e.message}")
  logger.warn(e.backtrace.join("\n"))
ensure
  @listening = false
end

#reset_serverObject

Send message to server to reset its state. Useful for automated testing.



32
33
34
35
36
37
38
39
# File 'lib/debounced/service_proxy.rb', line 32

def reset_server
  if socket.nil?
    logger.warn("No connection to #{server_name}; unable to reset server.")
  else
    logger_trace { "Resetting #{server_name}" }
    transmit({ type: 'reset' })
  end
end

#stopObject



85
86
87
# File 'lib/debounced/service_proxy.rb', line 85

def stop
  @abort_signal.make_true
end