Sha256: e02b69530ff4868d2efe3d0a0664270e4263766b7435f72b1e8c15b669c7fee2

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

module Scales
  module Worker
    class Pusher
      attr_reader :app
      attr_reader :type
      attr_reader :total
      attr_reader :done
      attr_reader :progress
    
      def initialize(type = Application::Rails)
        @type, @app = type, type.app
        reset_progress!
      end
      
      def reset_progress!
        @total, @done = 0, 0
      end
      
      def progress
        ((@done.to_f / @total.to_f) * 100).to_i rescue 0
      end

      def process!(path)
        env = Path.with_options_to_env(path)
        
        response  = @app.call(env)
        response.last.close
        
        Storage::Sync.set_content(path[:to], Response.to_string(response)) if path[:push]
        
        env
      end
      
      def post_process!(env)
        while path = Thread.current[:post_process_queue].pop
          request = Path.to_env(path, env)
          
          begin
            response  = @app.call(request)
            response.last.close
          rescue Exception => e
            puts e
          end
        end
      end
      
      # Process a single path in thread
      def process_push!(path)
        Thread.current[:post_process_queue] = []
        env = process!(path)
        post_process!(env)
        @done += 1
      end
      
      def push!(paths)
        raise "No Paths added".red if paths.nil? or paths.empty?
        
        puts "Environment:    #{Scales.env}".green
        puts "Application:    #{@type.name}".green
        puts "Path:           #{Dir.pwd}".green
        puts "Redis:          #{Scales.config.host}:#{Scales.config.port}/#{Scales.config.database}".green
        
        @total, @done = paths.size, 0
        paths.each do |path|
          print "Pushing paths:  #{progress}% (#{@done}/#{@total})".green
          process_push!(path)
          print "\r"
        end
        
        puts "Pushing paths:  #{progress}% (#{@done}/#{@total})".green
        puts "Done.".green
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
scales-worker-0.0.1.beta.2 lib/scales-worker/pusher.rb
scales-worker-0.0.1.beta.1 lib/scales-worker/pusher.rb