Class: DavinciThreader::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/davinci_threader/main.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMain

Initialize



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/davinci_threader/main.rb', line 44

def initialize

  self.log = true
  self.log_errors = true
  self.rpm = 60
  self.total = 0
  self.successful = 0
  self.errors = 0
  self.extras = []
  self.max_threads = 10
  self.asynchronous = true
  self.show_output = true
  self.synchronous_items = []
  self.synchronous_completed = 0
  @threads = []
  @thread_count = 0

  @monitor = Thread.new do
    while self.log
      printout if self.show_output && @start_time
      sleep 0.1
    end
  end

  yield(self)

  @threads.each(&:join)
  @threads_finished_at = Time.now
  @monitor.try(:exit)
  if self.show_output && @synchronous
    self.printout
    print "\n"
  end
  @synchronous.try(:join)
  @synchronous.try(:exit)
  puts "\n-> Done!".light_green if self.show_output && self.log

rescue Interrupt

  @monitor.try(:exit)

  begin
    puts "\n-> Waiting for remaining threads to finish...".yellow
    @threads.each(&:join)
    @synchronous.try(:exit)
    puts "-> Exited!".yellow
  rescue Interrupt
    force_exit
  end

  exit

end

Instance Attribute Details

#asynchronousObject

Returns the value of attribute asynchronous.



26
27
28
# File 'lib/davinci_threader/main.rb', line 26

def asynchronous
  @asynchronous
end

#errorsObject

Returns the value of attribute errors.



23
24
25
# File 'lib/davinci_threader/main.rb', line 23

def errors
  @errors
end

#extrasObject

Returns the value of attribute extras.



24
25
26
# File 'lib/davinci_threader/main.rb', line 24

def extras
  @extras
end

#logObject

Returns the value of attribute log.



19
20
21
# File 'lib/davinci_threader/main.rb', line 19

def log
  @log
end

#log_errorsObject

Returns the value of attribute log_errors.



20
21
22
# File 'lib/davinci_threader/main.rb', line 20

def log_errors
  @log_errors
end

#max_threadsObject

Returns the value of attribute max_threads.



25
26
27
# File 'lib/davinci_threader/main.rb', line 25

def max_threads
  @max_threads
end

#successfulObject

Returns the value of attribute successful.



22
23
24
# File 'lib/davinci_threader/main.rb', line 22

def successful
  @successful
end

#synchronous_completedObject

Returns the value of attribute synchronous_completed.



38
39
40
# File 'lib/davinci_threader/main.rb', line 38

def synchronous_completed
  @synchronous_completed
end

#synchronous_itemsObject

Returns the value of attribute synchronous_items.



37
38
39
# File 'lib/davinci_threader/main.rb', line 37

def synchronous_items
  @synchronous_items
end

#thread_countObject (readonly)

Returns the value of attribute thread_count.



39
40
41
# File 'lib/davinci_threader/main.rb', line 39

def thread_count
  @thread_count
end

#totalObject

Returns the value of attribute total.



21
22
23
# File 'lib/davinci_threader/main.rb', line 21

def total
  @total
end

Instance Method Details

#completedObject



210
211
212
# File 'lib/davinci_threader/main.rb', line 210

def completed
  self.errors+self.successful
end

#errorObject



207
208
209
# File 'lib/davinci_threader/main.rb', line 207

def error
  self.errors += 1
end

#force_exitObject

Force Exit



217
218
219
220
221
222
223
# File 'lib/davinci_threader/main.rb', line 217

def force_exit
  @monitor.try(:exit)
  puts "\n-> Killing remaining threads...".light_red
  @threads.each(&:exit)
  @synchronous.try(:exit)
  puts "-> Forced Exit!".light_red
end

#make_thread(*args) ⇒ Object

Make Thread



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/davinci_threader/main.rb', line 124

def make_thread *args

  @start_time ||= Time.now

  if !self.asynchronous
    yield(*args)
    self.printout if self.show_output
    return
  end

  @threads << Thread.new(*args) do
    begin
      @thread_count += 1
      yield(*args)
      @thread_count -= 1
    rescue => e
      @thread_count -= 1
      self.errors += 1
      if self.log_errors
        output = [e.message.light_red]
        output += e.backtrace.map(&:yellow)
        print "\n#{output.join("\n")}\n"
      end
    end
  end

  sleep self.rpm
  sleep 0.1 while self.thread_count > self.max_threads

  @threads.each_with_index do |t,i|
    if !t.alive?
      t.exit
      @threads.delete_at(i)
    end
  end

end

#printoutObject

Printout



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/davinci_threader/main.rb', line 165

def printout

  c = (@threads_finished_at || Time.now) - @start_time
  output = []
  if self.asynchronous
    output << "#{"#{@rpm.to_i}/MIN".cyan}"
    output << "#{Time.at(c.to_f).utc.strftime("%H:%M:%S")}"
    if self.completed > 100
      actual_rate = (self.completed.to_f / c.to_f) * 60.0
      output << "#{'%.2f' % actual_rate}/MIN".light_green
      output << "#{Time.at(((self.total-self.completed).to_f / actual_rate) * 60.0).utc.strftime("%H:%M:%S")}".light_green
    end
  end
  output << "#{self.completed}/#{self.total} (#{self.successful.to_s.light_green} <--> #{self.errors.to_s.light_red})"
  output << "Synchronous: #{"#{self.synchronous_items.count}".purple}" if @synchronous
  output += self.extras
  print "\r#{output.join(" :: ".yellow)}    "

end

#rpmObject



16
17
18
# File 'lib/davinci_threader/main.rb', line 16

def rpm
  1.minute.to_f / @rpm.to_f
end

#rpm=(value) ⇒ Object

Attributes



12
13
14
15
# File 'lib/davinci_threader/main.rb', line 12

def rpm=(value)
  @rpm = value
  self.max_threads = value*0.03
end

#show_outputObject



34
35
36
# File 'lib/davinci_threader/main.rb', line 34

def show_output
  @show_output
end

#show_output=(_on) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/davinci_threader/main.rb', line 27

def show_output=(_on)
  @show_output = _on
  if !_on
    @monitor.try(:exit)
    @monitor = nil
  end
end

#successObject

Increment Methods



204
205
206
# File 'lib/davinci_threader/main.rb', line 204

def success
  self.successful += 1
end

#synchronous_actionObject

Synchronous Action



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/davinci_threader/main.rb', line 101

def synchronous_action
  @synchronous = Thread.new do
    begin
      while !@threads_finished_at || self.synchronous_items.count > 0
        if self.synchronous_items.count == 0
          sleep(1)
        else
          yield(self.synchronous_items.first)
          self.synchronous_items.shift
          self.synchronous_completed += 1
          self.synchronous_printout if self.show_output && @threads_finished_at
        end
      end
    rescue => e
      ap e.message
      ap e.backtrace
    end
  end
end

#synchronous_printoutObject

Synchronous Printout



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/davinci_threader/main.rb', line 188

def synchronous_printout
  elapsed_time = Time.now - @threads_finished_at
  @synchronous_remaining = self.synchronous_items.count
  @synchronous_start_count ||= @synchronous_remaining
  @synchronous_completed_after_start = @synchronous_start_count - @synchronous_remaining
  if @synchronous_completed_after_start > 0
    @synchronous_rate_per_second = @synchronous_completed_after_start.to_f / elapsed_time
    seconds_remaining = @synchronous_remaining.to_f / @synchronous_rate_per_second
    @synchronous_nice_time = Time.at(seconds_remaining).utc.strftime("%H:%M:%S")
  end
  print "\r#{"Synchronous".purple} -> Remaining: #{"#{@synchronous_remaining}".light_yellow} :: Completed: #{"#{self.synchronous_completed}".light_green} :: Rate: #{"#{'%.2f' % ((@synchronous_rate_per_second || 0) * 60.0)}".light_cyan} :: #{"#{@synchronous_nice_time}".light_green}  "
end