Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Deduplicated example

  • Loading branch information...
commit 3c5c37aa08f0242f7831c5d1550b9b9375ddc79d 1 parent 252f2de
Markus Schirp authored
View
31 example/client
@@ -1,32 +1,13 @@
#!/usr/bin/env ruby
-$: << File.expand_path(File.join(File.dirname(__FILE__),'..','lib'))
-require 'rz/client'
+require './example'
-class Client
- include RZ::Client
+options = [:a,:b:c].map { |name| [name,Example.addresses.fetch(:name).fetch(:frontend_address)] }.to_hash
+options[:identity] = "client-#{Process.pid}"
- def run
- yield self
- ensure
- zmq_cleanup
- end
-
- def initialize
- initialize_client(
- :services => {
- :service_a => "tcp://127.0.0.1:4000",
- :service_b => "tcp://127.0.0.1:4010",
- :service_c => "tcp://127.0.0.1:4020"
- },
- :identity => "client.#{Process.pid}"
- )
- end
-end
-
-Client.new.run do |client|
- %w(service_a service_b service_c).map(&:to_sym).each do |service|
- puts 'executing 100 asynchronous requests'
+Client.new(options).run do |client|
+ options.keys.each do |service|
+ puts "executing 100 asynchronous requests to service #{service}"
100.times do |i|
client.request service, { :name => :eval, :arguments => ["sleep 1; #{i}"], :job_id => i}
View
95 example/example.rb
@@ -0,0 +1,95 @@
+#!/usr/bin/env ruby
+
+$: << File.expand_path(File.join(File.dirname(__FILE__),'..','lib'))
+
+require 'rz/client'
+require 'rz/service'
+require 'rz/worker'
+require 'rz/service/statistics'
+
+class Client
+ include RZ::Client
+
+ # overriding log noop log, this interface needs to improve
+ def log(level)
+ puts yield
+ end
+
+ def run
+ yield self
+ ensure
+ zmq_cleanup
+ end
+
+ def initialize(options)
+ initialize_client options
+ end
+end
+
+
+class Service
+ include RZ::Service
+ include RZ::Service::Statistics
+
+ hook :before_run do
+ puts 'hello from hook'
+ end
+
+ # overriding log noop log, this interface needs to improve
+ def log(level)
+ puts yield
+ end
+
+ def initialize(options)
+ initialize_service options
+ end
+end
+
+
+class Worker
+ include RZ::Worker
+
+ def initialize(options)
+ initialize_worker(options)
+ end
+
+ # overriding log noop log, this interface needs to improve
+ def log(level)
+ puts yield
+ end
+
+ register :eval do |string|
+ eval string
+ end
+end
+
+module Example
+ def self.addresses
+ {
+ :a => {
+ :response_address => 'tcp://127.0.0.1:4001',
+ :request_address_a => 'tcp://127.0.0.1:4002',
+ :request_address_b => 'tcp://127.0.0.1:4003',
+ :frontend_address => 'tcp://127.0.0.1:4000'
+ },
+ :b => {
+ :response_address => 'tcp://127.0.0.1:4010',
+ :request_address_a => 'tcp://127.0.0.1:4020',
+ :request_address_b => 'tcp://127.0.0.1:4030',
+ :frontend_address => 'tcp://127.0.0.1:4000'
+ },
+ :c => {
+ :response_address => 'tcp://127.0.0.1:4100',
+ :request_address_a => 'tcp://127.0.0.1:4200',
+ :request_address_b => 'tcp://127.0.0.1:4300',
+ :frontend_address => 'tcp://127.0.0.1:4000'
+ },
+ }
+ end
+
+ def self.options_for(type,name)
+ addresses = self.addresses.fetch(name) { raise ArgumentError,"address for: #{name} does not exist" }
+ addresses.merge :identity => "#{type}-#{name}-#{Process.pid}"
+ end
+end
+
View
10 example/service
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+
+require './example'
+
+name = ARGV[0] || raise("Usage ./service {a,b,c}")
+
+
+options = Example.options_for(:service,name.to_sym)
+
+Service.new(options).run
View
26 example/service-a
@@ -1,26 +0,0 @@
-#!/usr/bin/env ruby
-$: << File.expand_path(File.join(File.dirname(__FILE__),'..','lib'))
-
-require 'rz/service'
-require 'rz/service/statistics'
-
-class Service
- include RZ::Service
- include RZ::Service::Statistics
-
- hook :before_run do
- puts "hello from hook"
- end
-
- def initialize
- initialize_service(
- :frontend_address => 'tcp://127.0.0.1:4000',
- :response_address => 'tcp://127.0.0.1:4001',
- :request_address_a => 'tcp://127.0.0.1:4002',
- :request_address_b => 'tcp://127.0.0.1:4003',
- :identity => 'service-a'
- )
- end
-end
-
-Service.new.run
View
26 example/service-b
@@ -1,26 +0,0 @@
-#!/usr/bin/env ruby
-$: << File.expand_path(File.join(File.dirname(__FILE__),'..','lib'))
-
-require 'rz/service'
-require 'rz/service/statistics'
-
-class Service
- include RZ::Service
- include RZ::Service::Statistics
-
- hook :before_run do
- puts "hello from hook"
- end
-
- def initialize
- initialize_service(
- :frontend_address => 'tcp://127.0.0.1:4010',
- :response_address => 'tcp://127.0.0.1:4011',
- :request_address_a => 'tcp://127.0.0.1:4012',
- :request_address_b => 'tcp://127.0.0.1:4013',
- :identity => 'service-b'
- )
- end
-end
-
-Service.new.run
View
26 example/service-c
@@ -1,26 +0,0 @@
-#!/usr/bin/env ruby
-$: << File.expand_path(File.join(File.dirname(__FILE__),'..','lib'))
-
-require 'rz/service'
-require 'rz/service/statistics'
-
-class Service
- include RZ::Service
- include RZ::Service::Statistics
-
- hook :before_run do
- puts "hello from hook"
- end
-
- def initialize
- initialize_service(
- :frontend_address => 'tcp://127.0.0.1:4020',
- :response_address => 'tcp://127.0.0.1:4021',
- :request_address_a => 'tcp://127.0.0.1:4022',
- :request_address_b => 'tcp://127.0.0.1:4023',
- :identity => 'service-c'
- )
- end
-end
-
-Service.new.run
View
10 example/worker
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+
+require './example'
+
+name = ARGV[0] || raise("Usage ./worker {a,b,c}")
+
+
+options = Example.options_for(:worker,ARGV[0].to_sym)
+
+Worker.new(options).run
View
27 example/worker-a
@@ -1,27 +0,0 @@
-#!/usr/bin/env ruby
-$: << File.expand_path(File.join(File.dirname(__FILE__),'..','lib'))
-
-require 'rz/worker'
-
-class Worker
- include RZ::Worker
-
- hook :before_run do
- puts "hello from hook"
- end
-
- def initialize
- initialize_worker(
- :response_address => 'tcp://127.0.0.1:4001',
- :request_address_a => 'tcp://127.0.0.1:4002',
- :request_address_b => 'tcp://127.0.0.1:4003',
- :identity => "worker#{Process.pid}"
- )
- end
-end
-
-Worker.send :register,:eval do |string|
- eval string
-end
-
-Worker.new.run
View
27 example/worker-b
@@ -1,27 +0,0 @@
-#!/usr/bin/env ruby
-$: << File.expand_path(File.join(File.dirname(__FILE__),'..','lib'))
-
-require 'rz/worker'
-
-class Worker
- include RZ::Worker
-
- hook :before_run do
- puts "hello from hook"
- end
-
- def initialize
- initialize_worker(
- :response_address => 'tcp://127.0.0.1:4011',
- :request_address_a => 'tcp://127.0.0.1:4012',
- :request_address_b => 'tcp://127.0.0.1:4013',
- :identity => "worker#{Process.pid}"
- )
- end
-end
-
-Worker.send :register,:eval do |string|
- eval string
-end
-
-Worker.new.run
View
27 example/worker-c
@@ -1,27 +0,0 @@
-#!/usr/bin/env ruby
-$: << File.expand_path(File.join(File.dirname(__FILE__),'..','lib'))
-
-require 'rz/worker'
-
-class Worker
- include RZ::Worker
-
- hook :before_run do
- puts "hello from hook"
- end
-
- def initialize
- initialize_worker(
- :response_address => 'tcp://127.0.0.1:4021',
- :request_address_a => 'tcp://127.0.0.1:4022',
- :request_address_b => 'tcp://127.0.0.1:4023',
- :identity => "worker#{Process.pid}"
- )
- end
-end
-
-Worker.send :register,:eval do |string|
- eval string
-end
-
-Worker.new.run
Please sign in to comment.
Something went wrong with that request. Please try again.