Class: MoSQL::CLI
Constant Summary collapse
- BATCH =
1000
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#tailer ⇒ Object
readonly
Returns the value of attribute tailer.
Class Method Summary collapse
Instance Method Summary collapse
- #connect_mongo ⇒ Object
- #connect_sql ⇒ Object
-
#initialize(args) ⇒ CLI
constructor
A new instance of CLI.
- #load_collections ⇒ Object
- #parse_args ⇒ Object
- #run ⇒ Object
- #setup_signal_handlers ⇒ Object
Methods included from Logging
Constructor Details
#initialize(args) ⇒ CLI
Returns a new instance of CLI.
19 20 21 22 23 24 |
# File 'lib/mosql/cli.rb', line 19 def initialize(args) @args = args = [] @done = false setup_signal_handlers end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
12 13 14 |
# File 'lib/mosql/cli.rb', line 12 def args @args end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
12 13 14 |
# File 'lib/mosql/cli.rb', line 12 def end |
#tailer ⇒ Object (readonly)
Returns the value of attribute tailer.
12 13 14 |
# File 'lib/mosql/cli.rb', line 12 def tailer @tailer end |
Class Method Details
.run(args) ⇒ Object
14 15 16 17 |
# File 'lib/mosql/cli.rb', line 14 def self.run(args) cli = CLI.new(args) cli.run end |
Instance Method Details
#connect_mongo ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/mosql/cli.rb', line 114 def connect_mongo @mongo = Mongo::MongoClient.from_uri([:mongo]) config = @mongo['admin'].command(:ismaster => 1) if !config['setName'] && ![:skip_tail] log.warn("`#{options[:mongo]}' is not a replset.") log.warn("Will run the initial import, then stop.") log.warn("Pass `--skip-tail' to suppress this warning.") [:skip_tail] = true end [:service] ||= config['setName'] end |
#connect_sql ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/mosql/cli.rb', line 126 def connect_sql @sql = MoSQL::SQLAdapter.new(@schema, [:sql], [:schema]) if [:verbose] >= 2 @sql.db.sql_log_level = :debug @sql.db.loggers << Logger.new($stderr) end end |
#load_collections ⇒ Object
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/mosql/cli.rb', line 134 def load_collections collections = YAML.load_file([:collections]) begin @schema = MoSQL::Schema.new(collections) rescue MoSQL::SchemaError => e log.error("Error parsing collection map `#{@options[:collections]}':") log.error(e.to_s) exit(1) end end |
#parse_args ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/mosql/cli.rb', line 35 def parse_args = { :collections => 'collections.yml', :sql => 'postgres:///', :mongo => 'mongodb://localhost', :verbose => 0 } optparse = OptionParser.new do |opts| opts. = "Usage: #{$0} [options] " opts.on('-h', '--help', "Display this message") do puts opts exit(0) end opts.on('-v', "Increase verbosity") do [:verbose] += 1 end opts.on("-c", "--collections [collections.yml]", "Collection map YAML file") do |file| [:collections] = file end opts.on("--sql [sqluri]", "SQL server to connect to") do |uri| [:sql] = uri end opts.on("--mongo [mongouri]", "Mongo connection string") do |uri| [:mongo] = uri end opts.on("--schema [schema]", "PostgreSQL 'schema' to namespace tables") do |schema| [:schema] = schema end opts.on("--ignore-delete", "Ignore delete operations when tailing") do [:ignore_delete] = true end opts.on("--only-db [dbname]", "Don't scan for mongo dbs, just use the one specified") do |dbname| [:dbname] = dbname end opts.on("--tail-from [timestamp]", "Start tailing from the specified UNIX timestamp") do |ts| [:tail_from] = ts end opts.on("--service [service]", "Service name to use when storing tailing state") do |service| [:service] = service end opts.on("--skip-tail", "Don't tail the oplog, just do the initial import") do [:skip_tail] = true end opts.on("--reimport", "Force a data re-import") do [:reimport] = true end opts.on("--no-drop-tables", "Don't drop the table if it exists during the initial import") do [:no_drop_tables] = true end opts.on("--unsafe", "Ignore rows that cause errors on insert") do [:unsafe] = true end end optparse.parse!(@args) log = Log4r::Logger.new('Stripe') log.outputters = Log4r::StdoutOutputter.new(STDERR) if [:verbose] >= 1 log.level = Log4r::DEBUG else log.level = Log4r::INFO end end |
#run ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/mosql/cli.rb', line 145 def run parse_args load_collections connect_sql connect_mongo = MoSQL::Tailer.create_table(@sql.db, 'mosql_tailers') @tailer = MoSQL::Tailer.new([@mongo], :existing, , :service => [:service]) @streamer = Streamer.new(:options => , :tailer => @tailer, :mongo => @mongo, :sql => @sql, :schema => @schema) @streamer.import unless [:skip_tail] @streamer.optail end end |
#setup_signal_handlers ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/mosql/cli.rb', line 26 def setup_signal_handlers %w[TERM INT USR2].each do |sig| Signal.trap(sig) do log.info("Got SIG#{sig}. Preparing to exit...") @streamer.stop end end end |