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



31
32
33
34
35
36
37
38
39
40
41
42
43
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
# File 'lib/davinci_threader/main.rb', line 31

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
  @threads = []
  @thread_count = 0

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

  yield self

  @threads.each &:join
  sleep 1
  @monitor.exit
  puts "\n-> Done!".light_green if self.log

rescue Interrupt

  @monitor.exit

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

  exit

end

Instance Attribute Details

#asynchronousObject

Returns the value of attribute asynchronous.



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

def asynchronous
  @asynchronous
end

#errorsObject

Returns the value of attribute errors.



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

def errors
  @errors
end

#extrasObject

Returns the value of attribute extras.



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

def extras
  @extras
end

#logObject

Returns the value of attribute log.



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

def log
  @log
end

#log_errorsObject

Returns the value of attribute log_errors.



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

def log_errors
  @log_errors
end

#max_threadsObject

Returns the value of attribute max_threads.



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

def max_threads
  @max_threads
end

#successfulObject

Returns the value of attribute successful.



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

def successful
  @successful
end

#thread_countObject (readonly)

Returns the value of attribute thread_count.



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

def thread_count
  @thread_count
end

#totalObject

Returns the value of attribute total.



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

def total
  @total
end

Instance Method Details

#completedObject



148
149
150
# File 'lib/davinci_threader/main.rb', line 148

def completed
  self.errors+self.successful
end

#errorObject



145
146
147
# File 'lib/davinci_threader/main.rb', line 145

def error
  self.errors += 1
end

#force_exitObject

Force Exit



155
156
157
158
159
160
# File 'lib/davinci_threader/main.rb', line 155

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

#make_thread(*args) ⇒ Object

Make Thread



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/davinci_threader/main.rb', line 79

def make_thread *args

  @start_time ||= Time.now

  if !self.asynchronous
    yield(*args)
    self.printout
    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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/davinci_threader/main.rb', line 120

def printout

  c = 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 << "#{actual_rate.round}/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 += self.extras
  print "\r#{output.join(" :: ".yellow)}    "

end

#rpmObject



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

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

#rpm=(value) ⇒ Object

Attributes



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

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

#successObject

Increment Methods



142
143
144
# File 'lib/davinci_threader/main.rb', line 142

def success
  self.successful += 1
end