Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 12th, 2012  |  syntax: None  |  size: 3.25 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. # master.rb
  3.  
  4. #$:.unshift('~/w/ruote/lib')
  5. #$:.unshift('~/w/ruote-redis/lib')
  6.  
  7. require 'rubygems'
  8. require 'rufus-json/automatic'
  9. require 'ruote'
  10. require 'redis'
  11. require 'ruote-redis'
  12. require 'net/ssh'
  13. #require 'copy_participant.rb'
  14.  
  15. class CopyParticipant
  16.   include  Ruote::LocalParticipant
  17.  
  18.   def consume(workitem)
  19.     id = workitem.fields['params']['nb'].to_i
  20.     address = workitem.fields['addresses'][id]
  21.         opts = {
  22.       'host' => '127.0.0.1',
  23.       'db' => 11 + id,
  24.       'thread_safe' => true }
  25.         context.engine.register_participant(
  26.       "slave-#{id}",
  27.       Ruote::EngineParticipant,
  28.       'storage_class' => Ruote::Redis::Storage,
  29.       'storage_args' => opts)
  30.     system("scp slave.rb root@#{adress}:/root/test_ruote")
  31.     Net::SSH.start('172.16.65.81','root') do |ssh|
  32.       ssh.exec("ruby /root/test_ruote/slave.rb")
  33.         end
  34.         puts "Fin de CopyParticipant"
  35.         reply_to_engine(workitem)
  36.   end
  37. end
  38.  
  39. master = Ruote::Engine.new(
  40.   Ruote::Worker.new(
  41.     Ruote::Redis::Storage.new(
  42.       'host' => '127.0.0.1',
  43.       'db' => 10,
  44.       'thread_safe' => true,
  45.       'engine_id' => 'master')))
  46.  
  47. master.noisy = true
  48.  
  49. #slave = Ruote::Engine.new(Ruote::Worker.new(Ruote::Redis::Storage.new('host' => '127.0.0.1', 'db' => 12, 'thread_safe' => true, 'engine_id' => 'slave-0')))
  50. #slave.register_participant :alpha do |workitem|
  51. #       puts "Hello, I'm alpha"
  52. #end
  53. #slave.register_participant('master', Ruote::EngineParticipant, 'storage_class' => Ruote::Redis::Storage, 'storage_args' => {'host'=>'127.0.0.1','db'=>10,'thread_safe'=>true})
  54.  
  55. master.register_participant :init_workitem do |workitem|
  56.   workitem.fields['adresses'] = []
  57.   workitem.fields['adresses'].push("griffon-79")
  58.   workitem.fields['adresses'].push("griffon-81")
  59. end
  60.  
  61. master.register_participant :copy_to_slave_loc, CopyParticipant
  62.  
  63. #master.register_participant('slave-0', Ruote::EngineParticipant, 'storage_class' => Ruote::Redis::Storage, 'storage_args' => {'host'=>'127.0.0.1', 'db'=>12, 'thread_safe'=>true})
  64.  
  65. pdef = Ruote.process_definition do
  66.   participant :init_workitem
  67.   concurrent_iterator :times => 1 do
  68.     #Copy the slave file on the slave computer
  69.     participant :copy_to_slave_loc, :nb => '0'#, :forget => true
  70.     #launch the subprocess with the slave engine
  71.     subprocess :sub, :engine => 'slave-0'
  72.   end
  73.   subprocess :sub, :engine => 'slave-0'
  74.   define :sub do
  75.     participant :alpha
  76.   end
  77. end
  78.  
  79. wfid = master.launch(pdef)
  80. master.wait_for(wfid)
  81.  
  82. ################################################################################
  83. ################################################################################
  84.  
  85. # slave.rb
  86.  
  87. $:.unshift('~/w/ruote/lib')
  88. $:.unshift('~/w/ruote-redis/lib')
  89.  
  90. require 'rubygems'
  91. require 'rufus-json/automatic'
  92. require 'ruote'
  93. require 'redis'
  94. require 'ruote-redis'
  95. #require 'yajl' rescue require 'json'
  96.  
  97. slave = Ruote::Engine.new(
  98.   Ruote::Worker.new(
  99.     Ruote::Redis::Storage.new(
  100.       'host' => 'localhost',
  101.       'db' => 11,
  102.       'thread_safe' => true,
  103.       'engine_id' => 'slave-0')))
  104.  
  105. slave.noisy = true
  106.  
  107. slave.register_participant(
  108.   'master',
  109.   Ruote::EngineParticipant,
  110.   'storage_class' => Ruote::Redis::Storage,
  111.   'storage_args' => {
  112.     'host' => 'localhost',
  113.     'db' => 10,
  114.     'thread_safe' => true
  115.   })
  116.  
  117. slave.register_participant :alpha do |workitem|
  118.   puts "Hello, je suis alpha"
  119. end
  120.  
  121. slave.join