Module: QueueProcessor::RootCalculation::StateMachines

Extended by:
ActiveSupport::Concern
Included in:
QueueProcessor::RootCalculation
Defined in:
lib/queue-processor/root_calculation/state_machines.rb

Instance Method Summary collapse

Instance Method Details

#check_and_start_dependenciesObject

After we finish our calculation (but not all the dependencies) look for dependent work to do and queue it. Set processing at to indicate that we have work in progress and later calculations should wait until the dependent calculations we created all finish.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/queue-processor/root_calculation/state_machines.rb', line 87

def check_and_start_dependencies      
  if (has_dependent_calculations?)
    do_dependencies = self.with_lock do 
      if (self.processing_at.nil? && !calculation_stale?)
        update_attribute(:processing_at, delayed_job_queued_at)

        # this performs a callback, create_and_enqueue_dependent_calculations, to do queue up any calculations
        begin
          self.fire_events!(:start_dependent_calculations)    
        rescue StateMachine::Error => e
          Rails.logger.warn("#{self.describe}: invalid state transition for start_dependent_calculations: #{self.attributes}, changed: #{self.changed_attributes.map {|k,v| {k => {v => self.send(k) }}}}")
          raise e
        end
        
      else
        Rails.logger.warn("#{self.describe}: should have started dependencies, but something else set processing at ")
        false
      end        
    end
  end
end

#do_calculationObject

Perform whatever calculation is associated with this class



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/queue-processor/root_calculation/state_machines.rb', line 33

def do_calculation
  Rails.logger.debug {"#{self.describe}: starting calculation"}

  # mark as waiting
  self.with_lock do
    
    begin
      self.fire_events!(:start_calculation) 
    rescue StateMachine::Error => e
      Rails.logger.warn("#{self.describe} invalid state transition starting calculation: #{self.attributes}, changed: #{self.changed_attributes.map {|k,v| {k => {v => self.send(k) }}}}")
      raise e
    end
  end    

  # callback to do the actual calculation
  perform_calculation

  # mark as calculated if we are still waiting (does this need to do a with_lock?)
  self.with_lock do
    self.fire_events(:finish_calculation) 
  end

  Rails.logger.debug {"#{self.describe}: finished calculation"}
end

#finish_workObject

we’ve finished our calculation and all dependent calculations have finished



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/queue-processor/root_calculation/state_machines.rb', line 72

def finish_work
  Rails.logger.debug {"#{self.describe}: Cleaning up"}
  
  unless(processing_at.nil?)
    elapsed_time = (Time.now - self.processing_at)
    Rails.logger.info("#{self.describe}: finished after #{elapsed_time.round(2)} seconds")
    self.started_processing_at = self.processing_at # so observers can record how long it took to finish
    update_attribute(:processing_at, nil)
  end
  reload
end

#finished?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
# File 'lib/queue-processor/root_calculation/state_machines.rb', line 15

def finished?
  processing_idle? && 
  calculation_available? && 
  dependent_calculations_idle? &&
  !something_queued?
end

#perform_workObject

We’ve come off the queue and need to be calculated



24
25
26
27
28
29
30
# File 'lib/queue-processor/root_calculation/state_machines.rb', line 24

def perform_work
  do_calculation
  check_and_start_dependencies 
  publish_calculation

  true
end

#publish_calculationObject

Publish the calculation we just finished. The calculation is published before we’ve finished. We’re finished when we’ve queued any and all dependent calculations, but the number we just computed is available



61
62
63
64
65
66
67
68
69
# File 'lib/queue-processor/root_calculation/state_machines.rb', line 61

def publish_calculation
  Rails.logger.debug {"#{self.describe}: publishing calculation"}
  
  # we can only transition from calulating => available.  If something else 
  # queued this calculation again, we'll keep the calculation unavailable
  self.with_lock do
    self.fire_events(:release_calculation)
  end
end

#reset_state_machines!Object



6
7
8
9
# File 'lib/queue-processor/root_calculation/state_machines.rb', line 6

def reset_state_machines!
  reload
  update(:processing_at => nil,:calculation_state => 'available', :dependent_calculations_state => 'idle', :processing_state => 'idle')
end

#stuck?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/queue-processor/root_calculation/state_machines.rb', line 11

def stuck?
  (processing_waiting? || dependent_calculations_waiting?) && !dependencies_in_queue?
end