Class: ThreadsPad::Job
- Inherits:
-
Object
- Object
- ThreadsPad::Job
- Defined in:
- lib/threads_pad/job.rb
Instance Attribute Summary collapse
-
#pad ⇒ Object
Returns the value of attribute pad.
Instance Method Summary collapse
- #add_event(cond, block) ⇒ Object
- #current ⇒ Object
- #current=(value) ⇒ Object
- #debug(msg) ⇒ Object
- #job_reflection=(value) ⇒ Object
- #max ⇒ Object
- #max=(value) ⇒ Object
- #min ⇒ Object
- #min=(value) ⇒ Object
- #start ⇒ Object
- #terminated? ⇒ Boolean
- #work ⇒ Object
- #wrapper ⇒ Object
Instance Attribute Details
#pad ⇒ Object
Returns the value of attribute pad.
3 4 5 |
# File 'lib/threads_pad/job.rb', line 3 def pad @pad end |
Instance Method Details
#add_event(cond, block) ⇒ Object
55 56 57 58 |
# File 'lib/threads_pad/job.rb', line 55 def add_event cond, block @events = [] if @events.nil? @events << [cond, block] end |
#current ⇒ Object
39 40 41 |
# File 'lib/threads_pad/job.rb', line 39 def current @current end |
#current=(value) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/threads_pad/job.rb', line 32 def current=value @current = value check_events return if @job_reflection.nil? @job_reflection.current = @current @job_reflection.save_if_needed end |
#debug(msg) ⇒ Object
49 50 51 52 53 |
# File 'lib/threads_pad/job.rb', line 49 def debug msg @job_reflection.job_reflection_logs.create(level: 0, msg: msg, group_id: @job_reflection.group_id) if @job_reflection rescue => e puts "debug: #{e.message}" end |
#job_reflection=(value) ⇒ Object
6 7 8 |
# File 'lib/threads_pad/job.rb', line 6 def job_reflection= value @job_reflection = value end |
#max ⇒ Object
25 26 27 |
# File 'lib/threads_pad/job.rb', line 25 def max @job_reflection.max end |
#max=(value) ⇒ Object
28 29 30 31 |
# File 'lib/threads_pad/job.rb', line 28 def max=value @job_reflection.max = value @job_reflection.save! end |
#min ⇒ Object
22 23 24 |
# File 'lib/threads_pad/job.rb', line 22 def min @job_reflection.min end |
#min=(value) ⇒ Object
18 19 20 21 |
# File 'lib/threads_pad/job.rb', line 18 def min=value @job_reflection.min = value @job_reflection.save! end |
#start ⇒ Object
9 10 11 12 |
# File 'lib/threads_pad/job.rb', line 9 def start thread = Thread.new(&(proc{self.wrapper})) thread[:job] = self end |
#terminated? ⇒ Boolean
43 44 45 46 47 |
# File 'lib/threads_pad/job.rb', line 43 def terminated? return false if @job_reflection.nil? @job_reflection.reload_if_needed @job_reflection.terminated end |
#work ⇒ Object
15 16 |
# File 'lib/threads_pad/job.rb', line 15 def work end |
#wrapper ⇒ Object
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/threads_pad/job.rb', line 61 def wrapper #ActiveRecord::Base.forbid_implicit_checkout_for_thread! ActiveRecord::Base.connection_pool.with_connection do begin @current = 0.0 @job_reflection.done = false @job_reflection.terminated = false @job_reflection.started = true @job_reflection.thread_id = Thread.current.object_id.to_s @job_reflection.save! @job_reflection.result = work unless @events.blank? while !@pad.done? except: @job_reflection Thread.pass check_events end # if @events.include? :finish end rescue => e puts "ThreadsPad::Job#wrapper: #{e.message}" e.backtrace.each {|msg| puts msg} ensure @job_reflection.done = true if @job_reflection.destroy_on_finish @job_reflection.destroy else @job_reflection.save! end end end ActiveRecord::Base.connection.close end |