Class: Minicron::Transport::FayeClient

Inherits:
Object
  • Object
show all
Defined in:
lib/minicron/transport/faye/client.rb

Direct Known Subclasses

Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scheme, host, port, path) ⇒ FayeClient

Instantiate a new instance of the client

Parameters:

  • scheme (String)

    http or https

  • host (String)

    The host to be communicated with

  • port (String)

    The port number the server runs on

  • path (String)

    The path to the server, optional


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/minicron/transport/faye/client.rb', line 18

def initialize(scheme, host, port, path) # TODO: Add options hash for other options
  @scheme = scheme
  @host = host
  @path = path
  @port = port
  @url = "#{scheme}://#{host}:#{port}#{path}"
  @queue = {}
  @responses = []
  @retries = 3
  @retry_counts = {}
end

Instance Attribute Details

#queueObject

Returns the value of attribute queue


9
10
11
# File 'lib/minicron/transport/faye/client.rb', line 9

def queue
  @queue
end

#responsesObject

Returns the value of attribute responses


10
11
12
# File 'lib/minicron/transport/faye/client.rb', line 10

def responses
  @responses
end

#urlObject

Returns the value of attribute url


8
9
10
# File 'lib/minicron/transport/faye/client.rb', line 8

def url
  @url
end

Instance Method Details

#ensure_deliveryObject

Blocks until all messages in the sending queue have completed


86
87
88
89
90
91
92
93
94
95
# File 'lib/minicron/transport/faye/client.rb', line 86

def ensure_delivery
  # Keep waiting until the queue is empty but only if we need to
  if queue.length > 0
    until queue.length == 0
      sleep 0.05
    end
  end

  true
end

#ensure_em_runningObject

Starts EventMachine in a new thread if it isn't already running


31
32
33
34
# File 'lib/minicron/transport/faye/client.rb', line 31

def ensure_em_running
  Thread.new { EM.run } unless EM.reactor_running?
  sleep 0.1 until EM.reactor_running?
end

#request(body) ⇒ Object

Sends a request to the @url and adds it to the request queue

Parameters:

  • body (String)

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
# File 'lib/minicron/transport/faye/client.rb', line 39

def request(body)
  # Make sure eventmachine is running
  ensure_em_running

  # Make the request
  req = EventMachine::HttpRequest.new(
    @url,
    :connect_timeout => Minicron.config['client']['connect_timeout'],
    :inactivity_timeout => Minicron.config['client']['inactivity_timeout']
  ).post(:body => body)

  # Generate an id for the request
  req_id = Digest::MD5.hexdigest(body.to_s)

  # Put the request in the queue
  queue[req_id] = req

  # Set up the retry count for this request if it didn't already exist
  @retry_counts[req_id] ||= 0

  # Did the request succeed? If so remove it from the queue
  req.callback do
    @responses.push(
      :status => req.response_header.status,
      :header => req.response_header,
      :body => req.response
    )

    queue.delete(req_id)
  end

  # If not retry the request up to @retries times
  req.errback do |error|
    @responses.push(
      :status => req.response_header.status,
      :header => req.response_header,
      :body => req.response
    )

    if @retry_counts[req_id] < @retries
      @retry_counts[req_id] += 1
      request(body)
    end
  end
end

#tidy_upObject

Tidy up after we are done with the client


98
99
100
# File 'lib/minicron/transport/faye/client.rb', line 98

def tidy_up
  EM.stop if EM.reactor_running?
end