pry-em 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.MIT +19 -0
- data/README.markdown +44 -0
- data/lib/pry-em.rb +79 -0
- metadata +95 -0
data/LICENSE.MIT
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2011 Conrad Irwin <conrad.irwin@gmail.com>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Introduction
|
2
|
+
|
3
|
+
`pry-em` is a plugin for the Ruby shell [pry](http://pry.github.com) which allows you to poke around with objects in [EventMachine](http://rubyeventmachine.com). It's designed to make playing with async stuff as easy as pry!
|
4
|
+
|
5
|
+
## How to use it?
|
6
|
+
|
7
|
+
As with all pry plugins, you can just:
|
8
|
+
|
9
|
+
gem install pry-em
|
10
|
+
|
11
|
+
Then, any time you want to run a command that returns a deferrable, prefix it by `em:`. For example:
|
12
|
+
|
13
|
+
pry(main)> em: EM::HttpRequest.new("http://example.com/").get
|
14
|
+
|
15
|
+
This does two things. Firstly, before running your command, it ensures that EventMachine is running. And Secondly, it sits and waits for your deferrable to succeed or fail before returning the result to you in `_`
|
16
|
+
|
17
|
+
pry(main)> em: EM::HttpRequest.new("http://example.com/").get
|
18
|
+
callback => #<EventMachine::HttpClient:0x25b36f8
|
19
|
+
@bytes_remaining=0,
|
20
|
+
…
|
21
|
+
|
22
|
+
pry(main)> em: EM::HttpRequest.new("http://examplefail/").get
|
23
|
+
errback => #<EventMachine::HttpClient:0x25b36f8
|
24
|
+
@bytes_remaining=0,
|
25
|
+
@error="unable to resolve server address",
|
26
|
+
…
|
27
|
+
|
28
|
+
If your deferrable takes a loong time to succeed or fail (where a long time is "more than 3 seconds" by default), a timeout error will be raised. You can configure the length of the timeout by putting a number of seconds into the prefix:
|
29
|
+
|
30
|
+
pry(main)> em 10: EM::HttpRequest.new("https://slow.domain.example.com/").get
|
31
|
+
RuntimeError: Timeout after 10 seconds
|
32
|
+
from /0/ruby/pry-em/lib/pry-em.rb:71:in `wait_for_deferrable'
|
33
|
+
|
34
|
+
## How does it work? (aka. it's broken, why?!)
|
35
|
+
|
36
|
+
The basic magic is to boot the EM reactor into a different thread from that which is running the Pry shell. When you want to run something that requires the reactor, we send it across to the eventmachine thread, and then enter a simple "sleep until done" loop.
|
37
|
+
|
38
|
+
Unfortunately it's possibly (and in fact quite easy) to boot Pry into a thread which is already running the EM reactor. In this case, if we were to enter the "sleep until done" loop, we'd never make any progress, as the stuff that needed doing would be waiting for us to stop sleeping. If `pry-em` notices that this has happened, it will raise an Exception with a friendly message.
|
39
|
+
|
40
|
+
## Metafoo
|
41
|
+
|
42
|
+
`pry-em` is licensed under the MIT license, see LICENSE.MIT for details. Bug-reports, feature-requests and patches are most welcome.
|
43
|
+
|
44
|
+
I'm indebted to @samstokes for many of the ideas behind `pry-em`.
|
data/lib/pry-em.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
EmCommands = Pry::CommandSet.new do
|
2
|
+
|
3
|
+
EM_DESCRIPTION = "Wait for a deferrable to succeed or fail, a timeout can be specified before the colon."
|
4
|
+
EM_CONFIG = {
|
5
|
+
:keep_retval => true,
|
6
|
+
:interpolate => false,
|
7
|
+
:listing => "em[timeout=3]:",
|
8
|
+
:requires_gem => 'eventmachine'
|
9
|
+
}
|
10
|
+
|
11
|
+
command /\s*em\s*([0-9\.]*)\s*:(.*)/, EM_DESCRIPTION, EM_CONFIG do |timeout, source|
|
12
|
+
|
13
|
+
# Boot EM before eval'ing the source as it's likely to depend on the reactor.
|
14
|
+
run_em_if_necessary!
|
15
|
+
|
16
|
+
# This can happen for example if you do:
|
17
|
+
# em: EM::HttpRequest.new("http://www.google.com/").get.callback{ binding.pry }
|
18
|
+
# There ought to be a solution, but it will involve shunting either Pry or EM
|
19
|
+
# onto a new thread.
|
20
|
+
if EM.reactor_thread == Thread.current
|
21
|
+
raise "Could not wait for deferrable, you're in the EM thread!
|
22
|
+
If you don't know what to do, try `cd ..`, or just hit ctrl-C until it dies."
|
23
|
+
end
|
24
|
+
|
25
|
+
deferrable = target.eval(source)
|
26
|
+
|
27
|
+
# TODO: Allow the user to configure the default timeout
|
28
|
+
timeout = timeout == "" ? 3 : Float(timeout)
|
29
|
+
|
30
|
+
wait_for_deferrable(deferrable, timeout) unless deferrable.nil?
|
31
|
+
end
|
32
|
+
|
33
|
+
helpers do
|
34
|
+
# Boot a new EventMachine reactor into another thread.
|
35
|
+
# This allows us to continue to interact with the user on the front-end thread,
|
36
|
+
# while they run event-machine commands in the background.
|
37
|
+
def run_em_if_necessary!
|
38
|
+
require 'eventmachine' unless defined?(EM)
|
39
|
+
Thread.new{ EM.run } unless EM.reactor_running?
|
40
|
+
sleep 0.01 until EM.reactor_running?
|
41
|
+
end
|
42
|
+
|
43
|
+
# Run a deferrable on an EM reactor in a different thread,
|
44
|
+
# sleep until it has finished, and then return the result.
|
45
|
+
#
|
46
|
+
# The result is defined to be as useful to an interactive shell user as possible:
|
47
|
+
#
|
48
|
+
# If the deferrable succeeds or fails with one argument, that one argument is
|
49
|
+
# returned; though if it succeeds or fails with many arguments, an array is returned.
|
50
|
+
#
|
51
|
+
def wait_for_deferrable(deferrable, timeout)
|
52
|
+
|
53
|
+
retval, finished = nil
|
54
|
+
|
55
|
+
EM::Timer.new(timeout) { finished ||= :timeout }
|
56
|
+
|
57
|
+
[:callback, :errback].each do |method|
|
58
|
+
begin
|
59
|
+
deferrable.__send__ method do |*result|
|
60
|
+
finished = method
|
61
|
+
retval = result.size > 1 ? result : result.first
|
62
|
+
end
|
63
|
+
rescue NoMethodError
|
64
|
+
output.warn "WARNING: is not deferrable? #{deferrable}"
|
65
|
+
break
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
sleep 0.01 until finished
|
70
|
+
|
71
|
+
raise "Timeout after #{timeout} seconds" if finished == :timeout
|
72
|
+
|
73
|
+
# TODO: This doesn't interact well with the pager.
|
74
|
+
output.print "#{finished} " #=>
|
75
|
+
retval
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
Pry.config.commands.import EmCommands
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pry-em
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 9
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: "0.1"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Conrad Irwin
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-07-24 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: pry
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: eventmachine
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
description: em! is a synchronous wrapper around deferrables, so that you can interact with them as though they were normal function calls.
|
49
|
+
email: conrad.irwin@gmail.com
|
50
|
+
executables: []
|
51
|
+
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files: []
|
55
|
+
|
56
|
+
files:
|
57
|
+
- lib/pry-em.rb
|
58
|
+
- README.markdown
|
59
|
+
- LICENSE.MIT
|
60
|
+
has_rdoc: true
|
61
|
+
homepage: http://github.com/ConradIrwin/pry-em
|
62
|
+
licenses: []
|
63
|
+
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
hash: 3
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
requirements: []
|
88
|
+
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 1.6.2
|
91
|
+
signing_key:
|
92
|
+
specification_version: 3
|
93
|
+
summary: Provides an em! function that can be used to play with deferrable more easily.
|
94
|
+
test_files: []
|
95
|
+
|