jdrago999/distribot

View on GitHub
examples/controller

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

require 'bundler/setup'
require 'distribot'
require 'byebug'
require 'pp'
require 'active_support/core_ext/object'
require 'active_support/core_ext/array'
require 'active_support/json'

Distribot.configure do |config|
  config.redis_url = ENV['DISTRIBOT_REDIS_URL']
  config.rabbitmq_url = ENV['DISTRIBOT_RABBITMQ_URL']
end

module Example

  def self.phase_groups
    [
      [
        {
          name: 'start',
          is_initial: true,
          transitions_to: 'get-latest-stories'
        },
        {
          name: 'get-latest-stories',
          transitions_to: 'download-news',
          handlers: [
            {
              name: 'News::ArticleLister',
              version: '~> 1.0'
            }
          ]
        },
        {
          name: 'download-news',
          transitions_to: 'finish',
          handlers: [
            {
              name: 'News::ArticleDownloaderWorker',
              version: '1.0.0'
            }
          ]
        },
        {
          name: 'finish',
          is_final: true
        }
      ]
      # [
      #   {
      #     name: 'start',
      #     is_initial: true,
      #     transitions_to: 'part1'
      #   },
      #   {
      #     name: 'part1',
      #     handlers: [
      #       {
      #         name: 'HardWorker',
      #         version: '~> 1.0'
      #       }
      #     ],
      #     transitions_to: 'part2'
      #   },
      #   {
      #     name: 'part2',
      #     handlers: %w(
      #       GoodWorker
      #       FastWorker
      #     ),
      #     transitions_to: 'part3'
      #   },
      #   {
      #     name: 'part3',
      #     handlers: %w(
      #       CheapWorker
      #       ForeignWorker
      #     ),
      #     transitions_to: 'part4'
      #   },
      #   {
      #     name: 'part4',
      #     handlers: %w(
      #       SlowWorker
      #     ),
      #     transitions_to: 'finish'
      #   },
      #   {
      #     name: 'finish',
      #     is_final: true
      #   }
      # ],
      # [
      #   {
      #     name: 'start',
      #     is_initial: true,
      #     transitions_to: 'searching'
      #   },
      #   {
      #     name: 'searching',
      #     handlers: %w(
      #       GoodWorker
      #       FastWorker
      #       CheapWorker
      #     ),
      #     transitions_to: 'finish'
      #   },
      #   {
      #     name: 'finish',
      #     is_final: true
      #   }
      # ]
    ]
  end

  def self.make_flow(name)
    name += "#1"
    @flow = Distribot::Flow.new(
      name: name,
      phases: phase_groups.sample
    )
    @flow.save! do |info|
      finished = Distribot.redis.incr('finished')
      flow = Distribot::Flow.find(info[:flow_id])
      first_transition = flow.transitions.sort_by(&:timestamp).first
      last_transition = flow.transitions.sort_by(&:timestamp).last
      duration = last_transition.timestamp - first_transition.timestamp
      puts "WORKFLOW FINISHED: after #{duration} seconds -- #{info}: #{finished} total"
    end
    @flow
  end
end

Distribot.redis.set("finished", 0)

max = ARGV.shift.to_i
max = 1 if max <= 0
puts "<ENTER> when ready:"
gets

start_time = Time.now.to_f
Distribot.redis.set('dificulty', 3)
Distribot.redis.set('distribot.flows.running', 0)
Distribot.redis.set('distribot.flows.max', max)
Example.make_flow "Flow[1]"

loop do
  sleep 2
  max = Distribot.redis.get('distribot.flows.max').to_i
  current = Distribot.redis.get('distribot.flows.running').to_i
  running_time = Time.now.to_f - start_time
  finished = Distribot.redis.get('finished').to_i
  rate = ( finished / running_time ).to_f.round(2)
  puts "\tRUNNING: at #{Time.now} -- #{current} / #{max} (#{finished} finished in #{running_time.round(2)}sec = #{rate}/sec)"
  diff = max - current
  diff.times do |num|
    new_index = Distribot.redis.get('finished')
    puts "+++++++++ SPAWN #{num + 1}/#{diff} +++++++++++"
    Example.make_flow("Flow: #{new_index} -> ")
  end
end