Class: DCA::Jobs::AnalyzerJob

Inherits:
Job
  • Object
show all
Defined in:
lib/dca/jobs/analyzer_job.rb

Instance Attribute Summary

Attributes inherited from Job

#options

Instance Method Summary collapse

Methods inherited from Job

create, #destroy, #initialize, perform, queue, #safe_perform!, #shutdown?

Methods included from Helpers::Logger

#logger

Constructor Details

This class inherits a constructor from DCA::Jobs::Job

Instance Method Details

#changeObject



30
31
32
# File 'lib/dca/jobs/analyzer_job.rb', line 30

def change
  false
end

#distribute(position) ⇒ Object



34
35
36
# 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) ⇒ Object

Fetch newly created or modified positions

Raises:

  • (NotImplementedError)


52
53
54
# File 'lib/dca/jobs/analyzer_job.rb', line 52

def fetch position
  raise NotImplementedError
end

#on_analyze(position, state) ⇒ Object



60
61
62
63
# 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_changeObject



56
57
58
# File 'lib/dca/jobs/analyzer_job.rb', line 56

def on_change
  notify(:change)
end

#on_failure(error) ⇒ Object



75
76
77
78
# 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) ⇒ Object



65
66
67
68
69
70
71
72
73
# 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_successObject



80
81
82
# File 'lib/dca/jobs/analyzer_job.rb', line 80

def on_success
  notify(:success)
end

#performObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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) ⇒ Object

Return position model from hash



47
48
49
# File 'lib/dca/jobs/analyzer_job.rb', line 47

def position hash
  Models::Position.new hash
end

#positions(&block) ⇒ Object

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

Raises:

  • (NotImplementedError)


42
43
44
# File 'lib/dca/jobs/analyzer_job.rb', line 42

def positions(&block)
  raise NotImplementedError
end

#sessionObject



4
5
6
# File 'lib/dca/jobs/analyzer_job.rb', line 4

def session
  @session ||= options[:session] || UUID.generate(:compact)
end