Module: Bellbro::Trackable

Included in:
Service, Worker
Defined in:
lib/bellbro/trackable.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



3
4
5
# File 'lib/bellbro/trackable.rb', line 3

def record
  @record
end

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
# File 'lib/bellbro/trackable.rb', line 5

def self.included(base)
  base.class_eval do
    extend ClassMethods
  end
end

Instance Method Details

#record_add(attr, num) ⇒ Object



56
57
58
59
60
# File 'lib/bellbro/trackable.rb', line 56

def record_add(attr, num)
  attr = attr.to_sym
  validate(attr => num)
  @record[:data][attr] += num
end

#record_incr(attr) ⇒ Object



62
63
64
# File 'lib/bellbro/trackable.rb', line 62

def record_incr(attr)
  record_add(attr, 1)
end

#record_set(attr, value) ⇒ Object



50
51
52
53
54
# File 'lib/bellbro/trackable.rb', line 50

def record_set(attr, value)
  attr = attr.to_sym
  validate(attr => value)
  @record[:data][attr] = value
end

#record_update(attrs) ⇒ Object



43
44
45
46
47
48
# File 'lib/bellbro/trackable.rb', line 43

def record_update(attrs)
  attrs.symbolize_keys!
  attrs.each do |attr, value|
    record_set(attr, value)
  end
end

#status_update(force = false) ⇒ Object



32
33
34
35
36
37
# File 'lib/bellbro/trackable.rb', line 32

def status_update(force = false)
  return unless @log_record_schema && Bellbro::Settings.logger
  return unless force || ((@count += 1) % @write_interval) == 0
  line = Rails.env.test? ? JSON.pretty_generate(@record) : @record.to_json
  Retryable.retryable { write_log(line) }
end

#stop_trackingObject



66
67
68
69
70
71
72
# File 'lib/bellbro/trackable.rb', line 66

def stop_tracking
  @record[:complete] = true
  @record[:stopped] = Time.now.utc.iso8601
  @tracking = false
  status_update(true)
  @record = nil
end

#track(opts = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/bellbro/trackable.rb', line 21

def track(opts={})
  return if @log_record_schema # Ignore repeated calls to #track, as in RefreshLinksWorker
  opts.symbolize_keys!
  @log_record_schema = self.class.log_record_schema
  @write_interval = opts[:write_interval] || 500
  @count = 0
  @tracking = true
  initialize_log_record if @log_record_schema
  status_update(true)
end

#tracking?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/bellbro/trackable.rb', line 74

def tracking?
  !!@tracking
end

#write_log(line) ⇒ Object



39
40
41
# File 'lib/bellbro/trackable.rb', line 39

def write_log(line)
  ring line
end