Class: Scales::Worker::Pusher

Inherits:
Object
  • Object
show all
Defined in:
lib/scales-worker/pusher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = Application::Rails) ⇒ Pusher

Returns a new instance of Pusher.



10
11
12
13
# File 'lib/scales-worker/pusher.rb', line 10

def initialize(type = Application::Rails)
  @type, @app = type, type.app
  reset_progress!
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



4
5
6
# File 'lib/scales-worker/pusher.rb', line 4

def app
  @app
end

#doneObject (readonly)

Returns the value of attribute done.



7
8
9
# File 'lib/scales-worker/pusher.rb', line 7

def done
  @done
end

#progressObject (readonly)

Returns the value of attribute progress.



8
9
10
# File 'lib/scales-worker/pusher.rb', line 8

def progress
  @progress
end

#totalObject (readonly)

Returns the value of attribute total.



6
7
8
# File 'lib/scales-worker/pusher.rb', line 6

def total
  @total
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/scales-worker/pusher.rb', line 5

def type
  @type
end

Instance Method Details

#post_process!(env) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/scales-worker/pusher.rb', line 34

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 if response.last.respond_to?(:close)
    rescue Exception => e
      puts e
    end
  end
end

#process!(path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/scales-worker/pusher.rb', line 23

def process!(path)
  env = Path.with_options_to_env(path)
  
  response  = @app.call(env)
  response.last.close if response.last.respond_to?(:close)
  
  push_response(path, Response.to_string(response)) if path[:push]
  
  env
end

#process_push!(path) ⇒ Object

Process a single path in thread



48
49
50
51
52
53
# File 'lib/scales-worker/pusher.rb', line 48

def process_push!(path)
  Thread.current[:post_process_queue] = []
  env = process!(path)
  post_process!(env)
  @done += 1
end

#push!(paths) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/scales-worker/pusher.rb', line 55

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

#reset_progress!Object



15
16
17
# File 'lib/scales-worker/pusher.rb', line 15

def reset_progress!
  @total, @done = 0, 0
end