module Bellbro::Trackable
Attributes
record[R]
Public Class Methods
included(base)
click to toggle source
# File lib/bellbro/trackable.rb, line 5 def self.included(base) base.class_eval do extend ClassMethods end end
Public Instance Methods
record_add(attr, num)
click to toggle source
# 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)
click to toggle source
# File lib/bellbro/trackable.rb, line 62 def record_incr(attr) record_add(attr, 1) end
record_set(attr, value)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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_tracking()
click to toggle source
# 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={})
click to toggle source
# 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?()
click to toggle source
# File lib/bellbro/trackable.rb, line 74 def tracking? !!@tracking end
write_log(line)
click to toggle source
# File lib/bellbro/trackable.rb, line 39 def write_log(line) ring line end
Private Instance Methods
initialize_log_record()
click to toggle source
# File lib/bellbro/trackable.rb, line 80 def initialize_log_record @record = { host: Socket.gethostname, agent: { name: "#{self.class.name}", thread: "#{Thread.current.object_id}", jid: jid, }, domain: @site.try(:domain) || @domain, complete: false, started: Time.now.utc.iso8601, data: {} } @log_record_schema.each do |k, v| if v == Integer @record[:data][k] = 0 else @record[:data][k] = v.new end end end
log_record_attributes()
click to toggle source
# File lib/bellbro/trackable.rb, line 103 def log_record_attributes @log_record_schema.keys + [:jid, :agent, :archived] end
validate(attrs)
click to toggle source
# File lib/bellbro/trackable.rb, line 107 def validate(attrs) attrs.each do |attr, value| raise "Invalid attribute #{attr}" unless log_record_attributes.include?(attr) raise "Invalid type for #{attr}. Value #{value} is a #{value.class}, expected a #{@log_record_schema[attr]}" unless [value.class, value.class.superclass].include?(@log_record_schema[attr]) end end