Class: ReportsMash::Engine::Engine

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Defined in:
lib/reportsmash/engine/engine.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

instance

Constructor Details

#initializeEngine

Returns a new instance of Engine.



15
16
17
18
19
20
21
22
# File 'lib/reportsmash/engine/engine.rb', line 15

def initialize
  @transactions = {}
  @engine_start = Time.now
  @delivery_queue = []
  @db_transactions = {}
  @pid = Process.pid
  @started = false
end

Instance Attribute Details

#collectorObject

Returns the value of attribute collector.



13
14
15
# File 'lib/reportsmash/engine/engine.rb', line 13

def collector
  @collector
end

#delivery_queueObject

Returns the value of attribute delivery_queue.



11
12
13
# File 'lib/reportsmash/engine/engine.rb', line 11

def delivery_queue
  @delivery_queue
end

#pidObject

Returns the value of attribute pid.



12
13
14
# File 'lib/reportsmash/engine/engine.rb', line 12

def pid
  @pid
end

Instance Method Details

#boot_from_forkObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/reportsmash/engine/engine.rb', line 24

def boot_from_fork
  return if @started
  @started = true
  @pid = Process.pid
  @engine_start = Time.now

  $stderr.puts "Initializing plugin ReportsMash in child #{Process.ppid} -> #{Process.pid}\n"
  begin
    start_worker
  rescue => e
    $stderr.puts "Error while starting worker #{e.inspect}"
  end
end

#custom_attr(new_params) ⇒ Object



59
60
61
62
# File 'lib/reportsmash/engine/engine.rb', line 59

def custom_attr(new_params)
  trans = ReportsMash::Engine::ThreadState.get_current_state.transaction
  trans.user_params.merge!(new_params) if new_params.is_a? Array
end

#save_db_transaction(id, duration, sql, connection_id) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/reportsmash/engine/engine.rb', line 71

def save_db_transaction(id, duration, sql, connection_id)
  trans = ReportsMash::Engine::ThreadState.get_current_state.transaction
  return unless trans
  item = {
    duration: duration,
    sql: sql,
    connection_id: connection_id
  }
  trans.db_transactions.push item
  trans.db_duration_ms += duration
end

#save_external_http(item) ⇒ Object



94
95
96
97
98
99
# File 'lib/reportsmash/engine/engine.rb', line 94

def save_external_http(item)
  trans = ReportsMash::Engine::ThreadState.get_current_state.transaction
  return unless trans
  trans.external_http_requests.push item
  trans.external_http_duration_ms += item[:duration]
end

#save_external_mc(op, duration) ⇒ Object



101
102
103
104
105
106
# File 'lib/reportsmash/engine/engine.rb', line 101

def save_external_mc(op, duration)
  trans = ReportsMash::Engine::ThreadState.get_current_state.transaction
  return unless trans
  h = {op.to_sym => duration}
  trans.memcached_ops.push h
end

#save_external_redis(op, duration) ⇒ Object



108
109
110
111
112
113
# File 'lib/reportsmash/engine/engine.rb', line 108

def save_external_redis(op, duration)
  trans = ReportsMash::Engine::ThreadState.get_current_state.transaction
  return unless trans
  h = {op.to_sym => duration}
  trans.redis_ops.push h
end

#save_render_transaction(id, duration, path) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/reportsmash/engine/engine.rb', line 83

def save_render_transaction(id, duration, path)
  trans = ReportsMash::Engine::ThreadState.get_current_state.transaction
  return unless trans
  item = {
    duration: duration,
    path: path
  }
  trans.render_transactions.push item
  trans.render_duration_ms += duration
end

#start_workerObject



64
65
66
67
68
69
# File 'lib/reportsmash/engine/engine.rb', line 64

def start_worker
  @worker_thread = ReportsMash::Engine::Threading::WorkerThread.new do
    worker_loop = ReportsMash::Engine::WorkerLoop.new
    worker_loop.run
  end
end

#transaction_end(start_time, stop_time, e, response, env) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/reportsmash/engine/engine.rb', line 49

def transaction_end(start_time, stop_time, e, response, env)
  transaction = @transactions[env["X-REPORTSMASH-TRANSACTION-ID"]]
  (status_code, headers, body) = response       
  transaction.end(start_time, stop_time, status_code, headers)
  # don't allow more than 150 transactions in queue
  @delivery_queue.push transaction if @delivery_queue.count < 150
  env.delete("X-REPORTSMASH-TRANSACTION-ID") if env["X-REPORTSMASH-TRANSACTION-ID"]
  ReportsMash::Engine::ThreadState.clear
end

#transaction_start(request) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/reportsmash/engine/engine.rb', line 38

def transaction_start(request)
  if @pid != Process.pid && !@started
    # we have been forked. Make sure engine worker is running in this child
    ReportsMash::Engine.boot_from_fork()
  end
  transaction = ReportsMash::Engine::Transaction.new(@instance, request)
  ReportsMash::Engine::ThreadState.get_current_state.transaction = transaction
  request["X-REPORTSMASH-TRANSACTION-ID"] = transaction.tid
  @transactions[transaction.tid] = transaction
end