class DCA::Jobs::AnalyzerJob

Public Instance Methods

change() click to toggle source
# File lib/dca/jobs/analyzer_job.rb, line 30
def change
  false
end
distribute(position) click to toggle source
# File lib/dca/jobs/analyzer_job.rb, line 34
def distribute position
  self.class.create :distributed => true, :position => position.to_hash, session => self.session
end
fetch(position) click to toggle source

Fetch newly created or modified positions

# File lib/dca/jobs/analyzer_job.rb, line 52
def fetch position
  raise NotImplementedError
end
on_analyze(position, state) click to toggle source
# File lib/dca/jobs/analyzer_job.rb, line 60
def on_analyze(position, state)
  logger.debug "[#{position.class}] Analyze position base_id:#{position.base_id} state:#{state}"
  notify(:analyze, :position => position, :state => state)
end
on_change() click to toggle source
# File lib/dca/jobs/analyzer_job.rb, line 56
def on_change
  notify(:change)
end
on_failure(error) click to toggle source
# File lib/dca/jobs/analyzer_job.rb, line 75
def on_failure(error)
  logger.exception error
  notify(:failure, :exception => error)
end
on_fetch(position, state, result) click to toggle source
# File lib/dca/jobs/analyzer_job.rb, line 65
def on_fetch(position, state, result)
  if result
    logger.debug "[#{position.class}] Fetch valid position id:#{position.id} base_id:#{position.base_id} state:#{state}"
  else
    logger.debug "[#{position.class}] Fetch invalid position base_id:#{position.base_id} state:#{state}"
    logger.debug "  Validation errors:\n    #{position.errors.full_messages.join("\n    ")}"
  end
  notify(:fetch, :position => position, :state => state, :result => result )
end
on_success() click to toggle source
# File lib/dca/jobs/analyzer_job.rb, line 80
def on_success
  notify(:success)
end
perform() click to toggle source
# File lib/dca/jobs/analyzer_job.rb, line 8
def perform
  return on_change if change

  if options[:distributed] && options[:position]
    analyze position options[:position]
    return
  end

  index = 0
  # get list of positions and add to cache
  positions do |position|
    if options[:distributed]
      distribute position
    else
      analyze position
    end

    index += 1
    break if options[:limit] == index || shutdown?
  end
end
position(hash) click to toggle source

Return position model from hash

# File lib/dca/jobs/analyzer_job.rb, line 47
def position hash
  Models::Position.new hash
end
positions(&block) click to toggle source

Return all positions or newly created or modified if possible. Some cases not possible to get newly created or modified positions. In this case cache will be used to identify only newly created or modified positions. Position must be a hash and should contain unique key :id and checksum for compare with cached positions and identify newly created or modified

# File lib/dca/jobs/analyzer_job.rb, line 42
def positions(&block)
  raise NotImplementedError
end
session() click to toggle source
# File lib/dca/jobs/analyzer_job.rb, line 4
def session
  @session ||= options[:session] || UUID.generate(:compact)
end

Protected Instance Methods

analyze(position) click to toggle source
# File lib/dca/jobs/analyzer_job.rb, line 90
def analyze position
  state = position.state
  on_analyze position, state

  unless state == :unmodified
    new_position = fetch_safe! position

    unless new_position
      on_fetch position, state, false
    else
      position = new_position

      valid = new_position.valid?
      state = position.save if valid
      on_fetch position, state, valid
    end
  end
rescue Exception => exception
  on_failure exception
end
fetch_safe!(position) click to toggle source
# File lib/dca/jobs/analyzer_job.rb, line 111
def fetch_safe!(position)
  fetch position
rescue Exception => exception
  on_failure exception
  false
end
notify(event, options={}) click to toggle source
# File lib/dca/jobs/analyzer_job.rb, line 86
def notify(event, options={})
  Notifier.push self, event, options
end