asir 0.2.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. data/Gemfile +1 -2
  2. data/README.textile +4 -2
  3. data/VERSION +1 -1
  4. data/asir.gemspec +1 -4
  5. data/asir.riterate.yml +1 -0
  6. data/bin/asir +2 -1
  7. data/example/asir_control.sh +63 -1
  8. data/example/asir_control_client_http.rb +2 -2
  9. data/example/asir_control_client_resque.rb +16 -0
  10. data/example/asir_control_client_zmq.rb +3 -3
  11. data/example/config/asir_config.rb +20 -8
  12. data/example/ex02.rb +1 -1
  13. data/example/ex03.rb +2 -2
  14. data/example/ex04.rb +2 -2
  15. data/example/ex05.rb +1 -1
  16. data/example/ex06.rb +6 -5
  17. data/example/ex07.rb +2 -2
  18. data/example/ex08.rb +2 -2
  19. data/example/ex09.rb +2 -2
  20. data/example/ex10.rb +2 -2
  21. data/example/ex11.rb +5 -5
  22. data/example/ex12.rb +6 -6
  23. data/example/ex13.rb +4 -4
  24. data/example/ex14.rb +4 -4
  25. data/example/ex15.rb +2 -2
  26. data/example/ex16.rb +8 -8
  27. data/example/ex17.rb +12 -11
  28. data/example/ex18.rb +5 -5
  29. data/example/ex19.rb +3 -3
  30. data/example/ex20.rb +3 -3
  31. data/example/ex21.rb +3 -3
  32. data/example/ex22.rb +1 -1
  33. data/example/ex23.rb +2 -2
  34. data/example/ex24.rb +4 -4
  35. data/example/ex25.rb +41 -0
  36. data/example/example_helper.rb +38 -3
  37. data/example/sample_service.rb +4 -4
  38. data/hack_night/exercise/prob-3.rb +3 -3
  39. data/hack_night/exercise/prob-6.rb +2 -2
  40. data/hack_night/exercise/prob-7.rb +2 -2
  41. data/hack_night/solution/prob-2.rb +2 -2
  42. data/hack_night/solution/prob-3.rb +3 -3
  43. data/hack_night/solution/prob-6.rb +7 -6
  44. data/hack_night/solution/prob-7.rb +6 -6
  45. data/{spec → lab}/const_get_speed_spec.rb +0 -0
  46. data/lib/asir.rb +29 -7
  47. data/lib/asir/additional_data.rb +25 -0
  48. data/lib/asir/channel.rb +4 -5
  49. data/lib/asir/client.rb +29 -13
  50. data/lib/asir/config.rb +8 -0
  51. data/lib/asir/description.rb +34 -0
  52. data/lib/asir/environment.rb +96 -0
  53. data/lib/asir/error.rb +4 -1
  54. data/lib/asir/invoker.rb +14 -0
  55. data/lib/asir/main.rb +84 -103
  56. data/lib/asir/message.rb +1 -1
  57. data/lib/asir/poll_throttle.rb +53 -0
  58. data/lib/asir/retry_behavior.rb +1 -1
  59. data/lib/asir/thread_variable.rb +183 -0
  60. data/lib/asir/transport.rb +36 -23
  61. data/lib/asir/transport/beanstalk.rb +18 -52
  62. data/lib/asir/transport/conduit.rb +42 -0
  63. data/lib/asir/transport/connection_oriented.rb +32 -56
  64. data/lib/asir/transport/delegation.rb +5 -5
  65. data/lib/asir/transport/demux.rb +33 -0
  66. data/lib/asir/transport/file.rb +5 -3
  67. data/lib/asir/transport/payload_io.rb +8 -4
  68. data/lib/asir/transport/resque.rb +212 -0
  69. data/lib/asir/transport/stream.rb +19 -9
  70. data/lib/asir/transport/tcp_socket.rb +3 -2
  71. data/lib/asir/transport/zmq.rb +14 -17
  72. data/lib/asir/uri_config.rb +51 -0
  73. data/lib/asir/version.rb +1 -1
  74. data/spec/client_spec.rb +48 -0
  75. data/spec/demux_spec.rb +38 -0
  76. data/spec/json_spec.rb +0 -2
  77. data/spec/message_spec.rb +68 -0
  78. data/spec/performance_spec.rb +66 -0
  79. data/spec/spec_helper.rb +34 -0
  80. data/spec/thread_variable_spec.rb +135 -0
  81. data/spec/transport_spec.rb +82 -0
  82. metadata +28 -4
data/example/ex12.rb CHANGED
@@ -5,25 +5,25 @@ require 'example_helper'
5
5
  require 'asir/transport/beanstalk'
6
6
  require 'asir/coder/zlib'
7
7
  begin
8
- Email.client.transport = t =
8
+ Email.asir.transport = t =
9
9
  ASIR::Transport::Beanstalk.new(:address => '127.0.0.1', :port => 30904)
10
10
  t.encoder =
11
11
  ASIR::Coder::Chain.new(:encoders =>
12
12
  [ ASIR::Coder::Marshal.new,
13
13
  ASIR::Coder::Zlib.new, ])
14
- t.start_beanstalkd!; sleep 1
15
- pr Email.client.send_email(:pdf_invoice,
14
+ t.start_conduit!; sleep 1
15
+ pr Email.asir.send_email(:pdf_invoice,
16
16
  :to => "user@email.com", :customer => @customer)
17
17
  sleep 2
18
18
  server_process do
19
- t.prepare_beanstalk_server!
20
- t.run_beanstalk_server!
19
+ t.prepare_server!
20
+ t.run_server!
21
21
  end
22
22
  rescue Object => err
23
23
  $stderr.puts "#{err.inspect}\n#{err.backtrace * "\n"}"
24
24
  ensure
25
25
  t.close; sleep 3; server_kill; sleep 2
26
- t.stop_beanstalkd!
26
+ t.stop_conduit!
27
27
  end
28
28
 
29
29
  # !SLIDE END
data/example/ex13.rb CHANGED
@@ -6,7 +6,7 @@ require 'asir/transport/webrick'
6
6
  require 'asir/coder/base64'
7
7
  require 'asir/coder/zlib'
8
8
  begin
9
- Email.client.transport = t =
9
+ Email.asir.transport = t =
10
10
  ASIR::Transport::Webrick.new(:uri => "http://localhost:31913/")
11
11
  t.encoder =
12
12
  ASIR::Coder::Chain.new(:encoders =>
@@ -15,8 +15,8 @@ begin
15
15
  server_process do
16
16
  t.prepare_server!
17
17
  t.run_server!
18
- end; sleep 2
19
- pr Email.client.send_email(:pdf_invoice,
18
+ end
19
+ pr Email.asir.send_email(:pdf_invoice,
20
20
  :to => "user@email.com",
21
21
  :customer => @customer)
22
22
  sleep 2
@@ -24,7 +24,7 @@ rescue Object => err
24
24
  $stderr.puts "#{err.inspect}\n#{err.backtrace * "\n"}"
25
25
  ensure
26
26
  t.close rescue nil; sleep 3
27
- server_kill; sleep 2
27
+ server_kill
28
28
  end
29
29
 
30
30
  # !SLIDE END
data/example/ex14.rb CHANGED
@@ -5,7 +5,7 @@ require 'example_helper'
5
5
  require 'asir/transport/webrick'
6
6
  require 'asir/coder/base64'
7
7
  begin
8
- MyClass.client.transport = t =
8
+ MyClass.asir.transport = t =
9
9
  ASIR::Transport::Webrick.new(:uri => "http://localhost:30914/")
10
10
  t.encoder =
11
11
  ASIR::Coder::Chain.new(:encoders =>
@@ -14,13 +14,13 @@ begin
14
14
  server_process do
15
15
  t.prepare_server!
16
16
  t.run_server!
17
- end; sleep 2
18
- pr MyClass.new("abc123").client.size
17
+ end
18
+ pr MyClass.new("abc123").asir.size
19
19
  sleep 2
20
20
  rescue Object => err
21
21
  $stderr.puts "#{err.inspect}\n#{err.backtrace * "\n"}"
22
22
  ensure
23
- t.close; sleep 3; server_kill; sleep 2
23
+ t.close; sleep 3; server_kill
24
24
  end
25
25
 
26
26
  # !SLIDE END
data/example/ex15.rb CHANGED
@@ -3,8 +3,8 @@
3
3
 
4
4
  require 'example_helper'
5
5
 
6
- pr DelayedService.client.
7
- _configure{|req| req.delay = 5}.
6
+ pr DelayedService.asir.
7
+ _configure{|req, p| req.delay = 5}.
8
8
  do_it(Time.now)
9
9
 
10
10
  # !SLIDE END
data/example/ex16.rb CHANGED
@@ -4,17 +4,17 @@
4
4
  require 'example_helper'
5
5
  require 'asir/transport/beanstalk'
6
6
  begin
7
- DelayedService.client.transport = t =
7
+ DelayedService.asir.transport = t =
8
8
  ASIR::Transport::Beanstalk.new(:address => '127.0.0.1', :port => 30916)
9
9
  t.encoder =
10
10
  ASIR::Coder::Marshal.new
11
- t.start_beanstalkd!; sleep 1
11
+ t.start_conduit!; sleep 1
12
12
  server_process do
13
- t.prepare_beanstalk_server!
14
- t.run_beanstalk_server!
15
- end; sleep 1
16
- pr DelayedService.client.
17
- _configure{|req| req.delay = 5}.
13
+ t.prepare_server!
14
+ t.run_server!
15
+ end
16
+ pr DelayedService.asir.
17
+ _configure{|req, p| req.delay = 5}.
18
18
  do_it(Time.now)
19
19
  sleep 10
20
20
  rescue Object => err
@@ -22,7 +22,7 @@ rescue Object => err
22
22
  ensure
23
23
  t.close; sleep 1
24
24
  server_kill; sleep 1
25
- t.stop_beanstalkd!
25
+ t.stop_conduit!
26
26
  end
27
27
 
28
28
  # !SLIDE END
data/example/ex17.rb CHANGED
@@ -9,28 +9,29 @@ begin
9
9
  ASIR::Transport::Beanstalk.new(:address => '127.0.0.1', :port => 30917)
10
10
  t.encoder =
11
11
  ASIR::Coder::Marshal.new
12
- t.start_beanstalkd!; sleep 1
13
- DelayedService.client.transport =
12
+ t.start_conduit!; sleep 1
13
+ DelayedService.asir.transport =
14
14
  t0 = ASIR::Transport::Buffer.new(:transport => t)
15
15
  t0.pause!
16
16
  server_process do
17
- t.prepare_beanstalk_server!
18
- t.run_beanstalk_server!
19
- end; sleep 1
20
- pr [ :paused, t0.paused?, :at, Time.now.iso8601(2) ]
21
- pr DelayedService.client.
22
- _configure{|req| req.delay = 5}.
17
+ t.prepare_server!
18
+ t.run_server!
19
+ end
20
+ pr [ :paused?, t0.paused?, :at, Time.now.iso8601(2) ]
21
+ pr DelayedService.asir.
22
+ _configure{|req, p| req.delay = 5}.
23
23
  do_it(Time.now)
24
24
  sleep 2
25
25
  pr [ :resuming, :size, t0.size, :at, Time.now.iso8601(2) ]
26
26
  t0.resume!
27
- pr [ :paused, t0.paused?, :size, t0.size, :at, Time.now.iso8601(2) ]
28
- sleep 10
27
+ pr [ :paused?, t0.paused?, :size, t0.size, :at, Time.now.iso8601(2) ]
28
+ pr [ :resumed, :size, t0.size, :at, Time.now.iso8601(2) ]
29
+ sleep 7
29
30
  rescue Object => err
30
31
  $stderr.puts "#{err.inspect}\n#{err.backtrace * "\n"}"
31
32
  ensure
32
33
  t.close; sleep 1; server_kill; sleep 1
33
- t.stop_beanstalkd!
34
+ t.stop_conduit!
34
35
  end
35
36
 
36
37
  # !SLIDE END
data/example/ex18.rb CHANGED
@@ -15,19 +15,19 @@ begin
15
15
  server_process do
16
16
  tcp.prepare_server!
17
17
  tcp.run_server!
18
- end; sleep 2
18
+ end
19
19
  end
20
- Email.client.transport = t =
20
+ Email.asir.transport = t =
21
21
  ASIR::Transport::Retry.new(:transport => tcp,
22
22
  :try_sleep => 1,
23
23
  :try_sleep_increment => 2,
24
24
  :try_max => 3,
25
25
  :before_retry => start_server_proc
26
26
  )
27
- pr Email.client.send_email(:pdf_invoice,
27
+ pr Email.asir.send_email(:pdf_invoice,
28
28
  :to => "user@email.com", :customer => 123)
29
29
  sleep 1
30
- pr Email.client.send_email(:pdf_invoice,
30
+ pr Email.asir.send_email(:pdf_invoice,
31
31
  :to => "user2@email.com", :customer => 456)
32
32
  sleep 1
33
33
  rescue ::Exception => err
@@ -50,7 +50,7 @@ end
50
50
  # EXPECT: service.log" contents:
51
51
  # EXPECT: --- !ruby/object:ASIR::Message
52
52
  # EXPECT: :transport_exceptions:
53
- # EXPECT: ASIR::Error: Cannot connect to ASIR::Transport::TcpSocket tcp://127.0.0.1:
53
+ # EXPECT: Cannot connect to ASIR::Transport::TcpSocket tcp://127.0.0.1:
54
54
  # EXPECT: arguments:
55
55
  # EXPECT: - :pdf_invoice
56
56
  # EXPECT/: :to: user@email.com
data/example/ex19.rb CHANGED
@@ -8,9 +8,9 @@ begin
8
8
  server_process do
9
9
  tcp.prepare_server!
10
10
  tcp.run_server!
11
- end; sleep 2
12
- UnsafeService.client.transport = t = tcp
13
- pr UnsafeService.client.do_it("exit 999; :ok")
11
+ end
12
+ UnsafeService.asir.transport = t = tcp
13
+ pr UnsafeService.asir.do_it("exit 999; :ok")
14
14
  sleep 1
15
15
  rescue ::ASIR::Error::Unforwardable => err
16
16
  $stderr.puts "### #{$$}: Unforwardable ERROR: #{err.inspect}}"
data/example/ex20.rb CHANGED
@@ -10,9 +10,9 @@ begin
10
10
  server_process do
11
11
  zmq.prepare_server!
12
12
  zmq.run_server!
13
- end; sleep 1
14
- UnsafeService.client.transport = t = zmq
15
- pr UnsafeService.client.do_it(":ok")
13
+ end
14
+ UnsafeService.asir.transport = t = zmq
15
+ pr UnsafeService.asir.do_it(":ok")
16
16
  rescue ::Exception => err
17
17
  $stderr.puts "### #{$$}: ERROR: #{err.inspect}\n #{err.backtrace * "\n "}"
18
18
  raise
data/example/ex21.rb CHANGED
@@ -10,9 +10,9 @@ begin
10
10
  server_process do
11
11
  zmq.prepare_server!
12
12
  zmq.run_server!
13
- end; sleep 1
14
- UnsafeService.client.transport = t = zmq
15
- pr UnsafeService.client.do_it(":ok")
13
+ end
14
+ UnsafeService.asir.transport = t = zmq
15
+ pr UnsafeService.asir.do_it(":ok")
16
16
  rescue ::Exception => err
17
17
  $stderr.puts "### #{$$}: ERROR: #{err.inspect}\n #{err.backtrace * "\n "}"
18
18
  raise
data/example/ex22.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  # In-core, in-process service, with continuation block.
3
3
 
4
4
  require 'example_helper'
5
- pr(Email.client.send_email(:pdf_invoice,
5
+ pr(Email.asir.send_email(:pdf_invoice,
6
6
  :to => "user@email.com",
7
7
  :customer => @customer,
8
8
  &proc { | res | pr [ :in_block, res.result ] })
data/example/ex23.rb CHANGED
@@ -4,9 +4,9 @@
4
4
 
5
5
  require 'example_helper'
6
6
  begin
7
- Email.client.transport = t =
7
+ Email.asir.transport = t =
8
8
  ASIR::Transport::Subprocess.new(:one_way => true)
9
- pr(Email.client.send_email(:pdf_invoice,
9
+ pr(Email.asir.send_email(:pdf_invoice,
10
10
  :to => "user@email.com",
11
11
  :customer => @customer) { | resp |
12
12
  pr [ :in_block, resp.result ] })
data/example/ex24.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  # !SLIDE :capture_code_output true
2
2
  # Synchronous HTTP service on Rack under WEBrick
3
3
 
4
- gem 'rack'
5
4
  require 'example_helper'
5
+ gem 'rack'
6
6
  require 'asir/transport/rack'
7
7
  require 'asir/coder/base64'
8
8
  begin
9
- Email.client.transport = t =
9
+ Email.asir.transport = t =
10
10
  ASIR::Transport::Rack.new(:uri => "http://localhost:31924/")
11
11
  t.encoder =
12
12
  ASIR::Coder::Chain.new(:encoders =>
@@ -15,8 +15,8 @@ begin
15
15
  server_process do
16
16
  t.prepare_server!
17
17
  t.run_server!
18
- end; sleep 2
19
- pr Email.client.send_email(:pdf_invoice,
18
+ end
19
+ pr Email.asir.send_email(:pdf_invoice,
20
20
  :to => "user@email.com",
21
21
  :customer => @customer)
22
22
  sleep 2
data/example/ex25.rb ADDED
@@ -0,0 +1,41 @@
1
+ # !SLIDE :capture_code_output true
2
+ # One-way Resque service.
3
+
4
+ require 'rubygems'
5
+ gem 'resque'
6
+ require 'example_helper'
7
+ require 'asir/transport/resque'
8
+ begin
9
+ t = ASIR::Transport::Resque.new(:port => 31925,
10
+ :encoder => ASIR::Coder::Marshal.new)
11
+ # Control throttling of Resque::Worker inside ASIR::Transport::Resque
12
+ t.throttle = {
13
+ # :verbose => true,
14
+ :min_sleep => 0.0,
15
+ :max_sleep => 2.0,
16
+ :inc_sleep => 0.1,
17
+ :mul_sleep => 1.5,
18
+ :rand_sleep => 0.1,
19
+ }
20
+ t.start_conduit!; sleep 1
21
+ server_process do
22
+ t.prepare_server!
23
+ t.run_server!
24
+ end
25
+ UnsafeService.asir.transport = t
26
+ pr UnsafeService.asir.do_it(":ok")
27
+ rescue ::Exception => err
28
+ $stderr.puts "### #{$$}: ERROR: #{err.inspect}\n #{err.backtrace * "\n "}"
29
+ raise
30
+ ensure
31
+ sleep 5
32
+ t.close rescue nil; sleep 1; server_kill
33
+ t.stop_conduit!
34
+ end
35
+
36
+ # !SLIDE END
37
+ # EXPECT: : client process
38
+ # EXPECT: : server process
39
+ # EXPECT: UnsafeService.do_it => :ok
40
+ # EXPECT: : pr: nil
41
+ # EXPECT!: ERROR
@@ -1,5 +1,10 @@
1
1
  # Sample client support
2
2
  #
3
+ require 'rubygems'
4
+ case RUBY_PLATFORM
5
+ when /java/i
6
+ gem 'spoon'; require 'spoon'
7
+ end
3
8
 
4
9
  $: << File.expand_path("../../lib", __FILE__)
5
10
  require 'asir'
@@ -30,11 +35,39 @@ def pr result
30
35
  end
31
36
 
32
37
  def server_process &blk
33
- $server_pid = Process.fork do
34
- puts "*** #{$$}: server process"; $stdout.flush
35
- yield
38
+ # $stderr.puts " at #{__FILE__}:#{__LINE__}"
39
+ case RUBY_PLATFORM
40
+ when /java/i
41
+ # JRuby cannot fork.
42
+ # So we must prevent spawn a new jruby and
43
+ # instruct it to only run the server blk, and not
44
+ # the subsequent client code.
45
+ # In other words, we cannot rely on how Process.fork
46
+ # terminates within the block.
47
+ if ENV['ASIR_JRUBY_SPAWNED']
48
+ $stderr.puts " spawned server at #{__FILE__}:#{__LINE__}"
49
+ puts "*** #{$$}: server process"; $stdout.flush
50
+ yield
51
+ Process.exit!(0)
52
+ # dont do client, client is our parent process.
53
+ else
54
+ $stderr.puts " spawning at #{__FILE__}:#{__LINE__}"
55
+ ENV['ASIR_JRUBY_SPAWNED'] = "1"
56
+ cmd = "ruby -I #{File.dirname(__FILE__)} -I #{File.expand_path('../../lib', __FILE__)} #{$0} #{ARGV * ' '}"
57
+ $stderr.puts " cmd = #{cmd}"
58
+ $server_pid = Spoon.spawnp(cmd)
59
+ ENV.delete('ASIR_JRUBY_SPAWNED')
60
+ $stderr.puts " spawned #{$server_pid} at #{__FILE__}:#{__LINE__}"
61
+ end
62
+ else
63
+ # $stderr.puts " at #{__FILE__}:#{__LINE__}"
64
+ $server_pid = Process.fork do
65
+ puts "*** #{$$}: server process"; $stdout.flush
66
+ yield
67
+ end
36
68
  end
37
69
  sleep 1 # wait for server to be ready.
70
+ return false # do client.
38
71
  end
39
72
 
40
73
  def server_kill
@@ -42,6 +75,8 @@ def server_kill
42
75
  Process.kill 9, $server_pid
43
76
  Process.waitpid($server_pid)
44
77
  end
78
+ rescue Errno::ESRCH
79
+ ensure
45
80
  $server_pid = nil
46
81
  end
47
82
 
@@ -89,7 +89,7 @@ end
89
89
  # !SLIDE
90
90
  # Example Message
91
91
  #
92
- Email.client.send_email(:pdf_invoice,
92
+ Email.asir.send_email(:pdf_invoice,
93
93
  :to => "user@email.com",
94
94
  :customer => @customer)
95
95
  # ->
@@ -109,7 +109,7 @@ Email.send_email(:pdf_invoice,
109
109
  :to => "user@email.com",
110
110
  :customer => @customer)
111
111
  # ->
112
- Email.client.
112
+ Email.asir.
113
113
  send_email(:pdf_invoice,
114
114
  :to => "user@email.com",
115
115
  :customer => @customer)
@@ -133,9 +133,9 @@ ee.exception_backtrace = [ ... ]
133
133
  #
134
134
 
135
135
  require 'asir'
136
- # Added .client support.
136
+ # Added .asir support.
137
137
  module Email
138
- include ASIR::Client # Email.client
138
+ include ASIR::Client # Email.asir
139
139
  def send_email template_name, options
140
140
  $stderr.puts "*** #{$$}: Email.send_mail #{template_name.inspect} #{options.inspect}"
141
141
  :ok
@@ -10,7 +10,7 @@ MathService.send(:include, ASIR::Client)
10
10
  # Driver:
11
11
 
12
12
  begin
13
- MathService.client.transport = # ???
14
- MathService.client.transport._log_enabled = true
15
- puts MathService.client.sum([1, 2, 3])
13
+ MathService.asir.transport = # ???
14
+ MathService.asir.transport._log_enabled = true
15
+ puts MathService.asir.sum([1, 2, 3])
16
16
  end