Module: PryRemoteEm

Defined in:
lib/pry-remote-em/client.rb,
lib/pry-remote-em.rb,
lib/pry-remote-em/proto.rb,
lib/pry-remote-em/broker.rb,
lib/pry-remote-em/server.rb,
lib/pry-remote-em/metrics.rb,
lib/pry-remote-em/sandbox.rb,
lib/pry-remote-em/version.rb,
lib/pry-remote-em/client/proxy.rb,
lib/pry-remote-em/client/broker.rb,
lib/pry-remote-em/client/generic.rb,
lib/pry-remote-em/client/keyboard.rb,
lib/pry-remote-em/server/shell_cmd.rb,
lib/pry-remote-em/client/interactive_menu.rb

Overview

How it works with Pry

When PryRemoteEm.run is called it registers with EventMachine for a given ip and port. When a connection is received EM yields an instance of PryRemoteEm, we start a Fiber (f1) then call Pry.start specifying the Server instance as the input and output object for Pry. The Pry instance that is created goes into its REPL. When it gets to the read part it calls @input.readline (PryRemoteEm#readline) and passes a prompt, e.g., [1] pry(#<Foo>)>.

PryRemoteEm#readline calls #send_data with the prompt then yields from the current Fiber (f1); the one we started when EventMachine yielded to us. The root Fiber is now active again. At some point, the root Fiber receives data from the client. It calls #receive_data in our Server instance. That instance then resumes the Fiber (f1) that was waiting for #readline to finish.

Inside the resumed Fiber (f1) PryRemoteEm#readline returns the recieved data to the instance of Pry, which continues its REPL. Pry calls #puts, or #print or #write on its output object (PryRemoteEm). We send that data out to the client and immediately return. Pry then calls PryRemoteEm#readline again and again we send the prompt then immediately yield back to the root Fiber.

Pry just interacts with PryRemoteEm as if it were any other blocking Readline object. The important bit is making sure that it is started in a new Fiber that can be paused and resumed as needed. PryRemoteEm#readline pauses it and PryRemoteEm#receive_raw resumes it.

Reference: www.igvita.com/2010/03/22/untangling-evented-code-with-ruby-fibers/

Defined Under Namespace

Modules: Broker, Client, Metrics, Proto, Server Classes: Sandbox

Constant Summary collapse

DEFAULT_SERVER_HOST =
'127.0.0.1'
DEFAULT_SERVER_PORT =
6463
DEFAULT_BROKER_HOST =
'127.0.0.1'
DEFAULT_BROKER_PORT =
6462
NEGOTIATION_TIMEOUT =
15
HEARTBEAT_SEND_INTERVAL =
15
HEARTBEAT_CHECK_INTERVAL =
20
RECONNECT_TO_BROKER_TIMEOUT =
3
MAXIMUM_ERRORS_IN_SANDBOX =
100
VERSION =
'1.1.2'

Class Method Summary collapse

Class Method Details

.serversObject

Local PryRemoteEm servers, including EM signatures, indexed by id. Each signature can be used with high level EM methods like EM.stop_server or EM.get_sockname. If a server has been stopped EM.get_sockname will return nil for that server’s signature.



44
45
46
# File 'lib/pry-remote-em/server.rb', line 44

def servers
  @servers ||= {}
end

.stop_server(argument = nil) ⇒ Hash

Safely stop one or more PryRemoteEm servers and remove them from the list of servers.

Parameters:

  • argument (String, nil) (defaults to: nil)

    id, url or name, use ‘nil` to stop them all

Returns:

  • (Hash)

    stopped servers if they were



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pry-remote-em/server.rb', line 52

def stop_server(argument = nil)
  servers_to_stop = if argument
    servers.select do |id, description|
      argument == id || description[:urls].include?(argument) || argument == description[:name]
    end
  else
    servers
  end

  servers_to_stop.each do |id, description|
    EM.stop_server(description[:server]) if EM.get_sockname(description[:server])
    description[:heartbeat_timer].cancel
    Broker.unregister(id)
    servers.delete(id)
  end
end