Class: Balsamique::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/balsamique/reporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(balsamique, options = {}) ⇒ Reporter

Returns a new instance of Reporter.



6
7
8
9
10
11
# File 'lib/balsamique/reporter.rb', line 6

def initialize(balsamique, options = {})
  @bq = balsamique
  @poll = options.fetch(:poll, 10.0)
  @max_retries = options.fetch(:max_retries, 20)
  @logger = options.fetch(:logger, Logger.new($stdout))
end

Instance Attribute Details

#bqObject

Returns the value of attribute bq.



13
14
15
# File 'lib/balsamique/reporter.rb', line 13

def bq
  @bq
end

#loggerObject

Returns the value of attribute logger.



13
14
15
# File 'lib/balsamique/reporter.rb', line 13

def logger
  @logger
end

#max_retriesObject

Returns the value of attribute max_retries.



13
14
15
# File 'lib/balsamique/reporter.rb', line 13

def max_retries
  @max_retries
end

#pollObject

Returns the value of attribute poll.



13
14
15
# File 'lib/balsamique/reporter.rb', line 13

def poll
  @poll
end

#stoppedObject

Returns the value of attribute stopped.



13
14
15
# File 'lib/balsamique/reporter.rb', line 13

def stopped
  @stopped
end

Instance Method Details

#job_status(id) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/balsamique/reporter.rb', line 23

def job_status(id)
  status = bq.job_status(id)
  if status[id]
    bq.fill_job_failures(status)
    bq.fill_args_tasks(status)
  end
  status[id]
end

#perform(id, job_status, timestamp, retries) ⇒ Object



19
20
21
# File 'lib/balsamique/reporter.rb', line 19

def perform(id, job_status, timestamp, retries)
  logger.info("#{id} #{timestamp} #{retries} #{job_status.to_json}")
end

#runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/balsamique/reporter.rb', line 32

def run
  @stopped = false
  until stopped do
    report = bq.pop_report(Time.now.to_f)
    if report
      id, timestamp, retries = report
      begin
        if (status = job_status(id))
          perform(id, status, timestamp, retries)
          bq.remove_job(id) unless status[:task]
        else
          logger.info("#{id} #{timestamp} #{retries} null")
        end
        bq.complete_report(id)
      rescue => error
        logger.warn(
          "#{id} #{timestamp} #{retries} " +
          "#{error.class.name} #{error.message}")
        logger.debug(error)
        bq.complete_report(id) if retries > max_retries
      end
    else
      t_poll = Time.now.to_f + poll * (1.0 - 0.5 * rand())
      sleep 0.1 until Time.now.to_f > t_poll || stopped
    end
  end
end

#stopObject



15
16
17
# File 'lib/balsamique/reporter.rb', line 15

def stop
  @stopped = true
end