Class: Piggly::Command::Trace

Inherits:
Base show all
Defined in:
lib/piggly/command/trace.rb

Overview

This command connects to the database, dumps all stored procedures, compiles them with instrumentation code, and finally installs the instrumented code.

Class Method Summary collapse

Methods inherited from Base

command, connect, filter, o_accumulate, o_cache_root, o_connection_name, o_database_yml, o_dry_run, o_include_paths, o_reject, o_report_root, o_select, o_version

Class Method Details

.configure(argv, config = Config.new) ⇒ Object

Parses command-line options

@return [Config]


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/piggly/command/trace.rb', line 63

def configure(argv, config = Config.new)
  p = OptionParser.new do |o|
    o.on("-t", "--dry-run",           "only print the names of matching procedures", &o_dry_run(config))
    o.on("-s", "--select PATTERN",    "select procedures matching PATTERN", &o_select(config))
    o.on("-r", "--reject PATTERN",    "ignore procedures matching PATTERN", &o_reject(config))
    o.on("-c", "--cache-root PATH",   "local cache directory", &o_cache_root(config))
    o.on("-o", "--report-root PATH",  "report output directory", &o_report_root(config))
    o.on("-d", "--database PATH",     "read database adapter settings from YAML/JSON file", &o_database_yml(config))
    o.on("-k", "--connection NAME",   "use connection adapter NAME", &o_connection_name(config))
    o.on("-V", "--version",           "show version", &o_version(config))
    o.on("-h", "--help",              "show this message") { abort o.to_s }
  end

  begin
    p.parse! argv
    config
  rescue OptionParser::InvalidOption,
         OptionParser::InvalidArgument,
         OptionParser::MissingArgument
    puts p
    puts
    puts $!
    exit! 1
  end
end

.dump(connection, index) ⇒ Object

Writes all stored procedures in the database to disk

@return [void]


38
39
40
# File 'lib/piggly/command/trace.rb', line 38

def dump(connection, index)
  index.update(Dumper::ReifiedProcedure.all(connection))
end

.install(installer, procedures, profile) ⇒ Object



56
57
58
59
# File 'lib/piggly/command/trace.rb', line 56

def install(installer, procedures, profile)
  puts "tracing #{procedures.size} procedures"
  installer.install(procedures, profile)
end

.main(argv) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/piggly/command/trace.rb', line 12

def main(argv)
  config     = configure(argv)
  connection = connect(config)
  index      = Dumper::Index.new(config)

  dump(connection, index)

  procedures = filter(config, index)

  if procedures.empty?
    if config.filters.empty?
      abort "no stored procedures in the cache"
    else
      abort "no stored procedures in the cache matched your criteria"
    end
  elsif config.dry_run?
    puts procedures.map{|p| p.signature }
    exit 0
  end

  trace(config, procedures)
  install(Installer.new(config, connection), procedures, Profile.new)
end

.trace(config, procedures) ⇒ Object

Compiles all the stored procedures on disk and installs them

@return [void]


44
45
46
47
48
49
50
51
52
53
54
# File 'lib/piggly/command/trace.rb', line 44

def trace(config, procedures)
  puts "compiling #{procedures.size} procedures"

  compiler = Compiler::TraceCompiler.new(config)
  queue    = Util::ProcessQueue.new
  procedures.each{|p| queue.add { compiler.compile(p) }}

  # Force parser to load before we start forking
  Parser.parser
  queue.execute
end