Class: ThreadsPad::Job

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#padObject

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

#currentObject



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

#maxObject



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

#minObject



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

#startObject



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

Returns:

  • (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

#workObject



15
16
# File 'lib/threads_pad/job.rb', line 15

def work
end

#wrapperObject



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