Skip to content

Instantly share code, notes, and snippets.

@cookrn
Created December 15, 2015 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cookrn/d1ea16c37682c0995714 to your computer and use it in GitHub Desktop.
Save cookrn/d1ea16c37682c0995714 to your computer and use it in GitHub Desktop.
A tiny, standard-library only example of attaching to a running process over the network for inspection
#!/usr/bin/env ruby
require 'drb'
require 'irb'
DRb.start_service
$context = DRbObject.new_with_uri('druby://localhost:9876')
IRB.start
#!/usr/bin/env ruby
require 'drb'
require 'thread'
require 'webrick'
$i = 0
$main = self
$running = false
$semaphore = Mutex.new
module Context
extend self
def i
$i
end
end
Thread.new do
server = WEBrick::HTTPServer.new(Port: 9001)
server.mount_proc '/' do |req, res|
$semaphore.synchronize do
if !$running
$running = true
Thread.new { DRb.start_service('druby://localhost:9876', Context) }
end
end
end
begin
server.start
ensure
server.shutdown
end
end
loop do
$stderr.puts($i)
$stderr.puts($running.inspect)
$i = $i + 1
sleep(rand($i))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment