Class: ThreadsPad::JobReflection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job, **options) ⇒ JobReflection

Returns a new instance of JobReflection.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/threads_pad/job_reflection.rb', line 11

def initialize job, **options
  @job = job
  @current_iteration = 0
  if options[:iteration_sync]
    @iteration_sync = options[:iteration_sync].to_i
  else
    @iteration_sync = 100
  end

  @job.job_reflection = self if @job.present?
  super()
  init_attributes
  
end

Instance Attribute Details

#jobObject

Returns the value of attribute job.



5
6
7
# File 'lib/threads_pad/job_reflection.rb', line 5

def job
  @job
end

Instance Method Details

#init_attributesObject



26
27
28
29
30
31
32
33
# File 'lib/threads_pad/job_reflection.rb', line 26

def init_attributes
  self.current = 0
  self.max = 100
  self.min = 0
  self.started = false
  self.destroy_on_finish = false
  self.save!     
end

#reload_if_neededObject



46
47
48
49
50
51
52
# File 'lib/threads_pad/job_reflection.rb', line 46

def reload_if_needed
  @current_iteration += 1
  if @current_iteration > @iteration_sync
    @current_iteration = 0
    reload
  end    
end

#save_if_neededObject



38
39
40
41
42
43
44
45
# File 'lib/threads_pad/job_reflection.rb', line 38

def save_if_needed
  @current_iteration += 1
  if @current_iteration > @iteration_sync
    @current_iteration = 0
    save!
  end

end

#startObject



35
36
37
# File 'lib/threads_pad/job_reflection.rb', line 35

def start
  @job.start
end

#thread_alive?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/threads_pad/job_reflection.rb', line 53

def thread_alive?
  ret = false
  Thread.list.each do |t|
    if t.object_id.to_s == self.thread_id
      
      #get our job from found thread
      @job = t[:job]
      ret = @job.present?
    end

  end
  ret

end