Class: ThreadsPad::Pad

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil, **options) ⇒ Pad

Returns a new instance of Pad.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/threads_pad.rb', line 9

def initialize id=nil, **options
  
  @options = options || {}
  @list = []
  @grp_id = id
  if id 
    
    @list = JobReflection.where('group_id = ?', id).to_a

  end
  
end

Class Method Details

.<<(job) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/threads_pad.rb', line 101

def << job
  refl = JobReflection.new job
  
  job.start
  return refl
  puts "JR count #{JobReflection.count}"

end

.calc_current(list) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/threads_pad.rb', line 149

def calc_current list
  return nil if list.nil?
  res = 0
  list.each do |jr|
    res += (jr.current.to_f-jr.min)/(jr.max-jr.min) * 100.0 / list.count
    #puts "jr.current #{jr.current}"
  end
  #puts "list.count #{list.count}"
  #puts "calc current raw #{res}"
  return res.round
end

.current(list = nil) ⇒ Object



144
145
146
147
148
# File 'lib/threads_pad.rb', line 144

def current list=nil
  list = JobReflection.all if list.nil?
  calc_current list
  
end

.destroy_all(list = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/threads_pad.rb', line 109

def destroy_all list=nil
  JobReflectionLog.destroy_all  if list.nil?
  list = JobReflection.all if list.nil?
  list.each do |jr|
    if jr.started && !jr.done && jr.thread_alive?
      jr.destroy_on_finish = true 
      jr.save!
    else
      jr.destroy #if jr.done #|| !jr.started
    end
  end

end

.done?(list = nil) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
163
164
165
166
167
# File 'lib/threads_pad.rb', line 160

def done? list =nil
  list = JobReflection.all if list.nil?
  res = true
  list.each do |jr|
    res &&= jr.done || !jr.thread_alive?
  end
  res
end

.empty?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/threads_pad.rb', line 175

def  empty?
  JobReflection.count == 0
end

.terminate(list = nil) ⇒ Object



168
169
170
171
172
173
174
# File 'lib/threads_pad.rb', line 168

def terminate list=nil
  list = JobReflection.all if list.nil?
  list.each do |jr|
    jr.terminated = true
    jr.save!
  end
end

.wait(list = nil, wait_for_destroy_on_finish = false) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/threads_pad.rb', line 122

def wait list=nil, wait_for_destroy_on_finish=false
  sleep 0.1 # needed to be sure other threads are started
  running = true
  list = JobReflection.all if list.nil?
  while running do
    running = false
    list.each do |jr|
      begin
        jr.reload
        running = running || jr.thread_alive? && !jr.done && jr.started && (wait_for_destroy_on_finish || !jr.destroy_on_finish)
      rescue ActiveRecord::RecordNotFound
      end
    end
    #puts "waiting: #{list.inspect}"

    sleep 0.3
  end

end

.wait_all(list = nil) ⇒ Object



141
142
143
# File 'lib/threads_pad.rb', line 141

def wait_all list=nil
  self.wait list, true
end

Instance Method Details

#<<(job) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/threads_pad.rb', line 22

def << job
  job.pad = self
  refl = JobReflection.new job, @options
  if @options[:destroy_on_finish]
    refl.destroy_on_finish = true
    refl.save!
  end
  @list << refl
end

#calc_currentObject



93
94
95
96
97
98
# File 'lib/threads_pad.rb', line 93

def calc_current

  return puts "calc_current is nil" && nil if @list.nil?
  list = @list.map {|jr| jr.job}
  ThreadsPad::Pad.calc_current list
end

#currentObject



57
58
59
60
# File 'lib/threads_pad.rb', line 57

def current
  ThreadsPad::Pad.current @list
  
end

#destroy_allObject



48
49
50
# File 'lib/threads_pad.rb', line 48

def destroy_all
  ThreadsPad::Pad.destroy_all @list
end

#destroy_oldObject



51
52
53
54
55
56
# File 'lib/threads_pad.rb', line 51

def destroy_old
  @list.delete_if do |jr|
    jr.destroy if job_reflection_old?(jr)
  end
  
end

#done?(**options) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
# File 'lib/threads_pad.rb', line 61

def done? **options
  if options.key? :except
    list = @list.dup
    list.delete options[:except]
    ThreadsPad::Pad.done? list

  else
    ThreadsPad::Pad.done? @list
  end
end

#empty?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/threads_pad.rb', line 31

def empty?
  @list.count == 0
end

#log(msg, level = 0) ⇒ Object



78
79
80
81
82
# File 'lib/threads_pad.rb', line 78

def log msg, level=0
  return if @grp_id.nil?
  JobReflectionLog.create! group_id: @grp_id, msg: msg, level: level

end

#logsObject



74
75
76
77
# File 'lib/threads_pad.rb', line 74

def logs
  return [] if @grp_id.nil?
  JobReflectionLog.where('group_id = ?', @grp_id)
end

#on(cond, &block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/threads_pad.rb', line 83

def on cond, &block
  @list.each do |jr| 
    old = job_reflection_old?(jr)
    #puts "jr is old: #{jr.inspect}"  if old
     if !old
      jr.job.add_event cond, block
      return
    end
  end
end

#startObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/threads_pad.rb', line 34

def start
  @grp_id = get_group_id 
  destroy_old
  @list.each do |jr|
    jr.group_id = @grp_id
    jr.started = true
    jr.save!
    jr.start
  end
  @grp_id
end

#terminateObject



71
72
73
# File 'lib/threads_pad.rb', line 71

def terminate
  ThreadsPad::Pad.terminate @list
end

#waitObject



45
46
47
# File 'lib/threads_pad.rb', line 45

def wait
  ThreadsPad::Pad.wait @list
end